Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
commit d677c2eb77e21f1ff33100a945b7af6fd9f94957
Author: Adrian Wielgosik <adrian.wielgosik@gemius.com>
Date: Mon May 14 12:07:56 2018 +0200
faster
diff --git a/source/include/ir/type.h b/source/include/ir/type.h
index 2ddc5a9a..f940894b 100644
--- a/source/include/ir/type.h
+++ b/source/include/ir/type.h
@@ -37,26 +37,6 @@ namespace fir
struct ConstantArray;
struct Function;
-
- template <typename T>
- struct TypeCache
- {
- std::vector<T*> cache;
- T* getOrAddCachedType(T* type)
- {
- for(auto ty : this->cache)
- {
- if(ty->isTypeEqual(type))
- {
- if(type != ty) delete type;
- return ty;
- }
- }
-
- return *(cache.insert(cache.end(), type));
- }
- };
-
enum class TypeKind
{
Invalid,
@@ -254,7 +234,36 @@ namespace fir
+ struct HashTypeByStr
+ {
+ size_t operator()(fir::Type* x) const
+ {
+ return std::hash<std::string>()(x->str());
+ }
+ };
+
+ struct TypesEqual
+ {
+ bool operator()(fir::Type* x, fir::Type* y) const
+ {
+ return x->isTypeEqual(y);
+ }
+ };
+ template <typename T>
+ struct TypeCache
+ {
+ std::unordered_set<T*, HashTypeByStr, TypesEqual> cache;
+ T* getOrAddCachedType(T* type)
+ {
+ if (auto it = cache.find(type); it != cache.end()) {
+ delete type;
+ return *it;
+ }
+ cache.insert(type);
+ return type;
+ }
+ };
diff --git a/source/include/precompile.h b/source/include/precompile.h
index 1560ba55..8eb1455b 100644
--- a/source/include/precompile.h
+++ b/source/include/precompile.h
@@ -21,6 +21,7 @@
#include <algorithm>
#include <functional>
#include <unordered_map>
+#include <unordered_set>
#ifndef __has_include
#error "Please switch to a compiler that supports '__has_include'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment