@@ -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
8179std::optional<Context::QueryResult<TypeExtension *>>
8280Context::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
121112void Context::add (std::unique_ptr<Stmt> stmt) {
0 commit comments