Move `PrepareForSampling` out of `init_mu` in `SampleRecorder::PopDead` Move `sample->PrepareForSampling(...)` out of `sample->init_mu` in `SampleRecorder<T>::PopDead` to avoid lock contention with `SampleRecorder<T>::Iterate` during potentially slow sample initialization, and remove `ABSL_EXCLUSIVE_LOCKS_REQUIRED(init_mu)` from `HashtablezInfo::PrepareForSampling` to match. PiperOrigin-RevId: 986859432
diff --git a/absl/container/internal/hashtablez_sampler.h b/absl/container/internal/hashtablez_sampler.h index e54c62e..540205e 100644 --- a/absl/container/internal/hashtablez_sampler.h +++ b/absl/container/internal/hashtablez_sampler.h
@@ -76,8 +76,7 @@ // blocking for any readers that are currently sampling the object. void PrepareForSampling(int64_t stride, size_t inline_element_size_value, size_t key_size, size_t value_size, - uint16_t soo_capacity_value) - ABSL_EXCLUSIVE_LOCKS_REQUIRED(init_mu); + uint16_t soo_capacity_value); // These fields are mutated by the various Record* APIs and need to be // thread-safe.
diff --git a/absl/profiling/internal/sample_recorder.h b/absl/profiling/internal/sample_recorder.h index 8663d6c..16f2c6e 100644 --- a/absl/profiling/internal/sample_recorder.h +++ b/absl/profiling/internal/sample_recorder.h
@@ -184,13 +184,10 @@ // preparation. } // `sample` is detached from the graveyard and will not be used in PopDead. - // SampleRecorder<T>::Iterate will acquire per sample lock, so there will be - // no data race either. - absl::MutexLock sample_lock(sample->init_mu); - // Note: consider PrepareForSampling out of the lock. - // Currently many clients have PrepareForSampling marked as - // ABSL_EXCLUSIVE_LOCKS_REQUIRED. + // SampleRecorder<T>::Iterate will only read `dead` that is still non-null. sample->PrepareForSampling(std::forward<Targs>(args)...); + // SampleRecorder<T>::Iterate acquires the same lock before access to `dead`. + absl::MutexLock sample_lock(sample->init_mu); sample->dead = nullptr; return sample; }