From 4e7bdcfeea8db8bae7cd908217983f237cd344ef Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Sun, 1 Jan 2023 22:45:28 -0700 Subject: [PATCH] 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. --- Userland/Libraries/LibJS/Heap/Heap.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibJS/Heap/Heap.cpp b/Userland/Libraries/LibJS/Heap/Heap.cpp index a2c95203b6..d8fe220e8b 100644 --- a/Userland/Libraries/LibJS/Heap/Heap.cpp +++ b/Userland/Libraries/LibJS/Heap/Heap.cpp @@ -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);