Skip to content
Draft
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: 7 additions & 6 deletions src/array/ShareableArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,8 @@ export class ShareableArray<T> extends TransferableDataStructure {
*/
private defragment() {
const newData: ArrayBuffer = new ArrayBuffer(this.dataView.byteLength);
const newView = new DataView(newData);
const newUint8Array = new Uint8Array(newData);
const oldUint8Array = new Uint8Array(this.dataMem);

let currentDataStart = 0;

Expand All @@ -956,9 +957,10 @@ export class ShareableArray<T> extends TransferableDataStructure {
const currentObjectLength = this.dataView.getUint32(currentDataPos + 4) + ShareableArray.DATA_OBJECT_OFFSET;

// Copy all bytes to the new array
for (let i = 0; i < currentObjectLength; i++) {
newView.setUint8(currentDataStart + i, this.dataView.getUint8(currentDataPos + i));
}
newUint8Array.set(
oldUint8Array.subarray(currentDataPos, currentDataPos + currentObjectLength),
currentDataStart
);

// Update the position where this is stored in the index array
this.indexView.setUint32(ShareableArray.INDEX_TABLE_OFFSET + 4 * i, currentDataStart);
Expand All @@ -968,8 +970,7 @@ export class ShareableArray<T> extends TransferableDataStructure {
}

// Replace the data from the old data array with the data in the array
const oldArray = new Uint8Array(this.dataMem);
oldArray.set(new Uint8Array(newData));
oldUint8Array.set(newUint8Array);

// Update where the free space in the data array starts again
this.freeStart = currentDataStart;
Expand Down