From a8fdc5a3fc500b0383da9cc7be300dcab85a87cb Mon Sep 17 00:00:00 2001 From: HÃ¥vard Pettersen Date: Thu, 19 May 2022 13:14:02 +0000 Subject: make sample count atomic --- .../src/vespa/searchlib/index/field_length_calculator.h | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'searchlib') diff --git a/searchlib/src/vespa/searchlib/index/field_length_calculator.h b/searchlib/src/vespa/searchlib/index/field_length_calculator.h index 35a18b432f9..15d4c5ec285 100644 --- a/searchlib/src/vespa/searchlib/index/field_length_calculator.h +++ b/searchlib/src/vespa/searchlib/index/field_length_calculator.h @@ -15,7 +15,7 @@ namespace search::index { */ class FieldLengthCalculator { std::atomic _average_field_length; - uint32_t _num_samples; // Capped by _max_num_samples + std::atomic _num_samples; // Capped by _max_num_samples uint32_t _max_num_samples; public: @@ -39,7 +39,7 @@ public: } double get_average_field_length() const { return _average_field_length.load(std::memory_order_relaxed); } - uint32_t get_num_samples() const { return _num_samples; } + uint32_t get_num_samples() const { return _num_samples.load(std::memory_order_relaxed); } uint32_t get_max_num_samples() const { return _max_num_samples; } FieldLengthInfo get_info() const { @@ -47,10 +47,12 @@ public: } void add_field_length(uint32_t field_length) { - if (_num_samples < _max_num_samples) { - ++_num_samples; + auto num_samples = get_num_samples(); + if (num_samples < _max_num_samples) { + ++num_samples; + _num_samples.store(num_samples, std::memory_order_relaxed); } - _average_field_length.store((_average_field_length.load(std::memory_order_relaxed) * (_num_samples - 1) + field_length) / _num_samples, std::memory_order_relaxed); + _average_field_length.store((_average_field_length.load(std::memory_order_relaxed) * (num_samples - 1) + field_length) / num_samples, std::memory_order_relaxed); } }; -- cgit v1.2.3