Skip to content

Commit ee9d515

Browse files
authored
Optimize wasm reading (#1202)
* optimize wasm reading: use a set of the breaks we've seen, don't rescan blocks to see if they have breaks to them
1 parent b3d9ad5 commit ee9d515

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/wasm-binary.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,7 @@ class WasmBinaryBuilder {
863863
BreakTarget(Name name, int arity) : name(name), arity(arity) {}
864864
};
865865
std::vector<BreakTarget> breakStack;
866+
std::unordered_set<Name> breakTargetNames;
866867
bool breaksToReturn; // whether a break is done to the function scope, which is in effect a return
867868

868869
std::vector<Expression*> expressionStack;

src/wasm/wasm-binary.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,6 +1629,7 @@ void WasmBinaryBuilder::readFunctions() {
16291629
useDebugLocation = false;
16301630
breaksToReturn = false;
16311631
// process body
1632+
assert(breakTargetNames.size() == 0);
16321633
assert(breakStack.empty());
16331634
breakStack.emplace_back(RETURN_BREAK, func->result != none); // the break target for the function scope
16341635
assert(expressionStack.empty());
@@ -1637,6 +1638,7 @@ void WasmBinaryBuilder::readFunctions() {
16371638
assert(depth == 0);
16381639
assert(breakStack.size() == 1);
16391640
breakStack.pop_back();
1641+
assert(breakTargetNames.size() == 0);
16401642
if (!expressionStack.empty()) {
16411643
throw ParseException("stack not empty on function exit");
16421644
}
@@ -2222,6 +2224,7 @@ void WasmBinaryBuilder::visitBlock(Block *curr) {
22222224
pushBlockElements(curr, start, end);
22232225
curr->finalize(curr->type);
22242226
breakStack.pop_back();
2227+
breakTargetNames.erase(curr->name);
22252228
}
22262229
}
22272230

@@ -2237,12 +2240,13 @@ Expression* WasmBinaryBuilder::getBlockOrSingleton(WasmType type) {
22372240
block->name = label;
22382241
block->finalize(type);
22392242
// maybe we don't need a block here?
2240-
if (!brokenTo(block)) {
2243+
if (breakTargetNames.find(block->name) == breakTargetNames.end()) {
22412244
block->name = Name();
22422245
if (block->list.size() == 1) {
22432246
return block->list[0];
22442247
}
22452248
}
2249+
breakTargetNames.erase(block->name);
22462250
return block;
22472251
}
22482252

@@ -2285,6 +2289,7 @@ void WasmBinaryBuilder::visitLoop(Loop *curr) {
22852289
curr->body = block;
22862290
}
22872291
breakStack.pop_back();
2292+
breakTargetNames.erase(curr->name);
22882293
curr->finalize(curr->type);
22892294
}
22902295

@@ -2301,7 +2306,9 @@ WasmBinaryBuilder::BreakTarget WasmBinaryBuilder::getBreakTarget(int32_t offset)
23012306
breaksToReturn = true;
23022307
}
23032308
if (debug) std::cerr << "breaktarget "<< breakStack[index].name << " arity " << breakStack[index].arity << std::endl;
2304-
return breakStack[index];
2309+
auto& ret = breakStack[index];
2310+
breakTargetNames.insert(ret.name);
2311+
return ret;
23052312
}
23062313

23072314
void WasmBinaryBuilder::visitBreak(Break *curr, uint8_t code) {

0 commit comments

Comments
 (0)