Store symbol names as a ImmutableString
This will enable compile-time initialization of built-in symbols as
well as reducing copying strings.
Most of the code that deals with names is changed to use
ImmutableString where it makes sense to avoid conversions.
The lexer/parser now allocate const char pointers into pool memory
instead of allocating TStrings. These are then converted to
ImmutableString upon entering TParseContext.
BUG=angleproject:2267
TEST=angle_unittests, angle_end2end_tests
Change-Id: I244d6271ea1ecf7150d4f89dfa388a7745a1150c
Reviewed-on: https://chromium-review.googlesource.com/881561
Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
diff --git a/src/compiler/translator/HashNames.cpp b/src/compiler/translator/HashNames.cpp
index df448d8..6f7a419 100644
--- a/src/compiler/translator/HashNames.cpp
+++ b/src/compiler/translator/HashNames.cpp
@@ -6,6 +6,8 @@
#include "compiler/translator/HashNames.h"
+#include "compiler/translator/ImmutableString.h"
+#include "compiler/translator/ImmutableStringBuilder.h"
#include "compiler/translator/IntermNode.h"
#include "compiler/translator/Symbol.h"
@@ -18,59 +20,72 @@
// GLSL ES 3.00.6 section 3.9: the maximum length of an identifier is 1024 characters.
static const unsigned int kESSLMaxIdentifierLength = 1024u;
-static const char *kHashedNamePrefix = "webgl_";
+constexpr const ImmutableString kHashedNamePrefix("webgl_");
// Can't prefix with just _ because then we might introduce a double underscore, which is not safe
// in GLSL (ESSL 3.00.6 section 3.8: All identifiers containing a double underscore are reserved for
// use by the underlying implementation). u is short for user-defined.
-static const char *kUnhashedNamePrefix = "_u";
-static const unsigned int kUnhashedNamePrefixLength = 2u;
+constexpr const ImmutableString kUnhashedNamePrefix("_u");
-TString HashName(const TString &name, ShHashFunction64 hashFunction)
+ImmutableString HashName(const ImmutableString &name, ShHashFunction64 hashFunction)
{
ASSERT(!name.empty());
ASSERT(hashFunction);
- khronos_uint64_t number = (*hashFunction)(name.c_str(), name.length());
- TStringStream stream;
- stream << kHashedNamePrefix << std::hex << number;
- TString hashedName = stream.str();
+ khronos_uint64_t number = (*hashFunction)(name.data(), name.length());
+
+ // Build the hashed name in place.
+ static const unsigned int kHexStrMaxLength = sizeof(number) * 2;
+ static const size_t kHashedNameMaxLength = kHashedNamePrefix.length() + kHexStrMaxLength;
+
+ ImmutableStringBuilder hashedName(kHashedNameMaxLength);
+ hashedName << kHashedNamePrefix;
+
+ hashedName.appendHex(number);
+
return hashedName;
}
} // anonymous namespace
-TString HashName(const TString &name, ShHashFunction64 hashFunction, NameMap *nameMap)
+ImmutableString HashName(const ImmutableString &name,
+ ShHashFunction64 hashFunction,
+ NameMap *nameMap)
{
if (hashFunction == nullptr)
{
- if (name.length() + kUnhashedNamePrefixLength > kESSLMaxIdentifierLength)
+ if (name.length() + kUnhashedNamePrefix.length() > kESSLMaxIdentifierLength)
{
// If the identifier length is already close to the limit, we can't prefix it. This is
// not a problem since there are no builtins or ANGLE's internal variables that would
// have as long names and could conflict.
return name;
}
- return kUnhashedNamePrefix + name;
+ ImmutableStringBuilder prefixedName(kUnhashedNamePrefix.length() + name.length());
+ prefixedName << kUnhashedNamePrefix << name;
+ return prefixedName;
}
if (nameMap)
{
- NameMap::const_iterator it = nameMap->find(name.c_str());
+ NameMap::const_iterator it = nameMap->find(name.data());
if (it != nameMap->end())
- return it->second.c_str();
+ {
+ // TODO(oetuaho): Would be nice if we didn't need to allocate a string here.
+ return ImmutableString(it->second);
+ }
}
- TString hashedName = HashName(name, hashFunction);
+ ImmutableString hashedName = HashName(name, hashFunction);
if (nameMap)
{
- (*nameMap)[name.c_str()] = hashedName.c_str();
+ (*nameMap)[name.data()] = hashedName.data();
}
return hashedName;
}
-TString HashName(const TSymbol *symbol, ShHashFunction64 hashFunction, NameMap *nameMap)
+ImmutableString HashName(const TSymbol *symbol, ShHashFunction64 hashFunction, NameMap *nameMap)
{
if (symbol->symbolType() == SymbolType::Empty)
{
- return TString();
+ return ImmutableString("");
}
if (symbol->symbolType() == SymbolType::AngleInternal ||
symbol->symbolType() == SymbolType::BuiltIn)