Skip to content

Commit e7e66b6

Browse files
committed
[Res] revert back to the old string cache keys as they proved to be cheaper than freezing the types
1 parent 0bdc130 commit e7e66b6

2 files changed

Lines changed: 27 additions & 43 deletions

File tree

include/res.h

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ class Context final {
7777
std::vector<diag::DiagBuilder> diags = {};
7878
};
7979

80-
struct ExtensionCache final {
80+
struct ExtensionCache
81+
: private std::unordered_map<std::string, QueryResult<TypeExtension *>> {
8182
void insertIfMissing(Type *type,
8283
TraitType *trait,
8384
int depth,
@@ -87,16 +88,8 @@ class Context final {
8788
get(Type *type, TraitType *trait, int depth) const;
8889

8990
private:
90-
struct Key {
91-
Type *type;
92-
Type *trait;
93-
int depth;
94-
};
95-
96-
std::vector<std::pair<Key, QueryResult<TypeExtension *>>> entries;
97-
98-
res::Type *freezeType(res::Type *type) const;
99-
bool eq(Type *lhs, Type *rhs) const;
91+
void addTypeToKey(Type *type, std::stringstream &ss) const;
92+
std::string getKey(Type *type, TraitType *trait, int depth) const;
10093
};
10194

10295
private:

src/res.cpp

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -71,51 +71,42 @@ void Context::ExtensionCache::insertIfMissing(
7171
TraitType *trait,
7272
int depth,
7373
QueryResult<TypeExtension *> result) {
74-
if (get(type, trait, depth))
75-
return;
76-
77-
entries.emplace_back(Key{freezeType(type), freezeType(trait), depth},
78-
std::move(result));
74+
std::string key = getKey(type, trait, depth);
75+
if (!get(type, trait, depth))
76+
emplace(key, std::move(result));
7977
}
8078

8179
std::optional<Context::QueryResult<TypeExtension *>>
8280
Context::ExtensionCache::get(Type *type, TraitType *trait, int depth) const {
83-
for (auto &&[key, result] : entries)
84-
if (depth == key.depth && eq(type, key.type) && eq(trait, key.trait))
85-
return result;
81+
auto result = find(getKey(type, trait, depth));
82+
if (result != end())
83+
return result->second;
8684

8785
return std::nullopt;
8886
}
8987

90-
res::Type *Context::ExtensionCache::freezeType(res::Type *type) const {
91-
if (!type)
92-
return nullptr;
93-
94-
auto *frozenType = type->getRootType();
95-
for (auto &arg : frozenType->args)
96-
arg = freezeType(arg);
88+
void Context::ExtensionCache::addTypeToKey(Type *type,
89+
std::stringstream &ss) const {
90+
type = type->getRootType();
91+
ss << type->baseName;
92+
std::visit([&ss](auto &&arg) { ss << '[' << arg << ']'; }, type->metadata);
9793

98-
return frozenType;
94+
for (auto &&arg : type->args)
95+
addTypeToKey(arg, ss);
9996
}
10097

101-
bool Context::ExtensionCache::eq(Type *lhs, Type *rhs) const {
102-
if (!lhs)
103-
return !rhs;
104-
105-
if (!rhs)
106-
return false;
107-
108-
lhs = lhs->getRootType();
109-
rhs = rhs->getRootType();
110-
111-
if (!lhs->isSameKind(rhs))
112-
return false;
98+
std::string
99+
Context::ExtensionCache::getKey(Type *type, TraitType *trait, int depth) const {
100+
std::stringstream ss;
113101

114-
for (size_t i = 0; i < lhs->args.size(); ++i)
115-
if (!eq(lhs->args[i], rhs->args[i]))
116-
return false;
102+
for (auto &&t : std::initializer_list<res::Type *>{type, trait}) {
103+
if (t)
104+
addTypeToKey(t, ss);
105+
ss << ':';
106+
}
117107

118-
return true;
108+
ss << depth;
109+
return ss.str();
119110
}
120111

121112
void Context::add(std::unique_ptr<Stmt> stmt) {

0 commit comments

Comments
 (0)