Skip to content

Commit

Permalink
joint arrays' buckets (#898)
Browse files Browse the repository at this point in the history
combine array buckets for string keys and for int keys into one universal bucket;
this allows to save significant amount of memory
  • Loading branch information
drdzyk authored Sep 12, 2023
1 parent 89c1c44 commit f409a40
Show file tree
Hide file tree
Showing 50 changed files with 602 additions and 585 deletions.
3 changes: 2 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ Checks: |-
-bugprone-easily-swappable-parameters,
-misc-no-recursion,
-misc-const-correctness,
-cppcoreguidelines-avoid-const-or-ref-data-members
-cppcoreguidelines-avoid-const-or-ref-data-members,
-cppcoreguidelines-macro-usage
CheckOptions:
- key: bugprone-suspicious-string-compare.WarnOnImplicitComparison
Expand Down
2 changes: 1 addition & 1 deletion compiler/code-gen/files/global_vars_memory_stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void GlobalVarsMemoryStats::compile(CodeGenerator &W) const {

FunctionSignatureGenerator(W) << "array<int64_t> " << getter_name_ << "(int64_t lower_bound) " << BEGIN
<< "array<int64_t> result;" << NL
<< "result.reserve(" << global_vars_count << ", " << global_vars_count << ", false);" << NL << NL;
<< "result.reserve(" << global_vars_count << ", false);" << NL << NL;

for (size_t part_id = 0; part_id < global_var_parts.size(); ++part_id) {
W << "void " << getter_name_ << "_" << part_id << "(int64_t lower_bound, array<int64_t> &result);" << NL
Expand Down
2 changes: 1 addition & 1 deletion compiler/code-gen/files/tl2cpp/tl2cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ void write_tl_query_handlers(CodeGenerator &W) {
W << END << ";" << NL << NL;

W << "void tl_magic_fill_all_functions_impl(array<string> &out) noexcept " << BEGIN
<< "out.reserve(" << tl->functions.size() << ", 0, false);" << NL
<< "out.reserve(" << tl->functions.size() << ", false);" << NL
<< "for (int i=0; i<" << tl->functions.size() << "; ++i)" << BEGIN
<< "out.set_value(static_cast<int64_t>(tl_magic_ids[i]), string(tl_magic_names[i]));" << NL << END
<< NL << END << NL;
Expand Down
13 changes: 5 additions & 8 deletions compiler/code-gen/raw-data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ DepLevelContainer::const_iterator &DepLevelContainer::const_iterator::operator++
}

static inline int array_len() {
return (10 * sizeof(int)) / sizeof(double);
return (8 * sizeof(int)) / sizeof(double);
}

std::vector<int> compile_arrays_raw_representation(const DepLevelContainer &const_raw_array_vars, CodeGenerator &W) {
Expand Down Expand Up @@ -149,18 +149,15 @@ std::vector<int> compile_arrays_raw_representation(const DepLevelContainer &cons
shifts.push_back(shift);
shift += array_len_in_doubles;

// stub, ref_cnt
W << "{ .is = { .a = 0, .b = " << ExtraRefCnt::for_global_const << "}},";
// is_vector_internal, ref_cnt
W << "{ .is = { .a = 1, .b = " << ExtraRefCnt::for_global_const << "}},";
// max_key
W << "{ .i64 = " << array_size - 1 << "},";
// end_.next, end_.prev
W << "{ .is = { .a = 0, .b = 0}},";

// int_size, int_buf_size
W << "{ .is = { .a = " << array_size << ", .b = " << array_size << "}},";

// string_size, string_buf_size
W << "{ .is = { .a = 0 , .b = " << std::numeric_limits<uint32_t>::max() << " }}";
// size, buf_size
W << "{ .is = { .a = " << array_size << ", .b = " << array_size << "}}";

auto args_end = vertex->args().end();
for (auto it = vertex->args().begin(); it != args_end; ++it) {
Expand Down
23 changes: 2 additions & 21 deletions compiler/code-gen/vertex-compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1845,35 +1845,17 @@ void compile_array(VertexAdaptor<op_array> root, CodeGenerator &W) {
}

bool has_double_arrow = false;
int int_cnt = 0, string_cnt = 0, xx_cnt = 0;
const int size = root->args().size();
for (size_t key_id = 0; key_id < root->args().size(); ++key_id) {
if (auto arrow = root->args()[key_id].try_as<op_double_arrow>()) {
VertexPtr key = VertexUtil::get_actual_value(arrow->key());
if (!has_double_arrow && key->type() == op_int_const) {
if (key.as<op_int_const>()->str_val == std::to_string(key_id)) {
root->args()[key_id] = arrow->value();
int_cnt++;
continue;
}
}
has_double_arrow = true;
PrimitiveType tp = tinf::get_type(key)->ptype();
if (tp == tp_int) {
int_cnt++;
} else {
if (tp == tp_string && key->type() == op_string) {
const std::string &key_str = key.as<op_string>()->str_val;
if (php_is_int(key_str.c_str(), key_str.size())) {
int_cnt++;
} else {
string_cnt++;
}
} else {
xx_cnt++;
}
}
} else {
int_cnt++;
}
}
if (n <= 10 && !has_double_arrow && type->ptype() == tp_array && root->extra_type != op_ex_safe_version) {
Expand All @@ -1893,8 +1875,7 @@ void compile_array(VertexAdaptor<op_array> root, CodeGenerator &W) {
W << "array <mixed>";
}
W << " (array_size ("
<< int_cnt + xx_cnt << ", "
<< string_cnt + xx_cnt << ", "
<< size << ", "
<< (has_double_arrow ? "false" : "true") << "));" << NL;
for (auto cur : root->args()) {
W << arr_name;
Expand Down
Loading

0 comments on commit f409a40

Please sign in to comment.