Skip to content

Commit b4673be

Browse files
Merge pull request ClickHouse#79976 from ClickHouse/less_locking_in_ht_stats
Reduce locking in `HashTablesStatistics`
2 parents 5eaa932 + 4302492 commit b4673be

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

src/Interpreters/HashTablesStatistics.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ std::optional<Entry> HashTablesStatistics<Entry>::getSizeHint(const Params & par
1717
if (!params.isCollectionAndUseEnabled())
1818
throw DB::Exception(DB::ErrorCodes::LOGICAL_ERROR, "Collection and use of the statistics should be enabled.");
1919

20-
std::lock_guard lock(mutex);
21-
const auto cache = getHashTableStatsCache(params, lock);
20+
const auto cache = getHashTableStatsCache(params);
2221
if (const auto hint = cache->get(params.key))
2322
{
2423
LOG_TRACE(getLogger("HashTablesStatistics"), "An entry for key={} found in cache: {}", params.key, hint->dump());
@@ -34,8 +33,7 @@ void HashTablesStatistics<Entry>::update(const Entry & new_entry, const Params &
3433
if (!params.isCollectionAndUseEnabled())
3534
throw DB::Exception(DB::ErrorCodes::LOGICAL_ERROR, "Collection and use of the statistics should be enabled.");
3635

37-
std::lock_guard lock(mutex);
38-
const auto cache = getHashTableStatsCache(params, lock);
36+
const auto cache = getHashTableStatsCache(params);
3937
const auto hint = cache->get(params.key);
4038
// We'll maintain the maximum among all the observed values until another prediction is much lower (that should indicate some change)
4139
if (!hint || hint->shouldBeUpdated(new_entry))
@@ -60,9 +58,9 @@ std::optional<HashTablesCacheStatistics> HashTablesStatistics<Entry>::getCacheSt
6058
}
6159

6260
template <typename Entry>
63-
HashTablesStatistics<Entry>::CachePtr
64-
HashTablesStatistics<Entry>::getHashTableStatsCache(const Params & params, const std::lock_guard<std::mutex> &)
61+
HashTablesStatistics<Entry>::CachePtr HashTablesStatistics<Entry>::getHashTableStatsCache(const Params & params)
6562
{
63+
std::lock_guard lock(mutex);
6664
if (!hash_table_stats)
6765
hash_table_stats = std::make_shared<Cache>(params.max_entries_for_hash_table_stats * sizeof(Entry));
6866
return hash_table_stats;

src/Interpreters/HashTablesStatistics.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ class HashTablesStatistics
8080
std::optional<DB::HashTablesCacheStatistics> getCacheStats() const;
8181

8282
private:
83-
CachePtr getHashTableStatsCache(const Params & params, const std::lock_guard<std::mutex> &);
83+
CachePtr getHashTableStatsCache(const Params & params);
8484

8585
mutable std::mutex mutex;
86-
CachePtr hash_table_stats;
86+
CachePtr hash_table_stats TSA_GUARDED_BY(mutex);
8787
};
8888

8989
template <typename Entry>

0 commit comments

Comments
 (0)