LibJS: Only start ElapsedTimer for GC metrics when printing is enabled

We don't need to be checking the current time unconditionally when we
only observe the results if we're going to dump the GC stats.

This saves two trips to clock_gettime at the cost of an extra branch.
This commit is contained in:
Andrew Kaster 2023-01-01 22:45:28 -07:00 committed by Linus Groh
parent 88957b42e9
commit 4e7bdcfeea
Notes: sideshowbarker 2024-07-17 02:05:41 +09:00

View File

@ -96,7 +96,10 @@ void Heap::collect_garbage(CollectionType collection_type, bool print_report)
perf_event(PERF_EVENT_SIGNPOST, gc_perf_string_id, global_gc_counter++);
#endif
auto collection_measurement_timer = Core::ElapsedTimer::start_new();
Core::ElapsedTimer collection_measurement_timer;
if (print_report)
collection_measurement_timer.start();
if (collection_type == CollectionType::CollectGarbage) {
if (m_gc_deferrals) {
m_should_gc_when_deferral_ends = true;
@ -304,9 +307,8 @@ void Heap::sweep_dead_cells(bool print_report, Core::ElapsedTimer const& measure
});
}
int time_spent = measurement_timer.elapsed();
if (print_report) {
Time const time_spent = measurement_timer.elapsed_time();
size_t live_block_count = 0;
for_each_block([&](auto&) {
++live_block_count;
@ -315,7 +317,7 @@ void Heap::sweep_dead_cells(bool print_report, Core::ElapsedTimer const& measure
dbgln("Garbage collection report");
dbgln("=============================================");
dbgln(" Time spent: {} ms", time_spent);
dbgln(" Time spent: {} ms", time_spent.to_milliseconds());
dbgln(" Live cells: {} ({} bytes)", live_cells, live_cell_bytes);
dbgln("Collected cells: {} ({} bytes)", collected_cells, collected_cell_bytes);
dbgln(" Live blocks: {} ({} bytes)", live_block_count, live_block_count * HeapBlock::block_size);