From 27e654a775d3767c3dcedaf583d97c4c8b84d60c Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 12 Oct 2022 13:27:58 -0700 Subject: [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. --- src/ir/subtypes.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src') 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& 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 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> typeSubTypes; }; -- cgit v1.2.3