Use string_view in Mangler.

This is more lightweight than String and avoids a handful of copies.

Change-Id: Iad72a1b1027877f41bab10eeeaa340f2823a0896
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/449359
Commit-Queue: John Stiles <johnstiles@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
This commit is contained in:
John Stiles 2021-09-15 21:47:00 -04:00 committed by SkCQ
parent 25868511fd
commit 769146f7d3
5 changed files with 12 additions and 13 deletions

View File

@ -556,7 +556,7 @@ std::unique_ptr<Statement> Inliner::inlineStatement(int offset,
// regard, but see `InlinerAvoidsVariableNameOverlap` for a counterexample where unique
// names are important.
const String* name = symbolTableForStatement->takeOwnershipOfString(
fMangler.uniqueName(String(variable.name()), symbolTableForStatement));
fMangler.uniqueName(variable.name(), symbolTableForStatement));
auto clonedVar = std::make_unique<Variable>(
offset,
&variable.modifiers(),
@ -579,7 +579,7 @@ std::unique_ptr<Statement> Inliner::inlineStatement(int offset,
}
}
Inliner::InlineVariable Inliner::makeInlineVariable(const String& baseName,
Inliner::InlineVariable Inliner::makeInlineVariable(skstd::string_view baseName,
const Type* type,
SymbolTable* symbolTable,
Modifiers modifiers,
@ -660,7 +660,7 @@ Inliner::InlinedCall Inliner::inlineCall(FunctionCall* call,
// for void-return functions, or in cases that are simple enough that we can just replace
// the function-call node with the result expression.
std::unique_ptr<Expression> noInitialValue;
InlineVariable var = this->makeInlineVariable(String(function.declaration().name()),
InlineVariable var = this->makeInlineVariable(function.declaration().name(),
&function.declaration().returnType(),
symbolTable.get(), Modifiers{},
caller->isBuiltin(), &noInitialValue);
@ -685,7 +685,7 @@ Inliner::InlinedCall Inliner::inlineCall(FunctionCall* call,
continue;
}
}
InlineVariable var = this->makeInlineVariable(String(param->name()), &arguments[i]->type(),
InlineVariable var = this->makeInlineVariable(param->name(), &arguments[i]->type(),
symbolTable.get(), param->modifiers(),
caller->isBuiltin(), &arguments[i]);
inlineStatements.push_back(std::move(var.fVarDecl));

View File

@ -101,7 +101,7 @@ private:
const Variable* fVarSymbol;
std::unique_ptr<Statement> fVarDecl;
};
InlineVariable makeInlineVariable(const String& baseName,
InlineVariable makeInlineVariable(skstd::string_view baseName,
const Type* type,
SymbolTable* symbolTable,
Modifiers modifiers,

View File

@ -10,7 +10,7 @@
namespace SkSL {
String Mangler::uniqueName(String baseName, SymbolTable* symbolTable) {
String Mangler::uniqueName(skstd::string_view baseName, SymbolTable* symbolTable) {
SkASSERT(symbolTable);
// The inliner runs more than once, so the base name might already have been mangled and have a
// prefix like "_123_x". Let's strip that prefix off to make the generated code easier to read.
@ -23,12 +23,12 @@ String Mangler::uniqueName(String baseName, SymbolTable* symbolTable) {
// If we found digits, another underscore, and anything else, that's the mangler prefix.
// Strip it off.
if (offset > 1 && baseName[offset] == '_' && baseName[offset + 1] != '\0') {
baseName.erase(0, offset + 1);
baseName.remove_prefix(offset + 1);
} else {
// This name doesn't contain a mangler prefix, but it does start with an underscore.
// OpenGL disallows two consecutive underscores anywhere in the string, and we'll be
// adding one as part of the mangler prefix, so strip the leading underscore.
baseName.erase(0, 1);
baseName.remove_prefix(1);
}
}
@ -37,9 +37,8 @@ String Mangler::uniqueName(String baseName, SymbolTable* symbolTable) {
// isn't fully comprehensive, as code isn't always generated in top-to-bottom order.)
String uniqueName;
for (;;) {
uniqueName = String::printf("_%d_%s", fCounter++, baseName.c_str());
skstd::string_view frag{uniqueName.data(), uniqueName.length()};
if ((*symbolTable)[frag] == nullptr) {
uniqueName = String::printf("_%d_%.*s", fCounter++, (int)baseName.size(), baseName.data());
if ((*symbolTable)[uniqueName] == nullptr) {
break;
}
}

View File

@ -19,7 +19,7 @@ public:
/**
* Mangles baseName to create a name that is unique within symbolTable.
*/
String uniqueName(String baseName, SymbolTable* symbolTable);
String uniqueName(skstd::string_view baseName, SymbolTable* symbolTable);
void reset() {
fCounter = 0;

View File

@ -107,7 +107,7 @@ const SkSL::Modifiers* DSLWriter::Modifiers(const SkSL::Modifiers& modifiers) {
skstd::string_view DSLWriter::Name(skstd::string_view name) {
if (ManglingEnabled()) {
const String* s = SymbolTable()->takeOwnershipOfString(
Instance().fMangler.uniqueName(SkString(name).c_str(), SymbolTable().get()));
Instance().fMangler.uniqueName(name, SymbolTable().get()));
return s->c_str();
}
return name;