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 5 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
18 changes: 17 additions & 1 deletion lib/loader/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,30 @@ interface ASUtil {
__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
122 changes: 118 additions & 4 deletions lib/loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ function postInstantiate(baseModule, instance) {
F64 = new Float64Array(buffer);
}
}

checkMem();

/** Gets the runtime type info for the given id. */
Expand All @@ -134,7 +135,9 @@ function postInstantiate(baseModule, instance) {
function __allocString(str) {
const length = str.length;
const ref = alloc(length << 1, STRING_ID);
checkMem();
if (U16.buffer !== memory.buffer) {
U16 = new Uint16Array(buffer);
}
for (let i = 0, j = ref >>> 1; i < length; ++i) U16[j + i] = str.charCodeAt(i);
return ref;
}
Expand All @@ -143,7 +146,11 @@ function postInstantiate(baseModule, instance) {

/** Reads a string from the module's memory by its pointer. */
function __getString(ref) {
checkMem();
if (buffer !== memory.buffer) {
buffer = memory.buffer;
U16 = new Uint16Array(buffer);
U32 = new Uint32Array(buffer);
}
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 @@ -183,8 +190,11 @@ 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 {
for (let i = 0; i < length; ++i) view[(buf >> align) + i] = values[i];
}
return arr;
}

Expand Down Expand Up @@ -214,6 +224,110 @@ function postInstantiate(baseModule, instance) {

baseModule.__getArray = __getArray;

function __getUint8Array(arr) {
// check memory
if (U32.buffer !== memory.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
if (U32.buffer !== memory.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
if (U32.buffer !== memory.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
if (U32.buffer !== memory.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
if (U32.buffer !== memory.buffer) {
U32 = new Uint32Array(buffer);
MaxGraey marked this conversation as resolved.
Show resolved Hide resolved
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
if (U32.buffer !== memory.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
if (U32.buffer !== memory.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
if (U32.buffer !== memory.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 Down
2 changes: 2 additions & 0 deletions lib/loader/tests/assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ 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 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.
22 changes: 22 additions & 0 deletions lib/loader/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,32 @@ assert.strictEqual(module.__getString(module.COLOR), "red");
try { module.__release(ref); assert(false); } catch (e) {};
}

// should be able to distinguish between signed and unsigned
{
let arr = new Uint8Array([0, 255, 127]);
let ref = module.__retain(module.__allocArray(module.UINT8ARRAY_ID, arr));
assert(module.__instanceof(ref, module.UINT8ARRAY_ID));
assert.deepEqual(module.__getUint8Array(ref), arr);
module.__release(ref);
try { module.__release(ref); assert(false); } catch (e) {};
}

// should be able to distinguish between signed and unsigned
{
let arr = new Int16Array([0, 0xFFFF, -0x00FF]);
let ref = module.__retain(module.__allocArray(module.INT16ARRAY_ID, arr));
assert(module.__instanceof(ref, module.INT16ARRAY_ID));
assert.deepEqual(module.__getInt16Array(ref), arr);
module.__release(ref);
try { module.__release(ref); assert(false); } catch (e) {};
}

// should be able to distinguish between signed and unsigned
{
let arr = [1, -1 >>> 0, 0x80000000];
let ref = module.__retain(module.__allocArray(module.UINT32ARRAY_ID, arr));
assert(module.__instanceof(ref, module.UINT32ARRAY_ID));
assert.deepEqual(module.__getUint32Array(ref), new Uint32Array(arr));
assert.deepEqual(module.__getArray(ref), arr);
module.__release(ref);
try { module.__release(ref); assert(false); } catch (e) {};
Expand All @@ -70,6 +91,7 @@ assert.strictEqual(module.__getString(module.COLOR), "red");
let arr = [0.0, 1.5, 2.5];
let ref = module.__retain(module.__allocArray(module.FLOAT32ARRAY_ID, arr));
assert(module.__instanceof(ref, module.FLOAT32ARRAY_ID));
assert.deepEqual(module.__getFloat32Array(ref), new Float32Array(arr));
assert.deepEqual(module.__getArray(ref), arr);
module.__release(ref);
try { module.__release(ref); assert(false); } catch (e) {};
Expand Down