Skip to content

Commit

Permalink
Use flat abseil map with util hasher. Add big_int hasher.
Browse files Browse the repository at this point in the history
  • Loading branch information
fruffy committed Apr 27, 2024
1 parent cc4a991 commit 3acde1b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 3 additions & 2 deletions ir/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.

#include <ostream>

#include "absl/container/flat_hash_map.h"
#include "ir/id.h"
#include "ir/indexed_vector.h"
#include "ir/ir.h"
Expand Down Expand Up @@ -147,7 +148,7 @@ const IR::Constant *IR::Constant::get(const IR::Type *t, big_int v, Util::Source
}
// Constants are interned. Keys in the intern map are pairs of types and values.
using key_t = std::tuple<int, RTTI::TypeId, bool, big_int>;
static std::map<key_t, const Constant *> CONSTANTS;
static absl::flat_hash_map<key_t, const Constant *, Util::Hash> CONSTANTS;

auto *&result = CONSTANTS[{tb->width_bits(), t->typeId(), tb->isSigned, v}];
if (result == nullptr) {
Expand Down Expand Up @@ -176,7 +177,7 @@ const IR::StringLiteral *IR::StringLiteral::get(cstring value, const IR::Type *t
}
// String literals are interned.
using key_t = std::pair<cstring, const IR::Type *>;
static std::map<key_t, const IR::StringLiteral *> STRINGS;
static absl::flat_hash_map<key_t, const IR::StringLiteral *, Util::Hash> STRINGS;

auto *&result = STRINGS[{value, t}];
if (result == nullptr) {
Expand Down
5 changes: 5 additions & 0 deletions lib/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <tuple>
#include <type_traits>

#include "lib/big_int_util.h"

namespace Util {

namespace Detail {
Expand Down Expand Up @@ -176,6 +178,9 @@ struct Hasher<signed char> : Detail::IntegerHasher<signed char> {};
template <>
struct Hasher<char> : Detail::IntegerHasher<char> {};

template <>
struct Hasher<big_int> : Detail::StdHasher {};

template <>
struct Hasher<float> : Detail::FloatHasher<float> {};

Expand Down

0 comments on commit 3acde1b

Please sign in to comment.