Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[loader] Add fast methods for reading typed arrays #794

Merged
merged 20 commits into from
Sep 2, 2019
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion lib/loader/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,50 @@ interface ASUtil {
__getString(ref: number): string;
/** Allocates a new array in the module's memory and returns a reference (pointer) to it. */
__allocArray(id: number, values: number[]): number;
/** Allocates a new Uint8Array in the module's memory and returns a reference (pointer) to it. */
// __allocUint8Array(values: number[]): number;
// /** Allocates a new Int8Array in the module's memory and returns a reference (pointer) to it. */
// __allocInt8Array(values: number[]): number;
// /** Allocates a new Uint16Array in the module's memory and returns a reference (pointer) to it. */
// __allocUint16Array(values: number[]): number;
// /** Allocates a new Int16Array in the module's memory and returns a reference (pointer) to it. */
// __allocInt16Array(values: number[]): number;
// /** Allocates a new Uint32Array in the module's memory and returns a reference (pointer) to it. */
// __allocUint32Array(values: number[]): number;
// /** Allocates a new Int32Array in the module's memory and returns a reference (pointer) to it. */
// __allocInt32Array(values: number[]): number;
// /** Allocates a new Float32Array in the module's memory and returns a reference (pointer) to it. */
// __allocFloat32Array(values: number[]): number;
// /** Allocates a new Float64Array in the module's memory and returns a reference (pointer) to it. */
// __allocFloat64Array(values: number[]): number;
/** Reads (copies) the values of an array from the module's memory. */
__getArray(ref: number): number[];
/** Gets a view on the values of an array in the module's memory. */
__getArrayView(ref: number): TypedArray;
/** Reads (copies) the values of Uint8Array from the module's memory. */
__getUint8Array(ref: number): Uint8Array;
/** Reads (copies) the values of Int8Array from the module's memory. */
__getInt8Array(ref: number): Int8Array;
/** Reads (copies) the values of Uint16Array from the module's memory. */
__getUint16Array(ref: number): Uint16Array;
/** Reads (copies) the values of Int16Array from the module's memory. */
__getInt16Array(ref: number): Int16Array;
/** Reads (copies) the values of Uint32Array from the module's memory. */
__getUint32Array(ref: number): Uint32Array;
/** Reads (copies) the values of Int32Array from the module's memory. */
__getInt32Array(ref: number): Int32Array;
/** Reads (copies) the values of Float32Array from the module's memory. */
__getFloat32Array(ref: number): Float32Array;
/** Reads (copies) the values of Float64Array from the module's memory. */
__getFloat64Array(ref: number): Float64Array;
/** Retains a reference externally, making sure that it doesn't become collected prematurely. Returns the reference. */
__retain(ref: number): number;
/** Releases a previously retained reference to an object, allowing the runtime to collect it once its reference count reaches zero. */
__release(ref: number): void;
/** Allocates an instance of the class represented by the specified id. */
__alloc(size: number, id: number): number;
/** Tests whether an object is an instance of the class represented by the specified base id. */
__instanceof(ref: number, baseId: number): boolean;
__instanceof(ref: number, baseId: number): boolean;
/** Forces a cycle collection. Only relevant if objects potentially forming reference cycles are used. */
__collect(): void;
}
Expand Down
216 changes: 207 additions & 9 deletions lib/loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const SIZE_OFFSET = -4;
const ARRAYBUFFER_ID = 0;
const STRING_ID = 1;
const ARRAYBUFFERVIEW_ID = 2;
// const INT8ARRAY_ID = 3; // non-static
// const INT16ARRAY_ID = 4; // non-static

// Runtime type information
const ARRAYBUFFERVIEW = 1 << 0;
Expand All @@ -24,6 +26,7 @@ const KEY_SIGNED = 1 << 19;
const KEY_FLOAT = 1 << 20;
const KEY_NULLABLE = 1 << 21;
const KEY_MANAGED = 1 << 22;
const VAL_ALIGN_OFFSET = 5;

// Array(BufferView) layout
const ARRAYBUFFERVIEW_BUFFER_OFFSET = 0;
Expand Down Expand Up @@ -109,6 +112,7 @@ function postInstantiate(baseModule, instance) {
F64 = new Float64Array(buffer);
}
}

checkMem();

/** Gets the runtime type info for the given id. */
Expand All @@ -126,15 +130,17 @@ function postInstantiate(baseModule, instance) {
}

