Explorar o código

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.
Andrew Kaster %!s(int64=2) %!d(string=hai) anos
pai
achega
4e7bdcfeea
Modificáronse 1 ficheiros con 6 adicións e 4 borrados
  1. 6 4
      Userland/Libraries/LibJS/Heap/Heap.cpp

+ 6 - 4
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);