Skip to content

Commit

Permalink
API iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnaya committed Dec 11, 2020
1 parent 32d8478 commit 05e15fd
Show file tree
Hide file tree
Showing 46 changed files with 521 additions and 437 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
lts/Erbium
lts/Fermium
2 changes: 1 addition & 1 deletion benchmarks/freeAndDisposeSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function freeAndDisposeSuite(

for (let i = 0; i < 300; i++) {
global.testTargets.push(
createObjectBuffer({}, 2e6, { K1000RowsMockData })
createObjectBuffer(2e6, { K1000RowsMockData })
);
}

Expand Down
6 changes: 3 additions & 3 deletions benchmarks/lookupsSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export function lookupsSuite(
ABCArr.map((_, index) => [`${index}${ABCArr.join("")}`, 1])
);

const OB_WITH_ABC_KEYS = createObjectBuffer({}, 2e4, objWithABCKeys);
const OB_WITH_NA2Z_KEYS = createObjectBuffer({}, 2e4, objWithNA2ZKeys);
const OB_WITH_AK1000RowsMockData = createObjectBuffer({}, 2e6, {
const OB_WITH_ABC_KEYS = createObjectBuffer(2e4, objWithABCKeys);
const OB_WITH_NA2Z_KEYS = createObjectBuffer(2e4, objWithNA2ZKeys);
const OB_WITH_AK1000RowsMockData = createObjectBuffer(2e6, {
K1000RowsMockData,
});

Expand Down
8 changes: 4 additions & 4 deletions benchmarks/savingAndCreatingSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function savingAndCreatingSuite(
.add(
`create empty, size: 2e6`,
() => {
createObjectBuffer({}, 2e6, {});
createObjectBuffer(2e6, {});
},
{
minSamples: MIN_SAMPLES,
Expand All @@ -35,7 +35,7 @@ export function savingAndCreatingSuite(
.add(
`create with ${COMMENTS_ARR_SIZE} comments. size: 2e6`,
() => {
createObjectBuffer({}, 2e6, {
createObjectBuffer(2e6, {
arrOfComments,
});
},
Expand Down Expand Up @@ -63,7 +63,7 @@ export function savingAndCreatingSuite(
global.testTargets = [];

for (let i = 0; i < EXPECTED_MAX_ITERATIONS; i++) {
global.testTargets.push(createObjectBuffer({}, 2e6, {}));
global.testTargets.push(createObjectBuffer(2e6, {}));
}

runGc();
Expand All @@ -78,7 +78,7 @@ export function savingAndCreatingSuite(
.add(
`create with all mock data rows. size: 2e6`,
() => {
createObjectBuffer<any>({}, 2e6, { K1000RowsMockData });
createObjectBuffer<any>(2e6, { K1000RowsMockData });
},
{
minSamples: MIN_SAMPLES,
Expand Down
Empty file added codesandbox-examples
Empty file.
2 changes: 1 addition & 1 deletion docs/codeExamples/basicUsage.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const initialValue = {
arrayInside: [1, "a", null],
arrayBufferRocks: 1,
};
const myObject = createObjectBuffer({}, 1024, initialValue);
const myObject = createObjectBuffer(1024, initialValue);

myObject.arrayInside.push("another entry");
myObject.foo.anotherProperty = "value";
Expand Down
11 changes: 2 additions & 9 deletions playground/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@ import * as objectbufferModules from "../src";
// @ts-expect-error
import Worker from "worker-loader!./Worker.js";

/**
* @type {objectbufferModules.ExternalArgs}
*/
const externalArgs = {
arrayAdditionalAllocation: 0,
};

const ob = objectbufferModules.createObjectBuffer(
externalArgs,
1024,
{
some: {
Expand All @@ -26,7 +18,8 @@ const ob = objectbufferModules.createObjectBuffer(
],
},
},
{ useSharedArrayBuffer: true }
{},
"shared"
);

// o.yetAnother = [234, 234, "asf"];
Expand Down
8 changes: 2 additions & 6 deletions playground/transferAndSortArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as objectbufferModule from "../src"
// @ts-ignore
import Worker from "worker-loader!./transferAndSortArrayWorker.ts";

interface Post {
export interface Post {
postId: number;
id: number;
name: string;
Expand All @@ -21,16 +21,12 @@ async function getData() {
return data;
}

const externalArgs: objectbufferModule.ExternalArgs = {
};

async function main() {
const data = await getData();

const dataSize = 10 * 10000000

const myObjectBuffer = objectbufferModule.createObjectBuffer(
externalArgs,
dataSize * 1.15,
{
posts: data
Expand All @@ -49,7 +45,7 @@ async function main() {
resultChannel.port1.addEventListener("message", messageEvent => {
if (messageEvent.data instanceof ArrayBuffer) {
console.log("Got AB", messageEvent.data.byteLength);
objectbufferModule.replaceUnderlyingArrayBuffer(
objectbufferModule.unstable_replaceUnderlyingArrayBuffer(
myObjectBuffer,
messageEvent.data
);
Expand Down
5 changes: 1 addition & 4 deletions playground/transferAndSortArrayWorker.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
/* eslint-disable */

import * as objectbufferModule from "../src";

const externalArgs: objectbufferModule.ExternalArgs = {
};
import { Post } from "./transferAndSortArray"

addEventListener("message", ev => {
if (ev.data[0] instanceof ArrayBuffer) {
const port: MessagePort = ev.data[1];
const ab: ArrayBuffer = ev.data[0];

const myObjectBufferInWorker = objectbufferModule.loadObjectBuffer(
externalArgs,
ab
);

Expand Down
19 changes: 8 additions & 11 deletions src/apiTests/Map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

import { createObjectBuffer } from "../";
import { memoryStats } from "../internal/api";
import { externalArgsApiToExternalArgsApi } from "../internal/utils";

describe("Map", () => {
const externalArgs = externalArgsApiToExternalArgsApi({});

test("creation", () => {
const objectBuffer = createObjectBuffer<any>(externalArgs, 1024, {});
const objectBuffer = createObjectBuffer<any>(1024, {});
expect(memoryStats(objectBuffer).available).toMatchInlineSnapshot(`816`);

objectBuffer.foo = new Map([[1, "a"]]);
Expand All @@ -21,7 +18,7 @@ describe("Map", () => {
});

test("add", () => {
const objectBuffer = createObjectBuffer<any>(externalArgs, 1024, {});
const objectBuffer = createObjectBuffer<any>(1024, {});
expect(memoryStats(objectBuffer).available).toMatchInlineSnapshot(`816`);

objectBuffer.foo = new Map([[1, "a"]]);
Expand All @@ -36,15 +33,15 @@ describe("Map", () => {
});

test("has", () => {
const objectBuffer = createObjectBuffer<any>(externalArgs, 1024, {});
const objectBuffer = createObjectBuffer<any>(1024, {});
objectBuffer.foo = new Map([[1, "a"]]);
objectBuffer.foo.set("2", "b");
expect(objectBuffer.foo.has("2")).toEqual(true);
expect(objectBuffer.foo.has("none exiting")).toEqual(false);
});

test("delete", () => {
const objectBuffer = createObjectBuffer<any>(externalArgs, 1024, {});
const objectBuffer = createObjectBuffer<any>(1024, {});
expect(memoryStats(objectBuffer).available).toMatchInlineSnapshot(`816`);

objectBuffer.foo = new Map([[1, "a"]]);
Expand All @@ -63,7 +60,7 @@ describe("Map", () => {
});

test("clear", () => {
const objectBuffer = createObjectBuffer<any>(externalArgs, 1024, {});
const objectBuffer = createObjectBuffer<any>(1024, {});
expect(memoryStats(objectBuffer).available).toMatchInlineSnapshot(`816`);

objectBuffer.foo = new Map();
Expand All @@ -82,7 +79,7 @@ describe("Map", () => {
});

test("iterate", () => {
const objectBuffer = createObjectBuffer<any>(externalArgs, 1024, {});
const objectBuffer = createObjectBuffer<any>(1024, {});
expect(memoryStats(objectBuffer).available).toMatchInlineSnapshot(`816`);

objectBuffer.foo = new Map([[1, "a"]]);
Expand Down Expand Up @@ -121,7 +118,7 @@ describe("Map", () => {
nativeMap.delete(key);
}

const objectBuffer = createObjectBuffer<any>(externalArgs, 1024, {
const objectBuffer = createObjectBuffer<any>(1024, {
foo: new Map([
[1, "a"],
[2, "b"],
Expand All @@ -135,7 +132,7 @@ describe("Map", () => {
});

test("forEach", () => {
const objectBuffer = createObjectBuffer<any>(externalArgs, 1024, {});
const objectBuffer = createObjectBuffer<any>(1024, {});
expect(memoryStats(objectBuffer).available).toMatchInlineSnapshot(`816`);

objectBuffer.foo = new Map([[1, "a"]]);
Expand Down
19 changes: 8 additions & 11 deletions src/apiTests/Set.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

import { createObjectBuffer } from "../";
import { memoryStats } from "../internal/api";
import { externalArgsApiToExternalArgsApi } from "../internal/utils";

describe("Set tests", () => {
const externalArgs = externalArgsApiToExternalArgsApi({});

test("creation", () => {
const objectBuffer = createObjectBuffer<any>(externalArgs, 1024, {});
const objectBuffer = createObjectBuffer<any>(1024, {});
expect(memoryStats(objectBuffer).available).toMatchInlineSnapshot(`816`);

objectBuffer.foo = new Set(["a"]);
Expand All @@ -21,7 +18,7 @@ describe("Set tests", () => {
});

test("add", () => {
const objectBuffer = createObjectBuffer<any>(externalArgs, 1024, {});
const objectBuffer = createObjectBuffer<any>(1024, {});
expect(memoryStats(objectBuffer).available).toMatchInlineSnapshot(`816`);

objectBuffer.foo = new Set(["a"]);
Expand All @@ -36,15 +33,15 @@ describe("Set tests", () => {
});

test("has", () => {
const objectBuffer = createObjectBuffer<any>(externalArgs, 1024, {});
const objectBuffer = createObjectBuffer<any>(1024, {});
objectBuffer.foo = new Set(["a"]);
objectBuffer.foo.add("b");
expect(objectBuffer.foo.has("b")).toEqual(true);
expect(objectBuffer.foo.has("none exiting")).toEqual(false);
});

test("delete", () => {
const objectBuffer = createObjectBuffer<any>(externalArgs, 1024, {});
const objectBuffer = createObjectBuffer<any>(1024, {});
expect(memoryStats(objectBuffer).available).toMatchInlineSnapshot(`816`);

objectBuffer.foo = new Set(["a"]);
Expand All @@ -64,7 +61,7 @@ describe("Set tests", () => {
});

test("clear", () => {
const objectBuffer = createObjectBuffer<any>(externalArgs, 1024, {});
const objectBuffer = createObjectBuffer<any>(1024, {});
expect(memoryStats(objectBuffer).available).toMatchInlineSnapshot(`816`);

objectBuffer.foo = new Set(["a"]);
Expand All @@ -79,7 +76,7 @@ describe("Set tests", () => {
});

test("iterate", () => {
const objectBuffer = createObjectBuffer<any>(externalArgs, 1024, {});
const objectBuffer = createObjectBuffer<any>(1024, {});
expect(memoryStats(objectBuffer).available).toMatchInlineSnapshot(`816`);

objectBuffer.foo = new Set(["a"]);
Expand Down Expand Up @@ -115,7 +112,7 @@ describe("Set tests", () => {
nativeMap.delete(key);
}

const objectBuffer = createObjectBuffer<any>(externalArgs, 1024, {
const objectBuffer = createObjectBuffer<any>(1024, {
foo: new Set(["a", "b"]),
});
for (const [key] of objectBuffer.foo) {
Expand All @@ -126,7 +123,7 @@ describe("Set tests", () => {
});

test("forEach", () => {
const objectBuffer = createObjectBuffer<any>(externalArgs, 1024, {});
const objectBuffer = createObjectBuffer<any>(1024, {});
expect(memoryStats(objectBuffer).available).toMatchInlineSnapshot(`816`);

objectBuffer.foo = new Set(["a"]);
Expand Down
36 changes: 14 additions & 22 deletions src/apiTests/array.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
/* eslint-env jest */

import { createObjectBuffer } from "../";
import { memoryStats, disposeWrapperObject } from "../internal/api";
import { externalArgsApiToExternalArgsApi } from "../internal/utils";
import { memoryStats, reclaim } from "../internal/api";

// actually not very good, as the browser's TextEncoder won't work with SAB, but node will.
describe("SharedArrayBuffer tests", () => {
const externalArgs = externalArgsApiToExternalArgsApi({
arrayAdditionalAllocation: 0,
});

test("basic", () => {
const objectBuffer = createObjectBuffer<any>(
externalArgs,
1024,
{ arr: [1, 2, 3, 4] },
{ useSharedArrayBuffer: false }
);
const objectBuffer = createObjectBuffer<any>(1024, { arr: [1, 2, 3, 4] });

objectBuffer.arr.unshift("a");

Expand All @@ -37,10 +27,9 @@ describe("SharedArrayBuffer tests", () => {

test("splice 1", () => {
const ob = createObjectBuffer<{ arr: (number | null)[] }>(
{ arrayAdditionalAllocation: 10 },
1024,
{ arr: [] },
{ useSharedArrayBuffer: false }
{ arrayAdditionalAllocation: 10 }
);

const usedAfterCreate = memoryStats(ob).used;
Expand Down Expand Up @@ -94,10 +83,9 @@ describe("SharedArrayBuffer tests", () => {

test("splice 2", () => {
const ob = createObjectBuffer<{ arr: any }>(
{ arrayAdditionalAllocation: 0 },
1024 * 2,
{ arr: [] },
{ useSharedArrayBuffer: false }
{ arrayAdditionalAllocation: 0 }
);

const usedAfterCreate = memoryStats(ob).used;
Expand Down Expand Up @@ -183,9 +171,9 @@ describe("SharedArrayBuffer tests", () => {
`1016`
);

disposeWrapperObject(ob.arr);
reclaim(ob.arr);

removedArrays.forEach((a: any) => disposeWrapperObject(a));
removedArrays.forEach((a: any) => reclaim(a));

const usedDisposeArrays = memoryStats(ob).used;
expect(usedDisposeArrays - usedAfterCreate).toMatchInlineSnapshot(`56`);
Expand All @@ -194,9 +182,13 @@ describe("SharedArrayBuffer tests", () => {
test("flat", () => {
const input = [[{ v: 1 }], [{ v: 2 }], [{ v: 3 }], [{ v: 4 }]];

const o = createObjectBuffer({ arrayAdditionalAllocation: 0 }, 1024 * 2, {
arr: input,
});
const o = createObjectBuffer(
1024 * 2,
{
arr: input,
},
{ arrayAdditionalAllocation: 0 }
);

const output = o.arr.flat();

Expand All @@ -221,7 +213,7 @@ describe("SharedArrayBuffer tests", () => {
test("flatMap", () => {
const input = [[{ v: 1 }], [{ v: 2 }], [{ v: 3 }], [{ v: 4 }]];

const o = createObjectBuffer({ arrayAdditionalAllocation: 0 }, 1024 * 2, {
const o = createObjectBuffer(1024 * 2, {
arr: input,
});

Expand Down

3 comments on commit 05e15fd

@Bnaya
Copy link
Owner Author

@Bnaya Bnaya commented on 05e15fd Dec 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'benchmarkjs, node 15'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.10.

Benchmark suite Current: 05e15fd Previous: 2ffbbd7 Ratio
object memory free. K1000RowsMockData, pre-created OB, size: 2e6 10.45 ops/sec (±2.72%) 12.03 ops/sec (±1.93%) 1.15

This comment was automatically generated by workflow using github-action-benchmark.

CC: @Bnaya

@Bnaya
Copy link
Owner Author

@Bnaya Bnaya commented on 05e15fd Dec 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

benchmarkjs, node 14

Benchmark suite Current: 05e15fd Previous: 2ffbbd7 Ratio
create empty, size: 2e6 6683 ops/sec (±3.63%) 9253 ops/sec (±2.60%) 1.38
create with 2500 comments. size: 2e6 92.95 ops/sec (±0.98%) 133 ops/sec (±1.29%) 1.43
save 2500 comments into pre-created OB, size: 2e6 88.87 ops/sec (±1.12%) 124 ops/sec (±1.70%) 1.40
create with all mock data rows. size: 2e6 93.84 ops/sec (±1.70%) 135 ops/sec (±1.12%) 1.44
A-Z object keys 21729 ops/sec (±1.38%) 30188 ops/sec (±1.27%) 1.39
A-Z object prop Lookup in operator - non-existing 7035903 ops/sec (±0.77%) 9876088 ops/sec (±0.87%) 1.40
A-Z object prop Lookup in operator - existing 1440021 ops/sec (±1.15%) 2041795 ops/sec (±0.88%) 1.42
A-Z object prop access T 1367424 ops/sec (±0.99%) 1913962 ops/sec (±0.95%) 1.40
A-Z object prop access Z 1350655 ops/sec (±1.06%) 1915887 ops/sec (±0.94%) 1.42
OB_WITH_NA2Z_KEYS object keys 12419 ops/sec (±1.05%) 18530 ops/sec (±1.05%) 1.49
OB_WITH_NA2Z_KEYS object prop Lookup in operator - non-existing 825483 ops/sec (±0.89%) 1178962 ops/sec (±0.80%) 1.43
OB_WITH_NA2Z_KEYS object prop Lookup in operator - existing 790013 ops/sec (±1.05%) 1130341 ops/sec (±0.93%) 1.43
OB_WITH_NA2Z_KEYS object prop access 14ABCDEFGHIJKLMNOPQRSTUVWXYZ 753911 ops/sec (±1.31%) 1087857 ops/sec (±0.81%) 1.44
OB_WITH_NA2Z_KEYS object prop access 3ABCDEFGHIJKLMNOPQRSTUVWXYZ 769402 ops/sec (±1.28%) 1105569 ops/sec (±0.94%) 1.44
Array access. length: 1000 1807421 ops/sec (±1.00%) 2891762 ops/sec (±1.12%) 1.60
object memory free. K1000RowsMockData, pre-created OB, size: 2e6 9.49 ops/sec (±1.96%) 13.31 ops/sec (±1.87%) 1.40

This comment was automatically generated by workflow using github-action-benchmark.

@Bnaya
Copy link
Owner Author

@Bnaya Bnaya commented on 05e15fd Dec 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'benchmarkjs, node 14'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.10.

Benchmark suite Current: 05e15fd Previous: 2ffbbd7 Ratio
create empty, size: 2e6 6683 ops/sec (±3.63%) 9253 ops/sec (±2.60%) 1.38
create with 2500 comments. size: 2e6 92.95 ops/sec (±0.98%) 133 ops/sec (±1.29%) 1.43
save 2500 comments into pre-created OB, size: 2e6 88.87 ops/sec (±1.12%) 124 ops/sec (±1.70%) 1.40
create with all mock data rows. size: 2e6 93.84 ops/sec (±1.70%) 135 ops/sec (±1.12%) 1.44
A-Z object keys 21729 ops/sec (±1.38%) 30188 ops/sec (±1.27%) 1.39
A-Z object prop Lookup in operator - non-existing 7035903 ops/sec (±0.77%) 9876088 ops/sec (±0.87%) 1.40
A-Z object prop Lookup in operator - existing 1440021 ops/sec (±1.15%) 2041795 ops/sec (±0.88%) 1.42
A-Z object prop access T 1367424 ops/sec (±0.99%) 1913962 ops/sec (±0.95%) 1.40
A-Z object prop access Z 1350655 ops/sec (±1.06%) 1915887 ops/sec (±0.94%) 1.42
OB_WITH_NA2Z_KEYS object keys 12419 ops/sec (±1.05%) 18530 ops/sec (±1.05%) 1.49
OB_WITH_NA2Z_KEYS object prop Lookup in operator - non-existing 825483 ops/sec (±0.89%) 1178962 ops/sec (±0.80%) 1.43
OB_WITH_NA2Z_KEYS object prop Lookup in operator - existing 790013 ops/sec (±1.05%) 1130341 ops/sec (±0.93%) 1.43
OB_WITH_NA2Z_KEYS object prop access 14ABCDEFGHIJKLMNOPQRSTUVWXYZ 753911 ops/sec (±1.31%) 1087857 ops/sec (±0.81%) 1.44
OB_WITH_NA2Z_KEYS object prop access 3ABCDEFGHIJKLMNOPQRSTUVWXYZ 769402 ops/sec (±1.28%) 1105569 ops/sec (±0.94%) 1.44
Array access. length: 1000 1807421 ops/sec (±1.00%) 2891762 ops/sec (±1.12%) 1.60
object memory free. K1000RowsMockData, pre-created OB, size: 2e6 9.49 ops/sec (±1.96%) 13.31 ops/sec (±1.87%) 1.40

This comment was automatically generated by workflow using github-action-benchmark.

CC: @Bnaya

Please sign in to comment.