From a2a399bc481bab6abf75c2b0a8bed51a64929774 Mon Sep 17 00:00:00 2001 From: Karim Shamazov Date: Tue, 28 Jul 2026 18:16:26 +0300 Subject: [PATCH 1/4] reduce batch count --- .../code-gen/const-globals-batched-mem.cpp | 28 +++++++++++-------- compiler/code-gen/const-globals-batched-mem.h | 3 +- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/compiler/code-gen/const-globals-batched-mem.cpp b/compiler/code-gen/const-globals-batched-mem.cpp index e02d0d6117..b78f77ba49 100644 --- a/compiler/code-gen/const-globals-batched-mem.cpp +++ b/compiler/code-gen/const-globals-batched-mem.cpp @@ -110,18 +110,22 @@ void ConstantsBatchedMem::inc_count_by_type(const TypeData *type) { } } -int ConstantsBatchedMem::detect_constants_batch_count(int n_constants) { - // these values are heuristics (don't use integer division, to avoid changing buckets count frequently) - if (n_constants > 1200000) return 2048; - if (n_constants > 800000) return 1536; - if (n_constants > 500000) return 1024; - if (n_constants > 100000) return 512; - if (n_constants > 10000) return 256; - if (n_constants > 5000) return 128; - if (n_constants > 1000) return 32; - if (n_constants > 500) return 16; - if (n_constants > 100) return 4; - return 1; +int ConstantsBatchedMem::detect_constants_batch_count(size_t n_constants) { + constexpr auto BATCH_SIZE {71}; + const auto batch_count{1 + n_constants / BATCH_SIZE}; + if (batch_count <= 2) { + return batch_count; + } + + // round to avoid changing buckets count frequently + const auto msb{std::bit_width(batch_count) - 1}; + const auto mask{(1 << msb) | (1 << (msb - 1))}; + const auto tail_mask{(1 << (msb - 1)) - 1}; + if (batch_count & tail_mask) { + return (batch_count & mask) + (1 << (msb - 1)); + } + + return batch_count; } const ConstantsBatchedMem &ConstantsBatchedMem::prepare_mem_and_assign_offsets(const std::vector &all_constants) { diff --git a/compiler/code-gen/const-globals-batched-mem.h b/compiler/code-gen/const-globals-batched-mem.h index 67970e036e..419ea0f6fc 100644 --- a/compiler/code-gen/const-globals-batched-mem.h +++ b/compiler/code-gen/const-globals-batched-mem.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include "compiler/data/data_ptr.h" @@ -55,7 +56,7 @@ class ConstantsBatchedMem { void inc_count_by_type(const TypeData *type); public: - static int detect_constants_batch_count(int n_constants); + static int detect_constants_batch_count(size_t n_constants); static const ConstantsBatchedMem &prepare_mem_and_assign_offsets(const std::vector &all_constants); const std::vector &get_batches() const { return batches; } From c18d9f8e5bcbbd37f0aab980bffdb0b0b4f419e8 Mon Sep 17 00:00:00 2001 From: Karim Shamazov Date: Tue, 28 Jul 2026 18:35:36 +0300 Subject: [PATCH 2/4] 106 --- compiler/code-gen/const-globals-batched-mem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/code-gen/const-globals-batched-mem.cpp b/compiler/code-gen/const-globals-batched-mem.cpp index b78f77ba49..b41ee909a6 100644 --- a/compiler/code-gen/const-globals-batched-mem.cpp +++ b/compiler/code-gen/const-globals-batched-mem.cpp @@ -111,7 +111,7 @@ void ConstantsBatchedMem::inc_count_by_type(const TypeData *type) { } int ConstantsBatchedMem::detect_constants_batch_count(size_t n_constants) { - constexpr auto BATCH_SIZE {71}; + constexpr auto BATCH_SIZE {106}; const auto batch_count{1 + n_constants / BATCH_SIZE}; if (batch_count <= 2) { return batch_count; From fd34b8a8c975deeea0e5d8323d6a777e37eb509d Mon Sep 17 00:00:00 2001 From: Karim Shamazov Date: Tue, 28 Jul 2026 18:47:40 +0300 Subject: [PATCH 3/4] c++17 --- compiler/code-gen/const-globals-batched-mem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/code-gen/const-globals-batched-mem.cpp b/compiler/code-gen/const-globals-batched-mem.cpp index b41ee909a6..94be4f0fe4 100644 --- a/compiler/code-gen/const-globals-batched-mem.cpp +++ b/compiler/code-gen/const-globals-batched-mem.cpp @@ -118,7 +118,7 @@ int ConstantsBatchedMem::detect_constants_batch_count(size_t n_constants) { } // round to avoid changing buckets count frequently - const auto msb{std::bit_width(batch_count) - 1}; + const auto msb{sizeof(size_t) * __CHAR_BIT__ - 1 - __builtin_clzl(batch_count)}; const auto mask{(1 << msb) | (1 << (msb - 1))}; const auto tail_mask{(1 << (msb - 1)) - 1}; if (batch_count & tail_mask) { From 1358b98a23365354d401edf331708f9a323bbf8c Mon Sep 17 00:00:00 2001 From: Karim Shamazov Date: Wed, 29 Jul 2026 12:30:53 +0300 Subject: [PATCH 4/4] correct usage of __builtin_clz --- compiler/code-gen/const-globals-batched-mem.cpp | 4 ++-- compiler/code-gen/const-globals-batched-mem.h | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/compiler/code-gen/const-globals-batched-mem.cpp b/compiler/code-gen/const-globals-batched-mem.cpp index 94be4f0fe4..f0afad84ec 100644 --- a/compiler/code-gen/const-globals-batched-mem.cpp +++ b/compiler/code-gen/const-globals-batched-mem.cpp @@ -110,7 +110,7 @@ void ConstantsBatchedMem::inc_count_by_type(const TypeData *type) { } } -int ConstantsBatchedMem::detect_constants_batch_count(size_t n_constants) { +int ConstantsBatchedMem::detect_constants_batch_count(unsigned n_constants) { constexpr auto BATCH_SIZE {106}; const auto batch_count{1 + n_constants / BATCH_SIZE}; if (batch_count <= 2) { @@ -118,7 +118,7 @@ int ConstantsBatchedMem::detect_constants_batch_count(size_t n_constants) { } // round to avoid changing buckets count frequently - const auto msb{sizeof(size_t) * __CHAR_BIT__ - 1 - __builtin_clzl(batch_count)}; + const auto msb{sizeof(unsigned) * __CHAR_BIT__ - 1 - __builtin_clz(batch_count)}; const auto mask{(1 << msb) | (1 << (msb - 1))}; const auto tail_mask{(1 << (msb - 1)) - 1}; if (batch_count & tail_mask) { diff --git a/compiler/code-gen/const-globals-batched-mem.h b/compiler/code-gen/const-globals-batched-mem.h index 419ea0f6fc..8d048d8ae7 100644 --- a/compiler/code-gen/const-globals-batched-mem.h +++ b/compiler/code-gen/const-globals-batched-mem.h @@ -4,7 +4,6 @@ #pragma once -#include #include #include "compiler/data/data_ptr.h" @@ -56,7 +55,7 @@ class ConstantsBatchedMem { void inc_count_by_type(const TypeData *type); public: - static int detect_constants_batch_count(size_t n_constants); + static int detect_constants_batch_count(unsigned n_constants); static const ConstantsBatchedMem &prepare_mem_and_assign_offsets(const std::vector &all_constants); const std::vector &get_batches() const { return batches; }