Skip to content

Commit 14e0968

Browse files
authored
Use BinaryIndexes instead of copies in BinaryWriter (NFC) (#2161)
1 parent 9d4f053 commit 14e0968

3 files changed

Lines changed: 13 additions & 29 deletions

File tree

src/ir/module-utils.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
#include "ir/find_all.h"
2121
#include "ir/manipulation.h"
22-
#include "wasm-binary.h"
2322
#include "wasm.h"
2423

2524
namespace wasm {

src/wasm-binary.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "asm_v_wasm.h"
2929
#include "asmjs/shared-constants.h"
3030
#include "ir/import-utils.h"
31+
#include "ir/module-utils.h"
3132
#include "parsing.h"
3233
#include "wasm-builder.h"
3334
#include "wasm-traversal.h"
@@ -902,7 +903,7 @@ inline S32LEB binaryType(Type type) {
902903
class WasmBinaryWriter {
903904
public:
904905
WasmBinaryWriter(Module* input, BufferWithRandomAccess& o, bool debug = false)
905-
: wasm(input), o(o), debug(debug) {
906+
: wasm(input), o(o), debug(debug), indexes(*input) {
906907
prepare();
907908
}
908909

@@ -951,13 +952,6 @@ class WasmBinaryWriter {
951952
void writeDataSegments();
952953
void writeEvents();
953954

954-
// name of the Function => index. first imports, then internals
955-
std::unordered_map<Name, Index> mappedFunctions;
956-
// name of the Global => index. first imported globals, then internal globals
957-
std::unordered_map<Name, uint32_t> mappedGlobals;
958-
// name of the Event => index. first imported events, then internal events
959-
std::unordered_map<Name, uint32_t> mappedEvents;
960-
961955
uint32_t getFunctionIndex(Name name);
962956
uint32_t getGlobalIndex(Name name);
963957
uint32_t getEventIndex(Name name);
@@ -1003,6 +997,7 @@ class WasmBinaryWriter {
1003997
Module* wasm;
1004998
BufferWithRandomAccess& o;
1005999
bool debug;
1000+
ModuleUtils::BinaryIndexes indexes;
10061001

10071002
bool debugInfo = true;
10081003
std::ostream* sourceMap = nullptr;

src/wasm/wasm-binary.cpp

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <algorithm>
1818
#include <fstream>
1919

20-
#include "ir/module-utils.h"
2120
#include "support/bits.h"
2221
#include "wasm-binary.h"
2322
#include "wasm-stack.h"
@@ -34,11 +33,6 @@ void WasmBinaryWriter::prepare() {
3433
// https://github.com/WebAssembly/spec/pull/301 might want this:
3534
// assert(!func->type.isNull());
3635
}
37-
ModuleUtils::BinaryIndexes indexes(*wasm);
38-
mappedFunctions = std::move(indexes.functionIndexes);
39-
mappedGlobals = std::move(indexes.globalIndexes);
40-
mappedEvents = std::move(indexes.eventIndexes);
41-
4236
importInfo = wasm::make_unique<ImportInfo>(*wasm);
4337
}
4438

@@ -458,18 +452,18 @@ void WasmBinaryWriter::writeDataSegments() {
458452
}
459453

460454
uint32_t WasmBinaryWriter::getFunctionIndex(Name name) {
461-
assert(mappedFunctions.count(name));
462-
return mappedFunctions[name];
455+
assert(indexes.functionIndexes.count(name));
456+
return indexes.functionIndexes[name];
463457
}
464458

465459
uint32_t WasmBinaryWriter::getGlobalIndex(Name name) {
466-
assert(mappedGlobals.count(name));
467-
return mappedGlobals[name];
460+
assert(indexes.globalIndexes.count(name));
461+
return indexes.globalIndexes[name];
468462
}
469463

470464
uint32_t WasmBinaryWriter::getEventIndex(Name name) {
471-
assert(mappedEvents.count(name));
472-
return mappedEvents[name];
465+
assert(indexes.eventIndexes.count(name));
466+
return indexes.eventIndexes[name];
473467
}
474468

475469
void WasmBinaryWriter::writeFunctionTableDeclaration() {
@@ -534,22 +528,18 @@ void WasmBinaryWriter::writeEvents() {
534528
}
535529

536530
void WasmBinaryWriter::writeNames() {
537-
bool hasContents = false;
538-
if (wasm->functions.size() > 0) {
539-
hasContents = true;
540-
getFunctionIndex(wasm->functions[0]->name); // generate mappedFunctions
541-
}
542-
if (!hasContents) {
531+
if (wasm->functions.empty()) {
543532
return;
544533
}
534+
545535
if (debug) {
546536
std::cerr << "== writeNames" << std::endl;
547537
}
548538
auto start = startSection(BinaryConsts::Section::User);
549539
writeInlineString(BinaryConsts::UserSections::Name);
550540
auto substart =
551541
startSubsection(BinaryConsts::UserSections::Subsection::NameFunction);
552-
o << U32LEB(mappedFunctions.size());
542+
o << U32LEB(indexes.functionIndexes.size());
553543
Index emitted = 0;
554544
auto add = [&](Function* curr) {
555545
o << U32LEB(emitted);
@@ -558,7 +548,7 @@ void WasmBinaryWriter::writeNames() {
558548
};
559549
ModuleUtils::iterImportedFunctions(*wasm, add);
560550
ModuleUtils::iterDefinedFunctions(*wasm, add);
561-
assert(emitted == mappedFunctions.size());
551+
assert(emitted == indexes.functionIndexes.size());
562552
finishSubsection(substart);
563553
/* TODO: locals */
564554
finishSection(start);

0 commit comments

Comments
 (0)