Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions compiler/code-gen/const-globals-batched-mem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(unsigned n_constants) {
constexpr auto BATCH_SIZE {106};
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{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) {
return (batch_count & mask) + (1 << (msb - 1));
}

return batch_count;
}

const ConstantsBatchedMem &ConstantsBatchedMem::prepare_mem_and_assign_offsets(const std::vector<VarPtr> &all_constants) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/code-gen/const-globals-batched-mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,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(unsigned n_constants);
static const ConstantsBatchedMem &prepare_mem_and_assign_offsets(const std::vector<VarPtr> &all_constants);

const std::vector<OneBatchInfo> &get_batches() const { return batches; }
Expand Down
Loading