From 4fa5c6b88e8f06be12de86b18fa651294e67b4a0 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Fri, 15 May 2020 12:03:05 -0400 Subject: [PATCH] Fix uselessly allocating one extra WASM page spotted as a result of stepping in the code. The issue is that a uBP "page size" might differ from a WASM page size, which is always 65536 bytes. --- src/js/hntrie.js | 19 +++++++------------ src/js/strie.js | 15 ++++++--------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/src/js/hntrie.js b/src/js/hntrie.js index 62a24e74b..d32f21141 100644 --- a/src/js/hntrie.js +++ b/src/js/hntrie.js @@ -411,25 +411,20 @@ const HNTrieContainer = class { if ( typeof WebAssembly !== 'object' ) { return false; } if ( this.wasmMemory instanceof WebAssembly.Memory ) { return true; } const module = await getWasmModule(); - if ( module instanceof WebAssembly.Module === false ) { - return false; - } + if ( module instanceof WebAssembly.Module === false ) { return false; } const memory = new WebAssembly.Memory({ initial: 2 }); - const instance = await WebAssembly.instantiate( - module, - { - imports: { - memory, - growBuf: this.growBuf.bind(this, 24, 256) - } + const instance = await WebAssembly.instantiate(module, { + imports: { + memory, + growBuf: this.growBuf.bind(this, 24, 256) } - ); + }); if ( instance instanceof WebAssembly.Instance === false ) { return false; } this.wasmMemory = memory; const curPageCount = memory.buffer.byteLength >>> 16; - const newPageCount = this.buf.byteLength + PAGE_SIZE-1 >>> 16; + const newPageCount = roundToPageSize(this.buf.byteLength) >>> 16; if ( newPageCount > curPageCount ) { memory.grow(newPageCount - curPageCount); } diff --git a/src/js/strie.js b/src/js/strie.js index c084eb1a3..b9b94df22 100644 --- a/src/js/strie.js +++ b/src/js/strie.js @@ -690,22 +690,19 @@ const roundToPageSize = v => (v + PAGE_SIZE-1) & ~(PAGE_SIZE-1); if ( typeof WebAssembly !== 'object' ) { return false; } if ( this.wasmMemory instanceof WebAssembly.Memory ) { return true; } const module = await getWasmModule(); - if ( module instanceof WebAssembly.Module === false ) { - return false; - } + if ( module instanceof WebAssembly.Module === false ) { return false; } const memory = new WebAssembly.Memory({ - initial: this.buf8.length >>> 16 + initial: roundToPageSize(this.buf8.length) >>> 16 + }); + const instance = await WebAssembly.instantiate(module, { + imports: { memory, extraHandler: this.extraHandler } }); - const instance = await WebAssembly.instantiate( - module, - { imports: { memory, extraHandler: this.extraHandler } } - ); if ( instance instanceof WebAssembly.Instance === false ) { return false; } this.wasmMemory = memory; const curPageCount = memory.buffer.byteLength >>> 16; - const newPageCount = this.buf8.byteLength + PAGE_SIZE-1 >>> 16; + const newPageCount = roundToPageSize(this.buf8.byteLength) >>> 16; if ( newPageCount > curPageCount ) { memory.grow(newPageCount - curPageCount); }