Skip to content

Commit

Permalink
Use Uint8Array as-is to improve performance
Browse files Browse the repository at this point in the history
Close #3

Note that this package supports Deno v1.10 or over from this commit.

https://deno.com/blog/v1.10#worker.postmessage-supports-structured-clone-algorithm
  • Loading branch information
lambdalisue committed Jun 13, 2021
1 parent e208ef6 commit 14ed4ff
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 17 deletions.
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
2 changes: 1 addition & 1 deletion 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
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
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 14ed4ff

Please sign in to comment.