Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
reducing memory usage and making GetCode's buffer potentially evaluat…
…ed at compile time
  • Loading branch information
lemire authored and anonrig committed Sep 8, 2024
commit af4aeac64dbf0625333885ae7afae8968885a62c
42 changes: 35 additions & 7 deletions tools/js2c.cc
Original file line number Diff line number Diff line change
Expand Up @@ -359,17 +359,45 @@ std::string GetVariableName(const std::string& id) {
return result;
}

static const std::array<std::string, 65536> GetCodeTable() {
std::array<std::string, 65536> table{};
for (size_t i = 0; i < 65536; ++i) {
table[i] = std::to_string(i) + ',';


constexpr std::pair<std::array<char, 382106>,std::array<uint32_t, 65537>> precompute_string() {
Comment thread
jasnell marked this conversation as resolved.
Outdated
Comment thread
anonrig marked this conversation as resolved.
Outdated
Comment thread
anonrig marked this conversation as resolved.
Outdated
std::array<char, 382106> str;
Comment thread
anonrig marked this conversation as resolved.
std::array<uint32_t, 65537> off;
Comment thread
anonrig marked this conversation as resolved.
off[0] = 0;
char *p = &str[0];
// We roll our own int to string conversion to get constexpr
constexpr auto const_int_to_str = [](uint16_t value, char *s) -> size_t {
int index = 0;
do {
s[index++] = '0' + (value % 10);
value /= 10;
} while (value != 0);

for (int i = 0; i < index / 2; ++i) {
char temp = s[i];
s[i] = s[index - i - 1];
s[index - i - 1] = temp;
}
s[index] = ',';
return index;
};
for (int i = 0; i < 65536; ++i) {
size_t offset = const_int_to_str(i, p);
p += offset;
off[i + 1] = off[i] + offset;
}
return table;
return {str, off};
}

const std::string_view GetCode(uint16_t index) {
static std::array<std::string, 65536> table = GetCodeTable();
return table[index];
// uses about 644254 bytes of memory. An array of 65536 strings might use
// 2097152 bytes so we save 3x the memory
// Furthermore, compilers such as GCC will evaluate precompute_string() at compile time, thus
// potentially speeding up the program's startup time.
static auto [backing_string, offsets] = precompute_string();
return std::string_view(&backing_string[offsets[index]],
offsets[index + 1] - offsets[index]);
}

#ifdef NODE_JS2C_USE_STRING_LITERALS
Expand Down