From 3d97334fa3e1ea6a94058c73137f115eb2160b3c Mon Sep 17 00:00:00 2001 From: Sergey Bronnikov Date: Thu, 13 Jun 2024 21:24:25 +0300 Subject: [PATCH] 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 --- test/fuzz/luaL_loadbuffer/serializer.cc | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/test/fuzz/luaL_loadbuffer/serializer.cc b/test/fuzz/luaL_loadbuffer/serializer.cc index b4500da9cad3..3895f1547c71 100644 --- a/test/fuzz/luaL_loadbuffer/serializer.cc +++ b/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, Variable) { 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 */