Hash :
89398b65
Author :
Date :
2018-03-21T17:30:50
Avoid mangled name comparisons of 3-parameter functions The hash values used for looking up built-ins now encode whether the mangled name contains arrays, structs or interface blocks in its parameters list. This is written in the most significant bit of the hash value. We check this bit at the start of built-in lookup and if the bit is set we exit early. After that we know that the lookup name doesn't contain array, struct or interface block parameters. When we find a hash that matches a hash of a built-in function, we now know 3 things: 1) the length of the mangled name matches 2) the open parentheses in the mangled name matches 3) the lookup doesn't contain array, struct or block parameters. Additionally, we have an if statement checking whether the function name matches. Collisions are only possible with functions that 1) have the same name 2) have the same number of parameters With these preconditions we can check beforehand whether collisions are possible for 3-parameter functions. If there are no collisions, we don't need to compare the full mangled name. This is similar to what was already being done with functions that had 0 to 2 parameters. This reduces shader_translator binary size by around 4 KB on Windows. Besides increased complexity, the tradeoff is that an exhaustive search of hash values for possible 3-parameter combinations is costly, so the gen_builtin_functions.py code generation script now takes around one minute to run on a high-end workstation. Due to this, the script now exits early if it detects it has already been run with the same inputs based on a hash value stored in builtin_symbols_hash_autogen.txt. BUG=angleproject:2267 BUG=chromium:823856 TEST=angle_unittests Change-Id: I3ff8c6eb85b90d3c4971ac8d73ee171a07a7e55f Reviewed-on: https://chromium-review.googlesource.com/973372 Reviewed-by: Corentin Wallez <cwallez@chromium.org> Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
//
// Copyright (c) 2018 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// ImmutableString.h: Wrapper for static or pool allocated char arrays, that are guaranteed to be
// valid and unchanged for the duration of the compilation.
//
#ifndef COMPILER_TRANSLATOR_IMMUTABLESTRING_H_
#define COMPILER_TRANSLATOR_IMMUTABLESTRING_H_
#include <string>
#include "common/string_utils.h"
#include "compiler/translator/Common.h"
namespace sh
{
namespace
{
constexpr size_t constStrlen(const char *str)
{
if (str == nullptr)
{
return 0u;
}
size_t len = 0u;
while (*(str + len) != '\0')
{
++len;
}
return len;
}
}
class ImmutableString
{
public:
// The data pointer passed in must be one of:
// 1. nullptr (only valid with length 0).
// 2. a null-terminated static char array like a string literal.
// 3. a null-terminated pool allocated char array. This can't be c_str() of a local TString,
// since when a TString goes out of scope it clears its first character.
explicit constexpr ImmutableString(const char *data) : mData(data), mLength(constStrlen(data))
{
}
constexpr ImmutableString(const char *data, size_t length) : mData(data), mLength(length) {}
ImmutableString(const std::string &str)
: mData(AllocatePoolCharArray(str.c_str(), str.size())), mLength(str.size())
{
}
constexpr ImmutableString(const ImmutableString &) = default;
ImmutableString &operator=(const ImmutableString &) = default;
constexpr const char *data() const { return mData ? mData : ""; }
constexpr size_t length() const { return mLength; }
char operator[](size_t index) const { return data()[index]; }
constexpr bool empty() const { return mLength == 0; }
bool beginsWith(const char *prefix) const { return angle::BeginsWith(data(), prefix); }
constexpr bool beginsWith(const ImmutableString &prefix) const
{
return mLength >= prefix.length() && memcmp(data(), prefix.data(), prefix.length()) == 0;
}
bool contains(const char *substr) const { return strstr(data(), substr) != nullptr; }
constexpr bool operator==(const ImmutableString &b) const
{
if (mLength != b.mLength)
{
return false;
}
return memcmp(data(), b.data(), mLength) == 0;
}
constexpr bool operator!=(const ImmutableString &b) const { return !(*this == b); }
constexpr bool operator==(const char *b) const
{
if (b == nullptr)
{
return empty();
}
return strcmp(data(), b) == 0;
}
constexpr bool operator!=(const char *b) const { return !(*this == b); }
bool operator==(const std::string &b) const
{
return mLength == b.length() && memcmp(data(), b.c_str(), mLength) == 0;
}
bool operator!=(const std::string &b) const { return !(*this == b); }
constexpr bool operator<(const ImmutableString &b) const
{
if (mLength < b.mLength)
{
return true;
}
if (mLength > b.mLength)
{
return false;
}
return (memcmp(data(), b.data(), mLength) < 0);
}
template <size_t hashBytes>
struct FowlerNollVoHash
{
static const size_t kFnvOffsetBasis;
static const size_t kFnvPrime;
constexpr size_t operator()(const ImmutableString &a) const
{
const char *data = a.data();
size_t hash = kFnvOffsetBasis;
while ((*data) != '\0')
{
hash = hash ^ (*data);
hash = hash * kFnvPrime;
++data;
}
return hash;
}
};
// This hash encodes the opening parentheses location (if any), name length and whether the name
// contains { or [ characters in addition to a 19-bit hash. This way the hash is more useful for
// lookups. The string passed in should be at most 63 characters.
uint32_t mangledNameHash() const;
private:
const char *mData;
size_t mLength;
};
} // namespace sh
std::ostream &operator<<(std::ostream &os, const sh::ImmutableString &str);
#endif // COMPILER_TRANSLATOR_IMMUTABLESTRING_H_