From f81d047b8bf89ea5868022c01e3b0ac1c08a0bc1 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Wed, 15 Nov 2023 13:21:08 -0800 Subject: [PATCH] Remove types array proxy for memories > 4G The upstream issue with chrome was fixed so this proxy should no longer be needed. --- .circleci/config.yml | 2 +- src/preamble.js | 4 - src/preamble_minimal.js | 4 - src/runtime_view_proxy.js | 156 -------------------------------------- 4 files changed, 1 insertion(+), 165 deletions(-) delete mode 100644 src/runtime_view_proxy.js diff --git a/.circleci/config.yml b/.circleci/config.yml index 861e547160162..bbfdd77e31f22 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -112,7 +112,7 @@ commands: description: "install canary version of node" steps: - install-node-version: - node_version: "21.0.0-v8-canary202309143a48826a08" + node_version: "22.0.0-v8-canary20231108ff311d5a39" canary: true install-v8: description: "install v8 using jsvu" diff --git a/src/preamble.js b/src/preamble.js index 7c534455fd150..dcc6479ca9c9c 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -148,14 +148,10 @@ function updateMemoryViews() { #if SUPPORT_BIG_ENDIAN Module['HEAP_DATA_VIEW'] = HEAP_DATA_VIEW = new DataView(b); #endif -#if MEMORY64 && MAXIMUM_MEMORY > FOUR_GB -#include "runtime_view_proxy.js" -#else Module['HEAP8'] = HEAP8 = new Int8Array(b); Module['HEAP16'] = HEAP16 = new Int16Array(b); Module['HEAPU8'] = HEAPU8 = new Uint8Array(b); Module['HEAPU16'] = HEAPU16 = new Uint16Array(b); -#endif Module['HEAP32'] = HEAP32 = new Int32Array(b); Module['HEAPU32'] = HEAPU32 = new Uint32Array(b); Module['HEAPF32'] = HEAPF32 = new Float32Array(b); diff --git a/src/preamble_minimal.js b/src/preamble_minimal.js index 77fb0882ac96e..e28a1849c855a 100644 --- a/src/preamble_minimal.js +++ b/src/preamble_minimal.js @@ -73,14 +73,10 @@ function updateMemoryViews() { #if SUPPORT_BIG_ENDIAN {{{ maybeExport('HEAP_DATA_VIEW') }}} HEAP_DATA_VIEW = new DataView(b); #endif -#if MEMORY64 && MAXIMUM_MEMORY > FOUR_GB -#include "runtime_view_proxy.js" -#else {{{ maybeExport('HEAP8') }}} HEAP8 = new Int8Array(b); {{{ maybeExport('HEAP16') }}} HEAP16 = new Int16Array(b); {{{ maybeExport('HEAPU8') }}} HEAPU8 = new Uint8Array(b); {{{ maybeExport('HEAPU16') }}} HEAPU16 = new Uint16Array(b); -#endif {{{ maybeExport('HEAP32') }}} HEAP32 = new Int32Array(b); {{{ maybeExportIfAudioWorklet('HEAPU32') }}} HEAPU32 = new Uint32Array(b); {{{ maybeExportIfAudioWorklet('HEAPF32') }}} HEAPF32 = new Float32Array(b); diff --git a/src/runtime_view_proxy.js b/src/runtime_view_proxy.js deleted file mode 100644 index 51b581de543c0..0000000000000 --- a/src/runtime_view_proxy.js +++ /dev/null @@ -1,156 +0,0 @@ -/** - * @license - * Copyright 2023 The Emscripten Authors - * SPDX-License-Identifier: MIT - */ - -// Chrome does not allow TypedArrays with more than 4294967296 elements -// We'll create proxy objects for HEAP(U)8 when memory is > 4gb and for HEAP(U)16 when > 8gb -// https://bugs.chromium.org/p/v8/issues/detail?id=4153 -var maxArraySize = Math.min(b.byteLength, 4 * 1024 * 1024 * 1024 - 2); -/** - * @param {string} type - Heap type - * @param {number} [offset] - Heap offset - * @param {number} [length] - typed array length - */ -function getHeapBlock(type, offset, length) { - if (!offset) { - offset = 0 - } - - let heap = wasmMemory.buffer - - // we should always limit the length to maxArraySize - function createTypedArray(arrayType, offset, length) { - let bpe = arrayType.BYTES_PER_ELEMENT; - return new arrayType(heap, offset, length || Math.min((heap.byteLength - offset) / bpe, maxArraySize)); - } - - switch (type) { - case 'i1': - case 'i8': - return createTypedArray(Int8Array, offset, length); - case 'u1': - case 'u8': - return createTypedArray(Uint8Array, offset, length); - case 'i16': - return createTypedArray(Int16Array, offset, length); - case 'u16': - return createTypedArray(Uint16Array, offset, length); - default: - throw new Error('Invalid type'); - } -} - -function createProxyHandler(type, heapBlocks) { - let firstHeapBlock = heapBlocks[0] - let bpe = firstHeapBlock.BYTES_PER_ELEMENT - - function getRealStartAndEnd(start, end) { - let startReal = (start || 0) * bpe - let endReal = end ? end * bpe : wasmMemory.buffer.byteLength - return [startReal, endReal] - } - - function copyWithin(target, start, end) { - if (target * bpe >= maxArraySize || start * bpe >= maxArraySize || (end && end * bpe >= maxArraySize)) { - let len = end - start - let targetArray = getHeapBlock(type, target * bpe, len) - let sourceArray = getHeapBlock(type, start * bpe, len) - targetArray.set(sourceArray) - return heapBlocks[0] - } - return heapBlocks[0].copyWithin(target, start, end) - } - - function setOverridden(array, offset) { - let offsetReal = (offset || 0) * bpe - if (offsetReal >= maxArraySize || array.byteLength + offsetReal >= maxArraySize) { - let targetArray = getHeapBlock(type, offsetReal, array.length) - targetArray.set(array) - } else { - firstHeapBlock.set(array, offset) - } - } - - function subarray(start, end) { - let [startReal, endReal] = getRealStartAndEnd(start, end) - if (startReal >= maxArraySize || endReal >= maxArraySize) { - return getHeapBlock(type, startReal, endReal - startReal) - } - return firstHeapBlock.subarray(start, end) - } - - function fill(value, start, end) { - let [startReal, endReal] = getRealStartAndEnd(start, end) - if (startReal >= maxArraySize || endReal >= maxArraySize) { - let hb = getHeapBlock(type, startReal, endReal - startReal) - hb.fill(value, 0, end - start) - return firstHeapBlock - } - return firstHeapBlock.fill(value, start, end) - } - function slice(start, end) { - let [startReal, endReal] = getRealStartAndEnd(start, end) - if (startReal >= maxArraySize || endReal >= maxArraySize) { - let hb = getHeapBlock(type, startReal, endReal - startReal) - return hb.slice(0, end - start) - } - return firstHeapBlock.slice(start, end) - } - - return { - get(target, property) { - if (parseInt(property, 10) == property) { - let memoryOffset = property * bpe - let blockNumber = Math.floor(memoryOffset / maxArraySize) - return heapBlocks[blockNumber][property - blockNumber * maxArraySize] - } - switch (property) { - case 'copyWithin': return copyWithin; - case 'set': return setOverridden; - case 'subarray': return subarray; - case 'fill': return fill; - case 'slice': return slice; - case 'length': return wasmMemory.buffer.byteLength; - } - return firstHeapBlock[property] - }, - set(target, property, value) { - if (parseInt(property, 10) == property) { - let memoryOffset = property * bpe - let blockNumber = Math.floor(memoryOffset / maxArraySize) - heapBlocks[blockNumber][property - blockNumber * maxArraySize] = value - return true - } - - firstHeapBlock[property] = value - return true; - }, - } -} - -function createMemoryProxy(type) { - let heapBlocks = []; - let bpe = type === 'i16' || type === 'u16' ? 2 : 1 - let numberOfBlocks = Math.ceil(b.byteLength / maxArraySize / bpe) - for (let i = 0; i < numberOfBlocks; i++) { - heapBlocks.push(getHeapBlock(type, i * maxArraySize * bpe)) - } - return new Proxy(heapBlocks[0], createProxyHandler(type, heapBlocks)); -} - -if (b.byteLength > maxArraySize) { - Module['HEAP8'] = HEAP8 = createMemoryProxy('i8') - Module['HEAPU8'] = HEAPU8 = createMemoryProxy('u8') -} else { - Module['HEAP8'] = HEAP8 = new Int8Array(b); - Module['HEAPU8'] = HEAPU8 = new Uint8Array(b); -} -if (b.byteLength > maxArraySize * 2) { - Module['HEAP16'] = HEAP16 = createMemoryProxy('i16') - Module['HEAPU16'] = HEAPU16 = createMemoryProxy('u16') -} else { - Module['HEAP16'] = HEAP16 = new Int16Array(b); - Module['HEAPU16'] = HEAPU16 = new Uint16Array(b); -}