Skip to content

Commit

Permalink
test/fuzz: speedup string serialization
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
ligurio authored and Buristan committed Jun 17, 2024
1 parent c5b3e59 commit 3d97334
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions test/fuzz/luaL_loadbuffer/serializer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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 */
Expand Down

0 comments on commit 3d97334

Please sign in to comment.