/** Gets the runtime alignment of a collection's values or keys. */
function getAlign(which, info) {
return 31 - Math.clz32((info / which) & 31); // -1 if none
function getAlign(info) {
return 31 - Math.clz32((info >> VAL_ALIGN_OFFSET) & 31); // -1 if none
}

/** Allocates a new string in the module's memory and returns its retained pointer. */
function __allocString(str) {
const length = str.length;
const ref = alloc(length << 1, STRING_ID);
checkMem();
if (U16.buffer !== memory.buffer) {
U16 = new Uint16Array(memory.buffer);
}
for (let i = 0, j = ref >>> 1; i < length; ++i) U16[j + i] = str.charCodeAt(i);
return ref;
}
Expand All @@ -143,7 +149,11 @@ function postInstantiate(baseModule, instance) {

/** Reads a string from the module's memory by its pointer. */
function __getString(ref) {
checkMem();
const buf = memory.buffer;
if (buffer !== buf) {
U16 = new Uint16Array(buf);
U32 = new Uint32Array(buf);
}
const id = U32[ref + ID_OFFSET >>> 2];
if (id !== STRING_ID) throw Error("not a string: " + ref);
return getStringImpl(U32, U16, ref);
Expand Down Expand Up @@ -173,7 +183,7 @@ function postInstantiate(baseModule, instance) {
function __allocArray(id, values) {
const info = getInfo(id);
if (!(info & (ARRAYBUFFERVIEW | ARRAY))) throw Error("not an array: " + id + " @ " + info);
const align = getAlign(VAL_ALIGN, info);
const align = getAlign(info);
const length = values.length;
const buf = alloc(length << align, ARRAYBUFFER_ID);
const arr = alloc(info & ARRAY ? ARRAY_SIZE : ARRAYBUFFERVIEW_SIZE, id);
Expand All @@ -183,20 +193,97 @@ function postInstantiate(baseModule, instance) {
U32[arr + ARRAYBUFFERVIEW_DATALENGTH_OFFSET >>> 2] = length << align;
if (info & ARRAY) U32[arr + ARRAY_LENGTH_OFFSET >>> 2] = length;
const view = getView(align, info & VAL_SIGNED, info & VAL_FLOAT);
for (let i = 0; i < length; ++i) view[(buf >> align) + i] = values[i];
if (info & VAL_MANAGED) for (let i = 0; i < length; ++i) retain(values[i]);
if (info & VAL_MANAGED) {
for (let i = 0; i < length; ++i) view[(buf >>> align) + i] = retain(values[i]);
} else {
view.set(values, buf >>> align);
}
return arr;
}

baseModule.__allocArray = __allocArray;

/*
function __allocUint8Array(values) {
const length = values.length;
const buf = alloc(length, ARRAYBUFFER_ID);
const arr = alloc(ARRAYBUFFERVIEW_SIZE, INT8ARRAY_ID);
const buffer = memory.buffer;
if (U32.buffer !== buffer) {
U8 = new Uint8Array(buffer);
U32 = new Uint32Array(buffer);
}
U32[arr + ARRAYBUFFERVIEW_BUFFER_OFFSET >>> 2] = retain(buf);
U32[arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2] = buf;
U32[arr + ARRAYBUFFERVIEW_DATALENGTH_OFFSET >>> 2] = length;
U8.set(values, buf);
return arr;
}

baseModule.__allocUint8Array = __allocUint8Array;

function __allocInt8Array(values) {
const bytes = values.length;
const buf = alloc(bytes, ARRAYBUFFER_ID);
const arr = alloc(ARRAYBUFFERVIEW_SIZE, INT8ARRAY_ID);
const buffer = memory.buffer;
if (U32.buffer !== buffer) {
I8 = new Int8Array(buffer);
U32 = new Uint32Array(buffer);
}
U32[arr + ARRAYBUFFERVIEW_BUFFER_OFFSET >>> 2] = retain(buf);
U32[arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2] = buf;
U32[arr + ARRAYBUFFERVIEW_DATALENGTH_OFFSET >>> 2] = bytes;
I8.set(values, buf);
return arr;
}

baseModule.__allocInt8Array = __allocInt8Array;

function __allocUint16Array(values) {
const bytes = values.length << 1;
const buf = alloc(bytes, ARRAYBUFFER_ID);
const arr = alloc(ARRAYBUFFERVIEW_SIZE, INT16ARRAY_ID);
const buffer = memory.buffer;
if (U32.buffer !== buffer) {
U16 = new Uint16Array(buffer);
U32 = new Uint32Array(buffer);
}
U32[arr + ARRAYBUFFERVIEW_BUFFER_OFFSET >>> 2] = retain(buf);
U32[arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2] = buf;
U32[arr + ARRAYBUFFERVIEW_DATALENGTH_OFFSET >>> 2] = bytes;
U16.set(values, buf >>> 1);
return arr;
}

baseModule.__allocUint16Array = __allocUint16Array;

function __allocInt16Array(values) {
const bytes = values.length << 1;
const buf = alloc(bytes, ARRAYBUFFER_ID);
const arr = alloc(ARRAYBUFFERVIEW_SIZE, INT16ARRAY_ID);
const buffer = memory.buffer;
if (U32.buffer !== buffer) {
I16 = new Int16Array(buffer);
U32 = new Uint32Array(buffer);
}
U32[arr + ARRAYBUFFERVIEW_BUFFER_OFFSET >>> 2] = retain(buf);
U32[arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2] = buf;
U32[arr + ARRAYBUFFERVIEW_DATALENGTH_OFFSET >>> 2] = bytes;
I16.set(values, buf >>> 1);
return arr;
}

baseModule.__allocInt16Array = __allocInt16Array;
*/

/** Gets a view on the values of an array in the module's memory. */
function __getArrayView(arr) {
checkMem();
const id = U32[arr + ID_OFFSET >>> 2];
const info = getInfo(id);
if (!(info & ARRAYBUFFERVIEW)) throw Error("not an array: " + id);
const align = getAlign(VAL_ALIGN, info);
const align = getAlign(info);
var buf = U32[arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2];
const length = info & ARRAY
? U32[arr + ARRAY_LENGTH_OFFSET >>> 2]
Expand All @@ -214,6 +301,117 @@ function postInstantiate(baseModule, instance) {

baseModule.__getArray = __getArray;

function __getUint8Array(arr) {
// check memory
const buffer = memory.buffer;
if (U32.buffer !== buffer) {
U8 = new Uint8Array(buffer);
U32 = new Uint32Array(buffer);
}
const buf = U32[arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2];
const length = U32[buf + SIZE_OFFSET >>> 2];
return U8.slice(buf, buf + length);
MaxGraey marked this conversation as resolved.
Show resolved Hide resolved
}

baseModule.__getUint8Array = __getUint8Array;

function __getInt8Array(arr) {
// check memory
const buffer = memory.buffer;
if (U32.buffer !== buffer) {
I8 = new Int8Array(buffer);
U32 = new Uint32Array(buffer);
}
const buf = U32[arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2];
const length = U32[buf + SIZE_OFFSET >>> 2];
return I8.slice(buf, buf + length);
}

baseModule.__getInt8Array = __getInt8Array;

function __getUint16Array(arr) {
// check memory
const buffer = memory.buffer;
if (U32.buffer !== buffer) {
U16 = new Uint16Array(buffer);
U32 = new Uint32Array(buffer);
}
const buf = U32[arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2];
const length = U32[buf + SIZE_OFFSET >>> 2];
return U16.slice(buf >> 1, buf + length >> 1);
}

baseModule.__getUint16Array = __getUint16Array;

function __getInt16Array(arr) {
// check memory
const buffer = memory.buffer;
if (U32.buffer !== buffer) {
I16 = new Int16Array(buffer);
U32 = new Uint32Array(buffer);
}
const buf = U32[arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2];
const length = U32[buf + SIZE_OFFSET >>> 2];
return I16.slice(buf >> 1, buf + length >> 1);
}

baseModule.__getInt16Array = __getInt16Array;

function __getUint32Array(arr) {
// check memory
const buffer = memory.buffer;
if (U32.buffer !== buffer) {
U32 = new Uint32Array(buffer);
}
const buf = U32[arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2];
const length = U32[buf + SIZE_OFFSET >>> 2];
return U32.slice(buf >> 2, buf + length >> 2);
}

baseModule.__getUint32Array = __getUint32Array;

function __getInt32Array(arr) {
// check memory
const buffer = memory.buffer;
if (U32.buffer !== buffer) {
I32 = new Int32Array(buffer);
U32 = new Uint32Array(buffer);
}
const buf = U32[arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2];
const length = U32[buf + SIZE_OFFSET >>> 2];
return I32.slice(buf >> 2, buf + length >> 2);
}

baseModule.__getInt32Array = __getInt32Array;

function __getFloat32Array(arr) {
// check memory
const buffer = memory.buffer;
if (U32.buffer !== buffer) {
F32 = new Float32Array(buffer);
U32 = new Uint32Array(buffer);
}
const buf = U32[arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2];
const length = U32[buf + SIZE_OFFSET >>> 2];
return F32.slice(buf >> 2, buf + length >> 2);
}

baseModule.__getFloat32Array = __getFloat32Array;

function __getFloat64Array(arr) {
// check memory
const buffer = memory.buffer;
if (U32.buffer !== buffer) {
F64 = new Float64Array(buffer);
U32 = new Uint32Array(buffer);
}
const buf = U32[arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2];
const length = U32[buf + SIZE_OFFSET >>> 2];
return F64.slice(buf >> 3, buf + length >> 3);
}

baseModule.__getFloat64Array = __getFloat64Array;

/** Tests whether an object is an instance of the class represented by the specified base id. */
function __instanceof(ref, baseId) {
var id = U32[(ref + ID_OFFSET) >>> 2];
Expand All @@ -228,7 +426,7 @@ function postInstantiate(baseModule, instance) {

// Pull basic exports to baseModule so code in preInstantiate can use them
baseModule.memory = baseModule.memory || memory;
baseModule.table = baseModule.table || table;
baseModule.table = baseModule.table || table;

// Demangle exports and provide the usual utility on the prototype
return demangle(rawExports, Object.defineProperties(baseModule, {
Expand Down
3 changes: 3 additions & 0 deletions lib/loader/tests/assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ export function dotrace(num: f64): void {
trace("The answer is", 1, num);
}

export const UINT8ARRAY_ID = idof<Uint8Array>();
export const INT16ARRAY_ID = idof<Int16Array>();
export const UINT16ARRAY_ID = idof<Uint16Array>();
export const INT32ARRAY_ID = idof<Int32Array>();
export const UINT32ARRAY_ID = idof<Uint32Array>();
export const FLOAT32ARRAY_ID = idof<Float32Array>();
Expand Down
Binary file modified lib/loader/tests/build/untouched.wasm
Binary file not shown.
Loading