optimize the memory section, write only if needed

This commit is contained in:
Volker Berlin 2019-11-02 19:11:18 +01:00
parent 1dd644b67a
commit fac7e68b43

View File

@ -194,17 +194,20 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
* if any I/O error occur * if any I/O error occur
*/ */
private void writeMemorySection() throws IOException { private void writeMemorySection() throws IOException {
int dataSize = dataStream.size();
if( dataSize > 0 ) {
WasmOutputStream stream = new WasmOutputStream(); WasmOutputStream stream = new WasmOutputStream();
int pages = (dataStream.size() + 0xFFFF) / 0x10000; // a page is defined with a size of 64KiB int pages = (dataSize + 0xFFFF) / 0x10000; // a page is defined with a size of 64KiB
int count = 1; int count = 1;
stream.writeVaruint32( count ); stream.writeVaruint32( count );
for( int i = 0; i < count; i++ ) { for( int i = 0; i < count; i++ ) {
stream.writeVaruint32( 1 ); // flags; 1-maximum is available, 0-no maximum value available stream.writeVaruint32( 0 ); // flags; 1-maximum is available, 0-no maximum value available
stream.writeVaruint32( pages ); // initial length stream.writeVaruint32( pages ); // initial length
stream.writeVaruint32( pages ); // maximum length //stream.writeVaruint32( pages ); // maximum length
} }
wasm.writeSection( SectionType.Memory, stream ); wasm.writeSection( SectionType.Memory, stream );
} }
}
/** /**
* Write the event section if needed. * Write the event section if needed.