From 9e2522568200ace0799ba7d73145fe9e42400767 Mon Sep 17 00:00:00 2001 From: ansh1406 Date: Mon, 13 Jul 2026 00:35:55 +0530 Subject: [PATCH] src: use constexpr table for GetOctalCode Replace the runtime std::vector cache in GetOctalCode() with a compile-time constexpr lookup table of std::string_view Signed-off-by: Ansh Swaroop --- src/embedded_data.cc | 60 +++++++++++++++++++++++++++----------------- src/embedded_data.h | 4 +-- tools/js2c.cc | 4 +-- 3 files changed, 41 insertions(+), 27 deletions(-) diff --git a/src/embedded_data.cc b/src/embedded_data.cc index b5c4d28d8b400d..2a4f93b47bab61 100644 --- a/src/embedded_data.cc +++ b/src/embedded_data.cc @@ -1,33 +1,47 @@ #include "embedded_data.h" -#include +#include namespace node { -std::string ToOctalString(const uint8_t ch) { - // We can print most printable characters directly. The exceptions are '\' - // (escape characters), " (would end the string), and ? (trigraphs). The - // latter may be overly conservative: we compile with C++17 which doesn't - // support trigraphs. - if (ch >= ' ' && ch <= '~' && ch != '\\' && ch != '"' && ch != '?') { - return std::string(1, static_cast(ch)); +namespace { + +struct OctalStr { + char data[5] = {0}; + + constexpr OctalStr() = default; + explicit constexpr OctalStr(uint8_t ch) { + // We can print most printable characters directly. The exceptions are '\' + // (escape characters), " (would end the string), and ? (trigraphs). The + // latter may be overly conservative: we compile with C++20 which doesn't + // support trigraphs. + if (ch >= ' ' && ch <= '~' && ch != '\\' && ch != '"' && ch != '?') { + data[0] = static_cast(ch); + } else { + data[0] = '\\'; + data[1] = '0' + ((ch >> 6) & 7); + data[2] = '0' + ((ch >> 3) & 7); + data[3] = '0' + (ch & 7); + } } - // All other characters are blindly output as octal. - const char c0 = '0' + ((ch >> 6) & 7); - const char c1 = '0' + ((ch >> 3) & 7); - const char c2 = '0' + (ch & 7); - return std::string("\\") + c0 + c1 + c2; -} -std::vector GetOctalTable() { - size_t size = 1 << 8; - std::vector code_table(size); - for (size_t i = 0; i < size; ++i) { - code_table[i] = ToOctalString(static_cast(i)); + constexpr std::string_view view() const { + return {data, data[1] == '\0' ? 1u : 4u}; + } +}; + +constexpr auto MakeOctalTable() { + std::array table{}; + for (unsigned i = 0; i < 256; ++i) { + table[i] = OctalStr(static_cast(i)); } - return code_table; + return table; } -const std::string& GetOctalCode(uint8_t index) { - static std::vector table = GetOctalTable(); - return table[index]; +constexpr auto octal_table = MakeOctalTable(); + +} // namespace + +std::string_view GetOctalCode(uint8_t index) { + return octal_table[index].view(); } + } // namespace node diff --git a/src/embedded_data.h b/src/embedded_data.h index 84cd5b76dca108..35d154f6c87eac 100644 --- a/src/embedded_data.h +++ b/src/embedded_data.h @@ -2,7 +2,7 @@ #define SRC_EMBEDDED_DATA_H_ #include -#include +#include // This file must not depend on node.h or other code that depends on // the full Node.js implementation because it is used during the @@ -10,7 +10,7 @@ namespace node { -const std::string& GetOctalCode(uint8_t index); +std::string_view GetOctalCode(uint8_t index); } // namespace node diff --git a/tools/js2c.cc b/tools/js2c.cc index 2cb09f8e1d7ba6..e3b69ae4eb2cf8 100644 --- a/tools/js2c.cc +++ b/tools/js2c.cc @@ -515,8 +515,8 @@ Fragment GetDefinitionImpl(const std::vector& code, i, ch); } - const std::string& str = GetOctalCode(ch); - memcpy(result.data() + cur, str.c_str(), str.size()); + std::string_view str = GetOctalCode(ch); + memcpy(result.data() + cur, str.data(), str.size()); cur += str.size(); } }