summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2022-10-12 13:27:58 -0700
committerGitHub <noreply@github.com>2022-10-12 20:27:58 +0000
commit27e654a775d3767c3dcedaf583d97c4c8b84d60c (patch)
treeab20b42406e7cdde689ef34519dce149ec86f40f
parentc47febe1273d8c5d8db9f80d30eb65405ced0b18 (diff)
downloadbinaryen-27e654a775d3767c3dcedaf583d97c4c8b84d60c.tar.gz
binaryen-27e654a775d3767c3dcedaf583d97c4c8b84d60c.tar.bz2
binaryen-27e654a775d3767c3dcedaf583d97c4c8b84d60c.zip
[Wasm GC][NFC] Optimize getStrictSubTypes() (#5130)
Avoid allocating there. This is both faster and also it ensures we never modify our internal data structure after our constructor.
-rw-r--r--src/ir/subtypes.h12
1 files changed, 11 insertions, 1 deletions
diff --git a/src/ir/subtypes.h b/src/ir/subtypes.h
index 74c95032d..5a4ff87fc 100644
--- a/src/ir/subtypes.h
+++ b/src/ir/subtypes.h
@@ -37,7 +37,14 @@ struct SubTypes {
}
const std::vector<HeapType>& getStrictSubTypes(HeapType type) {
- return typeSubTypes[type];
+ if (auto iter = typeSubTypes.find(type); iter != typeSubTypes.end()) {
+ return iter->second;
+ }
+
+ // No entry exists. Return a canonical constant empty vec, to avoid
+ // allocation.
+ static const std::vector<HeapType> empty;
+ return empty;
}
// Get all subtypes of a type, and their subtypes and so forth, recursively.
@@ -75,6 +82,9 @@ private:
}
// Maps a type to its subtypes.
+ //
+ // After our constructor we never modify this data structure, so we can take
+ // references to the vectors here safely.
std::unordered_map<HeapType, std::vector<HeapType>> typeSubTypes;
};