commit fffd8298be288a0d5d046aa46882587a09bacf14 from: Sergey Bronnikov date: Thu Jun 13 18:32:01 2024 UTC test/fuzz: speedup string 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. NO_CHANGELOG=testing NO_DOC=testing commit - 9d3859b246977c307a22f5930755fe77e374a634 commit + fffd8298be288a0d5d046aa46882587a09bacf14 blob - b4500da9cad3d96ed0406836754e3336d1abe269 blob + 3895f1547c712dcbb87860f238a8369bb8bff502 --- test/fuzz/luaL_loadbuffer/serializer.cc +++ test/fuzz/luaL_loadbuffer/serializer.cc @@ -435,6 +435,8 @@ ClearIdentifier(const std::string &identifier) } else if (std::isalpha(c) || c == '_') { has_first_not_digit = true; cleared += c; + } else { + cleared += '_'; } } return cleared; @@ -456,12 +458,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; } @@ -951,7 +954,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"; @@ -1196,8 +1199,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 */