summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2020-12-03 13:27:36 -0800
committerGitHub <noreply@github.com>2020-12-03 13:27:36 -0800
commit15356635e34ed8981c993d67ed90e83bc7bfa405 (patch)
tree02b69b8a8430fd8dddba703219168347afceacb3 /src
parent00f96c854ed3691b01fa35bba0a1d010d08958bd (diff)
downloadbinaryen-15356635e34ed8981c993d67ed90e83bc7bfa405.tar.gz
binaryen-15356635e34ed8981c993d67ed90e83bc7bfa405.tar.bz2
binaryen-15356635e34ed8981c993d67ed90e83bc7bfa405.zip
[Types] Return const& for HeapTypes and their Structs and Arrays. NFC (#3417)
I am starting to write lines like curr->value->type.getHeapType().getStruct().fields[curr->index].type and it scares me to think that there may be copies going on there. The issue is that Types are interned, but HeapTypes and their components are not.
Diffstat (limited to 'src')
-rw-r--r--src/wasm-type.h6
-rw-r--r--src/wasm/wasm-type.cpp24
2 files changed, 20 insertions, 10 deletions
diff --git a/src/wasm-type.h b/src/wasm-type.h
index 4a55d7b33..9019dfc9e 100644
--- a/src/wasm-type.h
+++ b/src/wasm-type.h
@@ -158,7 +158,7 @@ public:
FeatureSet getFeatures() const;
// Gets the heap type corresponding to this type
- HeapType getHeapType() const;
+ const HeapType& getHeapType() const;
// Returns a number type based on its size in bytes and whether it is a float
// type.
@@ -365,12 +365,12 @@ struct HeapType {
return signature;
}
bool isStruct() const { return kind == StructKind; }
- Struct getStruct() const {
+ const Struct& getStruct() const {
assert(isStruct() && "Not a struct");
return struct_;
}
bool isArray() const { return kind == ArrayKind; }
- Array getArray() const {
+ const Array& getArray() const {
assert(isArray() && "Not an array");
return array;
}
diff --git a/src/wasm/wasm-type.cpp b/src/wasm/wasm-type.cpp
index b323dcc06..a810f6f98 100644
--- a/src/wasm/wasm-type.cpp
+++ b/src/wasm/wasm-type.cpp
@@ -513,24 +513,34 @@ FeatureSet Type::getFeatures() const {
return getSingleFeatures(*this);
}
-HeapType Type::getHeapType() const {
+// getHeapType() returns a const HeapType&, so we need a canonical object to
+// return for the basic types, so that we don't create a temporary copy on each
+// call.
+namespace statics {
+static HeapType funcHeapType(HeapType::FuncKind),
+ externHeapType(HeapType::ExternKind), exnHeapType(HeapType::ExnKind),
+ anyHeapType(HeapType::AnyKind), eqHeapType(HeapType::EqKind),
+ i31HeapType(HeapType::I31Kind);
+}
+
+const HeapType& Type::getHeapType() const {
if (isRef()) {
if (isCompound()) {
return getTypeInfo(*this)->ref.heapType;
}
switch (getBasic()) {
case funcref:
- return HeapType::FuncKind;
+ return statics::funcHeapType;
case externref:
- return HeapType::ExternKind;
+ return statics::externHeapType;
case exnref:
- return HeapType::ExnKind;
+ return statics::exnHeapType;
case anyref:
- return HeapType::AnyKind;
+ return statics::anyHeapType;
case eqref:
- return HeapType::EqKind;
+ return statics::eqHeapType;
case i31ref:
- return HeapType::I31Kind;
+ return statics::i31HeapType;
default:
break;
}