From 5927385942c950048df3143651f72ec27ce7be31 Mon Sep 17 00:00:00 2001 From: Jeremy Maitin-Shepard Date: Tue, 22 Oct 2024 21:12:14 -0700 Subject: [PATCH] Reduce contention on cache hash table mutex Previously, the mutex was unnecessarily held while destroying cache entries in the case that `total_bytes_limit == 0`, leading to contention on the mutex in the case that destruction is expensive (e.g. release a large memory allocation). PiperOrigin-RevId: 688801635 Change-Id: I347f5c6312920bb6b4ae4b3396dbd6414654dfaa --- tensorstore/internal/cache/cache.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tensorstore/internal/cache/cache.cc b/tensorstore/internal/cache/cache.cc index 317755a9f..454f8db6b 100644 --- a/tensorstore/internal/cache/cache.cc +++ b/tensorstore/internal/cache/cache.cc @@ -362,6 +362,9 @@ void StrongPtrTraitsCacheEntry::decrement_impl( cache->reference_count_.fetch_sub(CacheImpl::kNonEmptyShardIncrement, std::memory_order_relaxed); } + // Release lock before invoking entry destructor, as that may be + // expensive. + lock = {}; delete entry_impl; } } else {