From 4f8b8000f8a1e15e0ad8f88010f6bd3a6f40ef80 Mon Sep 17 00:00:00 2001 From: Chris Kennelly CA Date: Sat, 12 Sep 2026 13:13:12 -0700 Subject: [PATCH] Strength-reduce per-span division in CentralFreeList::DeallocateSpans. Precompute ms_per_cycle = 1000/frequency once outside the lifetime-histogram loop and multiply per span, instead of dividing by frequency each iteration. The legacy TCMALLOC_INTERNAL_LEGACY_LOCKING path is unchanged. PiperOrigin-RevId: 980422454 --- tcmalloc/central_freelist.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tcmalloc/central_freelist.h b/tcmalloc/central_freelist.h index 03f6ea0ca..e0a605a95 100644 --- a/tcmalloc/central_freelist.h +++ b/tcmalloc/central_freelist.h @@ -671,11 +671,21 @@ void CentralFreeList::DeallocateSpans(absl::Span spans) { if (objects_per_span_ > 1) { const double now = forwarder_.clock_now(); const double frequency = forwarder_.clock_frequency(); +#ifndef TCMALLOC_INTERNAL_LEGACY_LOCKING + // Precompute the cycles->milliseconds factor once so the per-span + // conversion is a multiply instead of a floating-point division. + const double ms_per_cycle = 1000.0 / frequency; +#endif for (Span* span : spans) { const double elapsed = std::max(now - static_cast(span->AllocTime()), 0.0); +#ifdef TCMALLOC_INTERNAL_LEGACY_LOCKING const absl::Duration lifetime = absl::Milliseconds(elapsed * 1000 / frequency); +#else + const absl::Duration lifetime = + absl::Milliseconds(elapsed * ms_per_cycle); +#endif completed_spans_[LifetimeBucketNum(lifetime)].LossyAdd(1); } }