commit 0e1f4280aba1f2fecf96dddcea048175aef3f60a from: Sergey Bronnikov date: Tue Jun 18 10:16:37 2024 UTC tests/capi: speedup protobuf serialization - clamp before cleaning string because cleaning is not cheap (O(n), where max n is equal to kMaxStrLength) - call cleaning for identifiers only, there is no sense to cleaning string literals - replace symbols disallowed by Lua grammar in indentifier's names with '_' The patch saves 16 sec on 145k samples (401 sec before the patch and 385 sec after the patch). It is actually not so much, but it is about 2.5 min per hour. commit - cd53055cc1a0c32a2e9b03387262cbdac411193c commit + 0e1f4280aba1f2fecf96dddcea048175aef3f60a blob - c8c992333b9121d1382eb7be32cc1284ee323b8a blob + e318464f054ba37a73aec30bbbc417d5ba32b773 --- tests/capi/luaL_loadbuffer_proto/serializer.cc +++ tests/capi/luaL_loadbuffer_proto/serializer.cc @@ -444,6 +444,8 @@ ClearIdentifier(const std::string &identifier) } else if (std::isalpha(c) || c == '_') { has_first_not_digit = true; cleared += c; + } else { + cleared += '_'; } } return cleared; @@ -465,12 +467,13 @@ clamp(double number, double upper, double lower) } inline std::string -ConvertToStringDefault(const std::string &s) +ConvertToStringDefault(const std::string &s, bool sanitize = false) { - std::string ident = ClearIdentifier(s); - ident = clamp(ident); + std::string ident = clamp(s); + if (sanitize) + ident = ClearIdentifier(ident); if (ident.empty()) - return std::string(kDefaultIdent); + ident = std::string(kDefaultIdent); return ident; } @@ -960,7 +963,7 @@ NESTED_PROTO_TOSTRING(IndexWithName, indexname, Variab { std::string indexname_str = PrefixExpressionToString( indexname.prefixexp()); - std::string idx_str = ConvertToStringDefault(indexname.name()); + std::string idx_str = ConvertToStringDefault(indexname.name(), true); /* Prevent using reserved keywords as indices. */ if (KReservedLuaKeywords.find(idx_str) != KReservedLuaKeywords.end()) { idx_str += "_1"; @@ -1205,8 +1208,12 @@ PROTO_TOSTRING(UnaryOperator, op) */ PROTO_TOSTRING(Name, name) { - std::string ident = ConvertToStringDefault(name.name()); - return ident + std::to_string(name.num() % kMaxIdentifiers); + std::string ident = ConvertToStringDefault(name.name(), true); + /* Identifier has default name, add an index. */ + if (!ident.compare(kDefaultIdent)) { + ident += std::to_string(name.num() % kMaxIdentifiers); + } + return ident; } } /* namespace */