Skip to content

Commit

Permalink
perf(ext/ffi): Fast UnsafePointerView read functions (denoland#16351)
Browse files Browse the repository at this point in the history
This PR makes pointer read methods of `Deno.UnsafePointerView` Fast API
compliant, with the exception of `getCString` which cannot be made fast
with current V8 Fast API.
  • Loading branch information
aapoalas committed Oct 20, 2022
1 parent 973069b commit 722ea20
Show file tree
Hide file tree
Showing 15 changed files with 305 additions and 102 deletions.
2 changes: 1 addition & 1 deletion cli/tests/testdata/run/ffi/unstable_ffi_10.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Deno.core.ops.op_ffi_read_i16(0n);
Deno.core.ops.op_ffi_read_i16(0n, 0);
2 changes: 1 addition & 1 deletion cli/tests/testdata/run/ffi/unstable_ffi_11.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Deno.core.ops.op_ffi_read_u32(0n);
Deno.core.ops.op_ffi_read_u32(0n, 0);
2 changes: 1 addition & 1 deletion cli/tests/testdata/run/ffi/unstable_ffi_12.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Deno.core.ops.op_ffi_read_i32(0n);
Deno.core.ops.op_ffi_read_i32(0n, 0);
2 changes: 1 addition & 1 deletion cli/tests/testdata/run/ffi/unstable_ffi_13.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Deno.core.ops.op_ffi_read_u64(0n);
Deno.core.ops.op_ffi_read_u64(0n, 0, new Uint32Array(2));
2 changes: 1 addition & 1 deletion cli/tests/testdata/run/ffi/unstable_ffi_14.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Deno.core.ops.op_ffi_read_f32(0n);
Deno.core.ops.op_ffi_read_f32(0n, 0);
2 changes: 1 addition & 1 deletion cli/tests/testdata/run/ffi/unstable_ffi_15.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Deno.core.ops.op_ffi_read_f64(0n);
Deno.core.ops.op_ffi_read_f64(0n, 0);
2 changes: 1 addition & 1 deletion cli/tests/testdata/run/ffi/unstable_ffi_5.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Deno.core.ops.op_ffi_buf_copy_into(0n, new Uint8Array(0), 0);
Deno.core.ops.op_ffi_buf_copy_into(0n, 0, new Uint8Array(0), 0);
2 changes: 1 addition & 1 deletion cli/tests/testdata/run/ffi/unstable_ffi_6.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Deno.core.ops.op_ffi_cstr_read(0n);
Deno.core.ops.op_ffi_cstr_read(0n, 0);
2 changes: 1 addition & 1 deletion cli/tests/testdata/run/ffi/unstable_ffi_7.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Deno.core.ops.op_ffi_read_u8(0n);
Deno.core.ops.op_ffi_read_u8(0n, 0);
2 changes: 1 addition & 1 deletion cli/tests/testdata/run/ffi/unstable_ffi_8.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Deno.core.ops.op_ffi_read_i8(0n);
Deno.core.ops.op_ffi_read_i8(0n, 0);
2 changes: 1 addition & 1 deletion cli/tests/testdata/run/ffi/unstable_ffi_9.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Deno.core.ops.op_ffi_read_u16(0n);
Deno.core.ops.op_ffi_read_u16(0n, 0);
64 changes: 44 additions & 20 deletions ext/ffi/00_ffi.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
const ops = core.ops;
const __bootstrap = window.__bootstrap;
const {
BigInt,
ObjectDefineProperty,
ArrayPrototypeMap,
Number,
Expand All @@ -17,9 +16,13 @@
Int32Array,
Uint32Array,
BigInt64Array,
BigUint64Array,
Function,
} = window.__bootstrap.primordials;

const U32_BUFFER = new Uint32Array(2);
const U64_BUFFER = new BigUint64Array(U32_BUFFER.buffer);
const I64_BUFFER = new BigInt64Array(U32_BUFFER.buffer);
class UnsafePointerView {
pointer;

Expand All @@ -29,107 +32,128 @@

getBool(offset = 0) {
return ops.op_ffi_read_bool(
offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
this.pointer,
offset,
);
}

getUint8(offset = 0) {
return ops.op_ffi_read_u8(
offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
this.pointer,
offset,
);
}

getInt8(offset = 0) {
return ops.op_ffi_read_i8(
offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
this.pointer,
offset,
);
}

getUint16(offset = 0) {
return ops.op_ffi_read_u16(
offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
this.pointer,
offset,
);
}

getInt16(offset = 0) {
return ops.op_ffi_read_i16(
offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
this.pointer,
offset,
);
}

getUint32(offset = 0) {
return ops.op_ffi_read_u32(
offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
this.pointer,
offset,
);
}

getInt32(offset = 0) {
return ops.op_ffi_read_i32(
offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
this.pointer,
offset,
);
}

getBigUint64(offset = 0) {
return ops.op_ffi_read_u64(
offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
ops.op_ffi_read_u64(
this.pointer,
offset,
U32_BUFFER,
);
return U64_BUFFER[0];
}

getBigInt64(offset = 0) {
return ops.op_ffi_read_i64(
offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
ops.op_ffi_read_i64(
this.pointer,
offset,
U32_BUFFER,
);
return I64_BUFFER[0];
}

getFloat32(offset = 0) {
return ops.op_ffi_read_f32(
offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
this.pointer,
offset,
);
}

getFloat64(offset = 0) {
return ops.op_ffi_read_f64(
offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
this.pointer,
offset,
);
}

getCString(offset = 0) {
return ops.op_ffi_cstr_read(
offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
this.pointer,
offset,
);
}

static getCString(pointer, offset = 0) {
return ops.op_ffi_cstr_read(
offset ? BigInt(pointer) + BigInt(offset) : pointer,
pointer,
offset,
);
}

getArrayBuffer(byteLength, offset = 0) {
return ops.op_ffi_get_buf(
offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
this.pointer,
offset,
byteLength,
);
}

static getArrayBuffer(pointer, byteLength, offset = 0) {
return ops.op_ffi_get_buf(
offset ? BigInt(pointer) + BigInt(offset) : pointer,
pointer,
offset,
byteLength,
);
}

copyInto(destination, offset = 0) {
ops.op_ffi_buf_copy_into(
offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
this.pointer,
offset,
destination,
destination.byteLength,
);
}

static copyInto(pointer, destination, offset = 0) {
ops.op_ffi_buf_copy_into(
offset ? BigInt(pointer) + BigInt(offset) : pointer,
pointer,
offset,
destination,
destination.byteLength,
);
Expand Down
Loading

0 comments on commit 722ea20

Please sign in to comment.