Skip to content

Instantly share code, notes, and snippets.

@adrian17
Last active May 15, 2018 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrian17/eb53f1661c64f154f6bcad255bcdf427 to your computer and use it in GitHub Desktop.
Save adrian17/eb53f1661c64f154f6bcad255bcdf427 to your computer and use it in GitHub Desktop.
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