Skip to content

Commit

Permalink
Merge pull request #10 from lambdalisue/improve-performance
Browse files Browse the repository at this point in the history
Improve performance by sending data as-is
  • Loading branch information
lambdalisue authored Jun 13, 2021
2 parents e208ef6 + 8e16d5c commit 512a724
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
deno-version: ${{ env.DENO_VERSION }}
- name: Test
run: |
deno test --unstable --allow-read --allow-net
deno test --no-check --unstable --allow-read --allow-net
timeout-minutes: 5

typecheck:
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
[Deno][deno] module to translate Worker's system of messages into
[Reader][reader] and [Writer][writer].

Note that `WorkerReader` and `WorkerWriter` access `Deno` namespace thus
[Using Deno in worker](https://deno.land/manual@v1.7.5/runtime/workers#using-deno-in-worker)
Note that this package requires
[`Worker.postMessage` supports structured clone
algorithm](https://deno.com/blog/v1.10#worker.postmessage-supports-structured-clone-algorithm)
introduced in Deno v1.10.

Note that this package accesses `Deno` namespace thus
[Using Deno in worker](https://deno.land/manual@v1.11.0/runtime/workers#using-deno-in-worker)
must be enabled.

[deno]: https://deno.land/
Expand Down
6 changes: 3 additions & 3 deletions reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class WorkerReader implements Deno.Reader, Deno.Closer {
this.#worker = worker;
this.#worker.onmessage = (e) => {
if (this.#queue && !this.#closed) {
this.#queue.put_nowait(new Uint8Array(e.data));
this.#queue.put_nowait(e.data);
}
};
}
Expand Down Expand Up @@ -45,8 +45,8 @@ export class WorkerReader implements Deno.Reader, Deno.Closer {

private readFromRemain(p: Uint8Array): number {
const n = p.byteLength;
const d = this.#remain.slice(0, n);
this.#remain = this.#remain.slice(n);
const d = this.#remain.subarray(0, n);
this.#remain = this.#remain.subarray(n);
p.set(d);
return d.byteLength;
}
Expand Down
12 changes: 6 additions & 6 deletions reader_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ Deno.test(
worker.onmessage?.call(
worker,
new MessageEvent("worker", {
data: [0, 1, 2, 3, 4],
data: new Uint8Array([0, 1, 2, 3, 4]),
}),
);
worker.onmessage?.call(
worker,
new MessageEvent("worker", {
data: [5, 6, 7, 8, 9],
data: new Uint8Array([5, 6, 7, 8, 9]),
}),
);
let p: Uint8Array;
Expand All @@ -45,14 +45,14 @@ Deno.test(
worker.onmessage?.call(
worker,
new MessageEvent("worker", {
data: [0, 1, 2, 3, 4],
data: new Uint8Array([0, 1, 2, 3, 4]),
}),
);
reader.close();
worker.onmessage?.call(
worker,
new MessageEvent("worker", {
data: [5, 6, 7, 8, 9],
data: new Uint8Array([5, 6, 7, 8, 9]),
}),
);
let p: Uint8Array;
Expand All @@ -78,14 +78,14 @@ Deno.test(
worker.onmessage?.call(
worker,
new MessageEvent("worker", {
data: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
data: new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
}),
);
reader.close();
worker.onmessage?.call(
worker,
new MessageEvent("worker", {
data: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
data: new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
}),
);
let n: number | null;
Expand Down
12 changes: 6 additions & 6 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ Deno.test({
]);

// Peformance check
const rtThreshold = 3000;
const wtThreshold = 3000;
const rtThreshold = 500;
const wtThreshold = 500;
assert(rt < rtThreshold, "Reader is too slow");
assert(wt < wtThreshold, "Writer is too slow");
} finally {
Expand Down Expand Up @@ -88,8 +88,8 @@ Deno.test({
]);

// Peformance check
const rtThreshold = 5000;
const wtThreshold = 5000;
const rtThreshold = 500;
const wtThreshold = 500;
assert(rt < rtThreshold, "Reader is too slow");
assert(wt < wtThreshold, "Writer is too slow");
} finally {
Expand Down Expand Up @@ -133,8 +133,8 @@ Deno.test({
]);

// Peformance check
const rtThreshold = 30000;
const wtThreshold = 30000;
const rtThreshold = 500;
const wtThreshold = 500;
assert(rt < rtThreshold, "Reader is too slow");
assert(wt < wtThreshold, "Writer is too slow");
} finally {
Expand Down
2 changes: 1 addition & 1 deletion types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type Payload = number[];
type Payload = Uint8Array;

export type WorkerForWorkerReader = {
onmessage?: (message: MessageEvent<Payload>) => void;
Expand Down
5 changes: 1 addition & 4 deletions writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ export class WorkerWriter implements Deno.Writer {
* https://github.com/lambdalisue/deno-workerio/issues/5
*/
write(p: Uint8Array): Promise<number> {
// XXX
// Send 'p' as-is once the issue below has resolved.
// https://github.com/denoland/deno/issues/3557
this.#worker.postMessage(Array.from(p));
this.#worker.postMessage(p);
return Promise.resolve(p.length);
}
}
6 changes: 3 additions & 3 deletions writer_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { WorkerForWorkerWriter, WorkerWriter } from "./mod.ts";
Deno.test(
"WorkerWriter invokes internal worker.postMessage when data is written by 'write' method",
async () => {
const posted: number[][] = [];
const posted: Uint8Array[] = [];
const worker: WorkerForWorkerWriter = {
postMessage(message) {
posted.push(message);
Expand All @@ -13,7 +13,7 @@ Deno.test(
const writer = new WorkerWriter(worker);
await writer.write(new Uint8Array([0, 1, 2, 3, 4]));
await writer.write(new Uint8Array([5, 6, 7, 8, 9]));
assertEquals(posted[0], [0, 1, 2, 3, 4]);
assertEquals(posted[1], [5, 6, 7, 8, 9]);
assertEquals(posted[0], new Uint8Array([0, 1, 2, 3, 4]));
assertEquals(posted[1], new Uint8Array([5, 6, 7, 8, 9]));
},
);

0 comments on commit 512a724

Please sign in to comment.