Skip to content

Commit

Permalink
Faster detached buffer checks for DataView
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Jul 29, 2024
1 parent 18161a7 commit 5f93ead
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 59 deletions.
35 changes: 15 additions & 20 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1766,47 +1766,47 @@ impl<'a> Context<'a> {
}

fn expose_int8_memory(&mut self, memory: MemoryId) -> MemView {
self.memview("Int8", memory)
self.memview("Int8Array", memory)
}

fn expose_uint8_memory(&mut self, memory: MemoryId) -> MemView {
self.memview("Uint8", memory)
self.memview("Uint8Array", memory)
}

fn expose_clamped_uint8_memory(&mut self, memory: MemoryId) -> MemView {
self.memview("Uint8Clamped", memory)
self.memview("Uint8ClampedArray", memory)
}

fn expose_int16_memory(&mut self, memory: MemoryId) -> MemView {
self.memview("Int16", memory)
self.memview("Int16Array", memory)
}

fn expose_uint16_memory(&mut self, memory: MemoryId) -> MemView {
self.memview("Uint16", memory)
self.memview("Uint16Array", memory)
}

fn expose_int32_memory(&mut self, memory: MemoryId) -> MemView {
self.memview("Int32", memory)
self.memview("Int32Array", memory)
}

fn expose_uint32_memory(&mut self, memory: MemoryId) -> MemView {
self.memview("Uint32", memory)
self.memview("Uint32Array", memory)
}

fn expose_int64_memory(&mut self, memory: MemoryId) -> MemView {
self.memview("BigInt64", memory)
self.memview("BigInt64Array", memory)
}

fn expose_uint64_memory(&mut self, memory: MemoryId) -> MemView {
self.memview("BigUint64", memory)
self.memview("BigUint64Array", memory)
}

fn expose_f32_memory(&mut self, memory: MemoryId) -> MemView {
self.memview("Float32", memory)
self.memview("Float32Array", memory)
}

fn expose_f64_memory(&mut self, memory: MemoryId) -> MemView {
self.memview("Float64", memory)
self.memview("Float64Array", memory)
}

fn expose_dataview_memory(&mut self, memory: MemoryId) -> MemView {
Expand All @@ -1820,10 +1820,8 @@ impl<'a> Context<'a> {
}
let mem = self.export_name_of(memory);

let is_dataview = kind == "DataView";

let cache = format!("cached{}Memory{}", kind, view.num);
let resized_check = if self.module.memories.get(memory).shared || is_dataview {
let resized_check = if self.module.memories.get(memory).shared {
// When it's backed by a `SharedArrayBuffer`, growing the wasm module's memory
// doesn't detach old references; instead, it just leaves them pointing to a
// slice of the up-to-date memory. So in order to check if it's been grown, we
Expand All @@ -1833,6 +1831,9 @@ impl<'a> Context<'a> {
cache = cache,
mem = mem
)
} else if kind == "DataView" {
// `DataView`s throw when accessing detached memory, including `byteLength`.
format!("{cache}.buffer.detached === true", cache = cache)
} else {
// Otherwise, we can do a quicker check of whether the buffer's been detached,
// which is indicated by a length of 0.
Expand All @@ -1841,12 +1842,6 @@ impl<'a> Context<'a> {

self.global(&format!("let {cache} = null;\n"));

let kind = if is_dataview {
kind
} else {
&format!("{}Array", kind)
};

self.global(&format!(
"
function {name}() {{
Expand Down
12 changes: 6 additions & 6 deletions crates/cli/tests/reference/anyref-import-catch.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true

cachedTextDecoder.decode();

let cachedUint8Memory0 = null;
let cachedUint8ArrayMemory0 = null;

function getUint8Memory0() {
if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) {
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
function getUint8ArrayMemory0() {
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8Memory0;
return cachedUint8ArrayMemory0;
}

function getStringFromWasm0(ptr, len) {
ptr = ptr >>> 0;
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
}

function addToExternrefTable0(obj) {
Expand Down
12 changes: 6 additions & 6 deletions crates/cli/tests/reference/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true

cachedTextDecoder.decode();

let cachedUint8Memory0 = null;
let cachedUint8ArrayMemory0 = null;

function getUint8Memory0() {
if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) {
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
function getUint8ArrayMemory0() {
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8Memory0;
return cachedUint8ArrayMemory0;
}

function getStringFromWasm0(ptr, len) {
ptr = ptr >>> 0;
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
}

const ClassBuilderFinalization = (typeof FinalizationRegistry === 'undefined')
Expand Down
12 changes: 6 additions & 6 deletions crates/cli/tests/reference/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true

cachedTextDecoder.decode();

let cachedUint8Memory0 = null;
let cachedUint8ArrayMemory0 = null;

function getUint8Memory0() {
if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) {
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
function getUint8ArrayMemory0() {
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8Memory0;
return cachedUint8ArrayMemory0;
}

function getStringFromWasm0(ptr, len) {
ptr = ptr >>> 0;
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
}

const ClassConstructorFinalization = (typeof FinalizationRegistry === 'undefined')
Expand Down
12 changes: 6 additions & 6 deletions crates/cli/tests/reference/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true

cachedTextDecoder.decode();

let cachedUint8Memory0 = null;
let cachedUint8ArrayMemory0 = null;

function getUint8Memory0() {
if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) {
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
function getUint8ArrayMemory0() {
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8Memory0;
return cachedUint8ArrayMemory0;
}

function getStringFromWasm0(ptr, len) {
ptr = ptr >>> 0;
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
}
/**
* @param {Color} color
Expand Down
12 changes: 6 additions & 6 deletions crates/cli/tests/reference/result-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,18 @@ let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true

cachedTextDecoder.decode();

let cachedUint8Memory0 = null;
let cachedUint8ArrayMemory0 = null;

function getUint8Memory0() {
if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) {
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
function getUint8ArrayMemory0() {
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8Memory0;
return cachedUint8ArrayMemory0;
}

function getStringFromWasm0(ptr, len) {
ptr = ptr >>> 0;
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
}
/**
* @returns {string}
Expand Down
18 changes: 9 additions & 9 deletions crates/cli/tests/reference/string-arg.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true

cachedTextDecoder.decode();

let cachedUint8Memory0 = null;
let cachedUint8ArrayMemory0 = null;

function getUint8Memory0() {
if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) {
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
function getUint8ArrayMemory0() {
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8Memory0;
return cachedUint8ArrayMemory0;
}

function getStringFromWasm0(ptr, len) {
ptr = ptr >>> 0;
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
}

let WASM_VECTOR_LEN = 0;
Expand All @@ -48,15 +48,15 @@ function passStringToWasm0(arg, malloc, realloc) {
if (realloc === undefined) {
const buf = cachedTextEncoder.encode(arg);
const ptr = malloc(buf.length, 1) >>> 0;
getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf);
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
WASM_VECTOR_LEN = buf.length;
return ptr;
}

let len = arg.length;
let ptr = malloc(len, 1) >>> 0;

const mem = getUint8Memory0();
const mem = getUint8ArrayMemory0();

let offset = 0;

Expand All @@ -71,7 +71,7 @@ function passStringToWasm0(arg, malloc, realloc) {
arg = arg.slice(offset);
}
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
const view = getUint8Memory0().subarray(ptr + offset, ptr + len);
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
const ret = encodeString(arg, view);

offset += ret.written;
Expand Down

0 comments on commit 5f93ead

Please sign in to comment.