From 3810baf44cd63ded64f886d38aea10d6a05450e0 Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Tue, 26 Oct 2021 00:00:57 +0300 Subject: [PATCH 1/2] CompiledExpressionCache limit elements size --- programs/server/Server.cpp | 7 ++++++- programs/server/config.xml | 5 ++++- programs/server/config.yaml.example | 5 ++++- src/Common/LRUCache.h | 14 +++++++------- src/Interpreters/JIT/CompiledExpressionCache.cpp | 4 ++-- src/Interpreters/JIT/CompiledExpressionCache.h | 2 +- 6 files changed, 24 insertions(+), 13 deletions(-) diff --git a/programs/server/Server.cpp b/programs/server/Server.cpp index d850ca45e26..2be9e57e437 100644 --- a/programs/server/Server.cpp +++ b/programs/server/Server.cpp @@ -957,9 +957,14 @@ if (ThreadFuzzer::instance().isEffective()) global_context->setMMappedFileCache(mmap_cache_size); #if USE_EMBEDDED_COMPILER + /// 128 MB constexpr size_t compiled_expression_cache_size_default = 1024 * 1024 * 128; size_t compiled_expression_cache_size = config().getUInt64("compiled_expression_cache_size", compiled_expression_cache_size_default); - CompiledExpressionCacheFactory::instance().init(compiled_expression_cache_size); + + constexpr size_t compiled_expression_cache_elements_size_default = 10000; + size_t compiled_expression_cache_elements_size = config().getUInt64("compiled_expression_cache_elements_size", compiled_expression_cache_elements_size_default); + + CompiledExpressionCacheFactory::instance().init(compiled_expression_cache_size, compiled_expression_cache_elements_size); #endif /// Set path for format schema files diff --git a/programs/server/config.xml b/programs/server/config.xml index e38a6daeaed..37f36aa5215 100644 --- a/programs/server/config.xml +++ b/programs/server/config.xml @@ -351,9 +351,12 @@ --> 1000 - + 134217728 + + 10000 + /var/lib/clickhouse/ diff --git a/programs/server/config.yaml.example b/programs/server/config.yaml.example index b2d016e06e5..c312e6a2208 100644 --- a/programs/server/config.yaml.example +++ b/programs/server/config.yaml.example @@ -279,9 +279,12 @@ mark_cache_size: 5368709120 # also it can be dropped manually by the SYSTEM DROP MMAP CACHE query. mmap_cache_size: 1000 -# Cache size for compiled expressions. +# Cache size in bytes for compiled expressions. compiled_expression_cache_size: 134217728 +# Cache size in elements for compiled expressions. +compiled_expression_cache_elements_size: 10000 + # Path to data directory, with trailing slash. path: /var/lib/clickhouse/ diff --git a/src/Common/LRUCache.h b/src/Common/LRUCache.h index 93abfce3c4e..3df27cbdce9 100644 --- a/src/Common/LRUCache.h +++ b/src/Common/LRUCache.h @@ -36,12 +36,10 @@ public: using Mapped = TMapped; using MappedPtr = std::shared_ptr; -private: - using Clock = std::chrono::steady_clock; - -public: - LRUCache(size_t max_size_) - : max_size(std::max(static_cast(1), max_size_)) {} + LRUCache(size_t max_size_, size_t max_elements_size_ = 0) + : max_size(std::max(static_cast(1), max_size_)) + , max_elements_size(max_elements_size_) + {} MappedPtr get(const Key & key) { @@ -252,6 +250,7 @@ private: /// Total weight of values. size_t current_size = 0; const size_t max_size; + const size_t max_elements_size; std::atomic hits {0}; std::atomic misses {0}; @@ -311,7 +310,8 @@ private: { size_t current_weight_lost = 0; size_t queue_size = cells.size(); - while ((current_size > max_size) && (queue_size > 1)) + + while ((current_size > max_size || (max_elements_size != 0 && queue_size > max_elements_size)) && (queue_size > 1)) { const Key & key = queue.front(); diff --git a/src/Interpreters/JIT/CompiledExpressionCache.cpp b/src/Interpreters/JIT/CompiledExpressionCache.cpp index 98f4eec982d..674e02236f5 100644 --- a/src/Interpreters/JIT/CompiledExpressionCache.cpp +++ b/src/Interpreters/JIT/CompiledExpressionCache.cpp @@ -16,12 +16,12 @@ CompiledExpressionCacheFactory & CompiledExpressionCacheFactory::instance() return factory; } -void CompiledExpressionCacheFactory::init(size_t cache_size) +void CompiledExpressionCacheFactory::init(size_t cache_size_in_bytes, size_t cache_size_in_elements) { if (cache) throw Exception(ErrorCodes::LOGICAL_ERROR, "CompiledExpressionCache was already initialized"); - cache = std::make_unique(cache_size); + cache = std::make_unique(cache_size_in_bytes, cache_size_in_elements); } CompiledExpressionCache * CompiledExpressionCacheFactory::tryGetCache() diff --git a/src/Interpreters/JIT/CompiledExpressionCache.h b/src/Interpreters/JIT/CompiledExpressionCache.h index 5182a77d77a..7d20627d5d2 100644 --- a/src/Interpreters/JIT/CompiledExpressionCache.h +++ b/src/Interpreters/JIT/CompiledExpressionCache.h @@ -52,7 +52,7 @@ private: public: static CompiledExpressionCacheFactory & instance(); - void init(size_t cache_size); + void init(size_t cache_size_in_bytes, size_t cache_size_in_elements); CompiledExpressionCache * tryGetCache(); }; From dbf481102986780667fac1da04b91ae8258450cd Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Tue, 26 Oct 2021 13:00:42 +0300 Subject: [PATCH 2/2] Updated documentation --- src/Common/LRUCache.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Common/LRUCache.h b/src/Common/LRUCache.h index 3df27cbdce9..bbc09fd3aff 100644 --- a/src/Common/LRUCache.h +++ b/src/Common/LRUCache.h @@ -36,6 +36,9 @@ public: using Mapped = TMapped; using MappedPtr = std::shared_ptr; + /** Initialize LRUCache with max_size and max_elements_size. + * max_elements_size == 0 means no elements size restrictions. + */ LRUCache(size_t max_size_, size_t max_elements_size_ = 0) : max_size(std::max(static_cast(1), max_size_)) , max_elements_size(max_elements_size_)