From 7f4a3fdac8733b202b53296b60274f55923f61be Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Fri, 24 Mar 2023 15:09:54 +0800 Subject: [PATCH 1/3] bloom_filter: use vector::back() when appropriate no need to use `size - 1` for accessing the last element in a vector, let's just use `vector::back()` for more compacted code. Signed-off-by: Kefu Chai --- utils/bloom_calculations.hh | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/utils/bloom_calculations.hh b/utils/bloom_calculations.hh index 425ead7aa2..c553671a9c 100644 --- a/utils/bloom_calculations.hh +++ b/utils/bloom_calculations.hh @@ -130,9 +130,7 @@ namespace bloom_calculations { * lower than this, it will throw an unsupported_operation_exception. */ inline double min_supported_bloom_filter_fp_chance() { - int max_buckets = probs.size() - 1; - int max_K = probs[max_buckets].size() - 1; - return probs[max_buckets][max_K]; + return probs.back().back(); } } From 1a82a7ac7299cff77d36ee76f800365f8b4d25de Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Fri, 24 Mar 2023 15:29:48 +0800 Subject: [PATCH 2/3] bloom_filter: add more constness to false positive rate tables we never mutate them, so mark them const for better readability. Signed-off-by: Kefu Chai --- utils/bloom_calculations.cc | 4 ++-- utils/bloom_calculations.hh | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/utils/bloom_calculations.cc b/utils/bloom_calculations.cc index 951a03b041..2a268f7a6b 100644 --- a/utils/bloom_calculations.cc +++ b/utils/bloom_calculations.cc @@ -21,7 +21,7 @@ namespace bloom_calculations { * Each cell (i,j) the false positive rate determined by using i buckets per * element and j hash functions. */ -std::vector> probs = { +const std::vector> probs = { {1.0}, // dummy row representing 0 buckets per element {1.0, 1.0}, // dummy row representing 1 buckets per element {1.0, 0.393, 0.400}, @@ -68,6 +68,6 @@ std::vector initialize_opt_k() { return arr; } -std::vector opt_k_per_buckets = initialize_opt_k(); +const std::vector opt_k_per_buckets = initialize_opt_k(); } } diff --git a/utils/bloom_calculations.hh b/utils/bloom_calculations.hh index c553671a9c..d82de4582b 100644 --- a/utils/bloom_calculations.hh +++ b/utils/bloom_calculations.hh @@ -45,8 +45,8 @@ namespace bloom_calculations { int constexpr min_k = 1; int constexpr EXCESS = 20; - extern std::vector> probs; - extern std::vector opt_k_per_buckets; + extern const std::vector> probs; + extern const std::vector opt_k_per_buckets; /** * Given the number of buckets that can be used per element, return a From a7b4f84b6a16a6f2a376d70338a47848625a0b30 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Fri, 24 Mar 2023 15:35:24 +0800 Subject: [PATCH 3/3] bloom_filter: mark internal help function static as `initialize_opt_k()` is not used out side of the translation unit, let's mark it `static`. Signed-off-by: Kefu Chai --- utils/bloom_calculations.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/bloom_calculations.cc b/utils/bloom_calculations.cc index 2a268f7a6b..ffe5cf8e47 100644 --- a/utils/bloom_calculations.cc +++ b/utils/bloom_calculations.cc @@ -50,7 +50,7 @@ const std::vector> probs = { * The optimal number of hashes for a given number of bits per element. * These values are automatically calculated from the data above. */ -std::vector initialize_opt_k() { +static std::vector initialize_opt_k() { std::vector arr; arr.resize(probs.size());