From 2b67b6b1da823f07fca182671e8f7a500e32013f Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Sat, 15 Jun 2019 12:29:59 +0200 Subject: [PATCH] compact the local variable table if there multiple of the same type --- .../binary/BinaryModuleWriter.java | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java index efec876..b1ad3ac 100644 --- a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java @@ -583,15 +583,30 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod */ @Override protected void writeMethodFinish() throws IOException { - WasmOutputStream localsStream = new WasmOutputStream(); - localsStream.writeVaruint32( locals.size() ); - for( AnyType valueType : locals ) { - localsStream.writeVaruint32( 1 ); // TODO optimize, write the count of same types. - localsStream.writeRefValueType( valueType ); + @SuppressWarnings( "resource" ) + WasmOutputStream localsTypeStream = new WasmOutputStream(); + int localEntryCount = 0; // number of local entries in output + int varCount = locals.size(); + for( int i = 0; i < varCount; ) { + AnyType valueType = locals.get( i++ ); + int count = 1; // number of local variables of the same type + while( i < varCount && locals.get( i ) == valueType ) { + count++; + i++; + } + localsTypeStream.writeVaruint32( count ); + localsTypeStream.writeRefValueType( valueType ); + localEntryCount++; } + + @SuppressWarnings( "resource" ) + WasmOutputStream localsStream = new WasmOutputStream(); + localsStream.writeVaruint32( localEntryCount ); + WasmOutputStream functionsStream = function.functionsStream = new WasmOutputStream(); - functionsStream.writeVaruint32( localsStream.size() + codeStream.size() + 1 ); + functionsStream.writeVaruint32( localsStream.size() + localsTypeStream.size() + codeStream.size() + 1 ); localsStream.writeTo( functionsStream ); + localsTypeStream.writeTo( functionsStream ); function.addCodeOffset( functionsStream.size() ); codeStream.writeTo( functionsStream ); functionsStream.write( END );