Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
13 changes: 13 additions & 0 deletions src/TransferableDataStructure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ export default abstract class TransferableDataStructure {
private decoderBuffer: ArrayBuffer = new ArrayBuffer(TransferableDataStructure.DECODER_BUFFER_SIZE);
private currentDecoderBufferSize: number = TransferableDataStructure.DECODER_BUFFER_SIZE;

// Check if the current environment supports decoding directly from a SharedArrayBuffer view.
protected static readonly SUPPORTS_SAB_VIEW = (() => {
try {
if (typeof SharedArrayBuffer === "undefined") return false;
const sab = new SharedArrayBuffer(0);
const view = new Uint8Array(sab);
new TextDecoder().decode(view);
return true;
} catch {
return false;
}
})();

protected allocateMemory(byteSize: number): SharedArrayBuffer | ArrayBuffer {
try {
return new SharedArrayBuffer(byteSize);
Expand Down
4 changes: 4 additions & 0 deletions src/array/ShareableArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,10 @@ export class ShareableArray<T> extends TransferableDataStructure {
// Copy from shared memory to a temporary private buffer (since we cannot directly decode from shared memory)
const sourceView = new Uint8Array(this.dataView.buffer, dataPos + ShareableArray.DATA_OBJECT_OFFSET, valueLength);

if (ShareableArray.SUPPORTS_SAB_VIEW) {
return encoder.decode(sourceView);
}

const targetView = new Uint8Array(this.getFittingDecoderBuffer(valueLength), 0, valueLength);
targetView.set(sourceView);

Expand Down
2 changes: 1 addition & 1 deletion src/encoding/NumberEncoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Serializable from "./Serializable";

export default class NumberEncoder implements Serializable<number> {
decode(buffer: Uint8Array): number {
const bufferView = new DataView(buffer.buffer);
const bufferView = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);

// First byte indicates if we did store a float or an int
const numberType = bufferView.getUint8(0);
Expand Down
8 changes: 8 additions & 0 deletions src/map/ShareableMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,10 @@ export class ShareableMap<K, V> extends TransferableDataStructure {

const sourceView = new Uint8Array(this.dataView.buffer, startPos + ShareableMap.DATA_OBJECT_OFFSET, keyLength);

if (ShareableMap.SUPPORTS_SAB_VIEW) {
return this.textDecoder.decode(sourceView);
}

const targetView = new Uint8Array(this.getFittingDecoderBuffer(keyLength), 0, keyLength);
targetView.set(sourceView);

Expand Down Expand Up @@ -762,6 +766,10 @@ export class ShareableMap<K, V> extends TransferableDataStructure {
// Copy from shared memory to a temporary private buffer (since we cannot directly decode from shared memory)
const sourceView = new Uint8Array(this.dataView.buffer, startPos + ShareableMap.DATA_OBJECT_OFFSET + keyLength, valueLength);

if (ShareableMap.SUPPORTS_SAB_VIEW) {
return encoder.decode(sourceView);
}

const targetView = new Uint8Array(this.getFittingDecoderBuffer(valueLength), 0, valueLength);
targetView.set(sourceView);

Expand Down