Skip to content

Commit 127f968

Browse files
committed
allocate only expressions in arenas - functions, imports, exports, function types, can more simply be held by unique_ptrs on the owning module. this avoids need to coordinate arena allocation for their elements, and only the far more plentiful expression nodes are a perf factor anyhow
1 parent 46f99e6 commit 127f968

13 files changed

Lines changed: 82 additions & 82 deletions

src/asm2wasm.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,13 @@ class Asm2WasmBuilder {
216216
// function types. we fill in this information as we see
217217
// uses, in the first pass
218218

219-
std::map<IString, FunctionType*> importedFunctionTypes;
219+
std::map<IString, std::unique_ptr<FunctionType>> importedFunctionTypes;
220220
std::map<IString, std::vector<CallImport*>> importedFunctionCalls;
221221

222222
void noteImportedFunctionCall(Ref ast, WasmType resultType, AsmData *asmData, CallImport* call) {
223223
assert(ast[0] == CALL && ast[1][0] == NAME);
224224
IString importName = ast[1][1]->getIString();
225-
auto* type = allocator.alloc<FunctionType>();
225+
std::unique_ptr<FunctionType> type = make_unique<FunctionType>();
226226
type->name = IString((std::string("type$") + importName.str).c_str(), false); // TODO: make a list of such types
227227
type->result = resultType;
228228
Ref args = ast[2];
@@ -231,7 +231,7 @@ class Asm2WasmBuilder {
231231
}
232232
// if we already saw this signature, verify it's the same (or else handle that)
233233
if (importedFunctionTypes.find(importName) != importedFunctionTypes.end()) {
234-
FunctionType* previous = importedFunctionTypes[importName];
234+
FunctionType* previous = importedFunctionTypes[importName].get();
235235
#if 0
236236
std::cout << "compare " << importName.str << "\nfirst: ";
237237
type.print(std::cout, 0);
@@ -254,7 +254,7 @@ class Asm2WasmBuilder {
254254
}
255255
}
256256
} else {
257-
importedFunctionTypes[importName] = type;
257+
importedFunctionTypes[importName].swap(type);
258258
}
259259
importedFunctionCalls[importName].push_back(call);
260260
}
@@ -508,7 +508,7 @@ void Asm2WasmBuilder::processAsm(Ref ast) {
508508
}
509509
}
510510
}
511-
auto import = allocator.alloc<Import>();
511+
auto import = new Import();
512512
import->name = name;
513513
import->module = moduleName;
514514
import->base = imported[2]->getIString();
@@ -678,7 +678,7 @@ void Asm2WasmBuilder::processAsm(Ref ast) {
678678
getTempRet0 = value;
679679
}
680680
assert(wasm.checkFunction(value));
681-
auto export_ = allocator.alloc<Export>();
681+
auto export_ = new Export;
682682
export_->name = key;
683683
export_->value = value;
684684
wasm.addExport(export_);
@@ -690,7 +690,7 @@ void Asm2WasmBuilder::processAsm(Ref ast) {
690690

691691
std::vector<IString> toErase;
692692

693-
for (auto* import : wasm.imports) {
693+
for (auto& import : wasm.imports) {
694694
IString name = import->name;
695695
if (importedFunctionTypes.find(name) != importedFunctionTypes.end()) {
696696
// special math builtins
@@ -699,7 +699,7 @@ void Asm2WasmBuilder::processAsm(Ref ast) {
699699
import->type = builtin;
700700
continue;
701701
}
702-
import->type = ensureFunctionType(getSig(importedFunctionTypes[name]), &wasm);
702+
import->type = ensureFunctionType(getSig(importedFunctionTypes[name].get()), &wasm);
703703
} else if (import->module != ASM2WASM) { // special-case the special module
704704
// never actually used
705705
toErase.push_back(name);
@@ -714,7 +714,7 @@ void Asm2WasmBuilder::processAsm(Ref ast) {
714714
for (auto& pair : importedFunctionCalls) {
715715
IString name = pair.first;
716716
auto& list = pair.second;
717-
auto type = importedFunctionTypes[name];
717+
auto type = importedFunctionTypes[name].get();
718718
for (auto* call : list) {
719719
for (size_t i = call->operands.size(); i < type->params.size(); i++) {
720720
auto val = allocator.alloc<Const>();
@@ -754,7 +754,7 @@ void Asm2WasmBuilder::processAsm(Ref ast) {
754754
{ builder.makeGetLocal(0, i32) }
755755
)
756756
));
757-
auto export_ = allocator.alloc<Export>();
757+
auto export_ = new Export;
758758
export_->name = export_->value = GROW_WASM_MEMORY;
759759
wasm.addExport(export_);
760760
}
@@ -869,7 +869,7 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) {
869869
std::cout << '\n';
870870
}
871871

872-
auto function = allocator.alloc<Function>();
872+
auto function = new Function;
873873
function->name = name;
874874
Ref params = ast[2];
875875
Ref body = ast[3];
@@ -1017,7 +1017,7 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) {
10171017
static bool addedImport = false;
10181018
if (!addedImport) {
10191019
addedImport = true;
1020-
auto import = allocator.alloc<Import>(); // f64-rem = asm2wasm.f64-rem;
1020+
auto import = new Import; // f64-rem = asm2wasm.f64-rem;
10211021
import->name = F64_REM;
10221022
import->module = ASM2WASM;
10231023
import->base = F64_REM;
@@ -1055,7 +1055,7 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) {
10551055
static bool addedImport = false;
10561056
if (!addedImport) {
10571057
addedImport = true;
1058-
auto import = allocator.alloc<Import>(); // debugger = asm2wasm.debugger;
1058+
auto import = new Import; // debugger = asm2wasm.debugger;
10591059
import->name = DEBUGGER;
10601060
import->module = ASM2WASM;
10611061
import->base = DEBUGGER;
@@ -1168,7 +1168,7 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) {
11681168
static bool addedImport = false;
11691169
if (!addedImport) {
11701170
addedImport = true;
1171-
auto import = allocator.alloc<Import>(); // f64-to-int = asm2wasm.f64-to-int;
1171+
auto import = new Import; // f64-to-int = asm2wasm.f64-to-int;
11721172
import->name = F64_TO_INT;
11731173
import->module = ASM2WASM;
11741174
import->base = F64_TO_INT;

src/asm_v_wasm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ std::string getSig(WasmType result, const ListType& operands) {
5555

5656
WasmType sigToWasmType(char sig);
5757

58-
FunctionType sigToFunctionType(std::string sig);
58+
FunctionType* sigToFunctionType(std::string sig);
5959

6060
FunctionType* ensureFunctionType(std::string sig, Module* wasm);
6161

src/asmjs/asm_v_wasm.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ WasmType sigToWasmType(char sig) {
8282
}
8383
}
8484

85-
FunctionType* sigToFunctionType(std::string sig, MixedArena& allocator) {
86-
auto ret = allocator.alloc<FunctionType>();
85+
FunctionType* sigToFunctionType(std::string sig) {
86+
auto ret = new FunctionType;
8787
ret->result = sigToWasmType(sig[0]);
8888
for (size_t i = 1; i < sig.size(); i++) {
8989
ret->params.push_back(sigToWasmType(sig[i]));
@@ -97,7 +97,7 @@ FunctionType* ensureFunctionType(std::string sig, Module* wasm) {
9797
return wasm->getFunctionType(name);
9898
}
9999
// add new type
100-
auto type = wasm->allocator.alloc<FunctionType>();
100+
auto type = new FunctionType;
101101
type->name = name;
102102
type->result = sigToWasmType(sig[0]);
103103
for (size_t i = 1; i < sig.size(); i++) {

src/passes/Print.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -522,17 +522,17 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
522522
}
523523
for (auto& child : curr->functionTypes) {
524524
doIndent(o, indent);
525-
visitFunctionType(child, true);
525+
visitFunctionType(child.get(), true);
526526
o << maybeNewLine;
527527
}
528528
for (auto& child : curr->imports) {
529529
doIndent(o, indent);
530-
visitImport(child);
530+
visitImport(child.get());
531531
o << maybeNewLine;
532532
}
533533
for (auto& child : curr->exports) {
534534
doIndent(o, indent);
535-
visitExport(child);
535+
visitExport(child.get());
536536
o << maybeNewLine;
537537
}
538538
if (curr->table.names.size() > 0) {
@@ -542,7 +542,7 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
542542
}
543543
for (auto& child : curr->functions) {
544544
doIndent(o, indent);
545-
visitFunction(child);
545+
visitFunction(child.get());
546546
o << maybeNewLine;
547547
}
548548
decIndent();

src/passes/RemoveImports.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@ struct RemoveImports : public WalkerPass<PostWalker<RemoveImports, Visitor<Remov
4848
}
4949

5050
void visitModule(Module *curr) {
51-
auto imports = curr->imports; // copy
52-
for (auto* import : imports) {
53-
curr->removeImport(import->name);
51+
std::vector<Name> names;
52+
for (auto& import : curr->imports) {
53+
names.push_back(import->name);
54+
}
55+
for (auto& name : names) {
56+
curr->removeImport(name);
5457
}
5558
}
5659
};

src/wasm-binary.h

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -447,9 +447,9 @@ class WasmBinaryWriter : public Visitor<WasmBinaryWriter, void> {
447447

448448
void prepare() {
449449
// we need function types for all our functions
450-
for (auto* func : wasm->functions) {
450+
for (auto& func : wasm->functions) {
451451
if (func->type.isNull()) {
452-
func->type = ensureFunctionType(getSig(func), wasm)->name;
452+
func->type = ensureFunctionType(getSig(func.get()), wasm)->name;
453453
}
454454
}
455455
}
@@ -523,7 +523,7 @@ class WasmBinaryWriter : public Visitor<WasmBinaryWriter, void> {
523523
if (debug) std::cerr << "== writeSignatures" << std::endl;
524524
auto start = startSection(BinaryConsts::Section::Signatures);
525525
o << U32LEB(wasm->functionTypes.size());
526-
for (auto* type : wasm->functionTypes) {
526+
for (auto& type : wasm->functionTypes) {
527527
if (debug) std::cerr << "write one" << std::endl;
528528
o << int8_t(BinaryConsts::TypeForms::Basic);
529529
o << U32LEB(type->params.size());
@@ -553,7 +553,7 @@ class WasmBinaryWriter : public Visitor<WasmBinaryWriter, void> {
553553
if (debug) std::cerr << "== writeImports" << std::endl;
554554
auto start = startSection(BinaryConsts::Section::ImportTable);
555555
o << U32LEB(wasm->imports.size());
556-
for (auto* import : wasm->imports) {
556+
for (auto& import : wasm->imports) {
557557
if (debug) std::cerr << "write one" << std::endl;
558558
o << U32LEB(getFunctionTypeIndex(import->type->name));
559559
writeInlineString(import->module.str);
@@ -606,7 +606,7 @@ class WasmBinaryWriter : public Visitor<WasmBinaryWriter, void> {
606606
if (debug) std::cerr << "== writeFunctionSignatures" << std::endl;
607607
auto start = startSection(BinaryConsts::Section::FunctionSignatures);
608608
o << U32LEB(wasm->functions.size());
609-
for (auto* curr : wasm->functions) {
609+
for (auto& curr : wasm->functions) {
610610
if (debug) std::cerr << "write one" << std::endl;
611611
o << U32LEB(getFunctionTypeIndex(curr->type));
612612
}
@@ -623,7 +623,7 @@ class WasmBinaryWriter : public Visitor<WasmBinaryWriter, void> {
623623
if (debug) std::cerr << "write one at" << o.size() << std::endl;
624624
size_t sizePos = writeU32LEBPlaceholder();
625625
size_t start = o.size();
626-
Function* function = wasm->functions[i];
626+
Function* function = wasm->getFunction(i);
627627
mappedLocals.clear();
628628
numLocalsByType.clear();
629629
if (debug) std::cerr << "writing" << function->name << std::endl;
@@ -654,7 +654,7 @@ class WasmBinaryWriter : public Visitor<WasmBinaryWriter, void> {
654654
if (debug) std::cerr << "== writeexports" << std::endl;
655655
auto start = startSection(BinaryConsts::Section::ExportTable);
656656
o << U32LEB(wasm->exports.size());
657-
for (auto* curr : wasm->exports) {
657+
for (auto& curr : wasm->exports) {
658658
if (debug) std::cerr << "write one" << std::endl;
659659
o << U32LEB(getFunctionIndex(curr->value));
660660
writeInlineString(curr->name.str);
@@ -720,7 +720,7 @@ class WasmBinaryWriter : public Visitor<WasmBinaryWriter, void> {
720720
if (debug) std::cerr << "== writeNames" << std::endl;
721721
auto start = startSection(BinaryConsts::Section::Names);
722722
o << U32LEB(wasm->functions.size());
723-
for (auto* curr : wasm->functions) {
723+
for (auto& curr : wasm->functions) {
724724
writeInlineString(curr->name.str);
725725
o << U32LEB(0); // TODO: locals
726726
}
@@ -1354,7 +1354,7 @@ class WasmBinaryBuilder {
13541354
if (debug) std::cerr << "num: " << numTypes << std::endl;
13551355
for (size_t i = 0; i < numTypes; i++) {
13561356
if (debug) std::cerr << "read one" << std::endl;
1357-
auto curr = allocator.alloc<FunctionType>();
1357+
auto curr = new FunctionType;
13581358
auto form = getInt8();
13591359
assert(form == BinaryConsts::TypeForms::Basic);
13601360
size_t numParams = getU32LEB();
@@ -1379,11 +1379,11 @@ class WasmBinaryBuilder {
13791379
if (debug) std::cerr << "num: " << num << std::endl;
13801380
for (size_t i = 0; i < num; i++) {
13811381
if (debug) std::cerr << "read one" << std::endl;
1382-
auto curr = allocator.alloc<Import>();
1382+
auto curr = new Import;
13831383
curr->name = Name(std::string("import$") + std::to_string(i));
13841384
auto index = getU32LEB();
13851385
assert(index < wasm.functionTypes.size());
1386-
curr->type = wasm.functionTypes[index];
1386+
curr->type = wasm.getFunctionType(index);
13871387
assert(curr->type->name.is());
13881388
curr->module = getInlineString();
13891389
curr->base = getInlineString();
@@ -1400,8 +1400,7 @@ class WasmBinaryBuilder {
14001400
for (size_t i = 0; i < num; i++) {
14011401
if (debug) std::cerr << "read one" << std::endl;
14021402
auto index = getU32LEB();
1403-
assert(index < wasm.functionTypes.size());
1404-
functionTypes.push_back(wasm.functionTypes[index]);
1403+
functionTypes.push_back(wasm.getFunctionType(index));
14051404
}
14061405
}
14071406

@@ -1481,7 +1480,7 @@ class WasmBinaryBuilder {
14811480
if (debug) std::cerr << "num: " << num << std::endl;
14821481
for (size_t i = 0; i < num; i++) {
14831482
if (debug) std::cerr << "read one" << std::endl;
1484-
auto curr = allocator.alloc<Export>();
1483+
auto curr = new Export;
14851484
auto index = getU32LEB();
14861485
assert(index < functionTypes.size());
14871486
curr->name = getInlineString();
@@ -1774,7 +1773,7 @@ class WasmBinaryBuilder {
17741773
void visitCallIndirect(CallIndirect *curr) {
17751774
if (debug) std::cerr << "zz node: CallIndirect" << std::endl;
17761775
auto arity = getU32LEB();
1777-
curr->fullType = wasm.functionTypes[getU32LEB()];
1776+
curr->fullType = wasm.getFunctionType(getU32LEB());
17781777
auto num = curr->fullType->params.size();
17791778
assert(num == arity);
17801779
curr->operands.resize(num);

src/wasm-builder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Builder {
4545
WasmType resultType,
4646
std::vector<NameType>&& vars,
4747
Expression* body = nullptr) {
48-
auto* func = allocator.alloc<Function>();
48+
auto* func = new Function;
4949
func->name = name;
5050
func->result = resultType;
5151
func->body = body;

src/wasm-linker.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void Linker::layout() {
5656
Name target = f.first;
5757
// Create an import for the target if necessary.
5858
if (!out.wasm.checkImport(target)) {
59-
auto import = out.wasm.allocator.alloc<Import>();
59+
auto import = new Import;
6060
import->name = import->base = target;
6161
import->module = ENV;
6262
import->type = ensureFunctionType(getSig(*f.second.begin()), &out.wasm);
@@ -154,7 +154,7 @@ void Linker::layout() {
154154
if (out.symbolInfo.implementedFunctions.count(start) != 0) {
155155
Fatal() << "Start function already present: `" << start << "`\n";
156156
}
157-
auto* func = out.wasm.allocator.alloc<Function>();
157+
auto* func = new Function;
158158
func->name = start;
159159
out.wasm.addFunction(func);
160160
exportFunction(start, true);
@@ -254,7 +254,7 @@ void Linker::emscriptenGlue(std::ostream& o) {
254254
// add import, if necessary
255255
if (allSigs.count(sig) == 0) {
256256
allSigs.insert(sig);
257-
auto import = parent->out.wasm.allocator.alloc<Import>();
257+
auto import = new Import;
258258
import->name = import->base = curr->target;
259259
import->module = ENV;
260260
import->type = ensureFunctionType(getSig(curr), &parent->out.wasm);

src/wasm-linker.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ class Linker {
246246
return;
247247
}
248248
if (out.wasm.checkExport(name)) return; // Already exported
249-
auto exp = out.wasm.allocator.alloc<Export>();
249+
auto exp = new Export;
250250
exp->name = exp->value = name;
251251
out.wasm.addExport(exp);
252252
}

0 commit comments

Comments
 (0)