From 14ed4ffde938a95cd8947b69b25bb05973733cc3 Mon Sep 17 00:00:00 2001 From: Alisue Date: Sun, 13 Jun 2021 22:14:46 +0900 Subject: [PATCH] Use Uint8Array as-is to improve performance 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 --- README.md | 9 +++++++-- reader.ts | 2 +- reader_test.ts | 12 ++++++------ types.ts | 2 +- writer.ts | 5 +---- writer_test.ts | 6 +++--- 6 files changed, 19 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 464cd97..b05e6c2 100644 --- a/README.md +++ b/README.md @@ -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/ diff --git a/reader.ts b/reader.ts index 8cf4041..a4c271d 100644 --- a/reader.ts +++ b/reader.ts @@ -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); } }; } diff --git a/reader_test.ts b/reader_test.ts index e4ecbb9..5fbdcf8 100644 --- a/reader_test.ts +++ b/reader_test.ts @@ -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; @@ -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; @@ -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; diff --git a/types.ts b/types.ts index 19ea774..88b6d40 100644 --- a/types.ts +++ b/types.ts @@ -1,4 +1,4 @@ -type Payload = number[]; +type Payload = Uint8Array; export type WorkerForWorkerReader = { onmessage?: (message: MessageEvent) => void; diff --git a/writer.ts b/writer.ts index 4597582..360c277 100644 --- a/writer.ts +++ b/writer.ts @@ -16,10 +16,7 @@ export class WorkerWriter implements Deno.Writer { * https://github.com/lambdalisue/deno-workerio/issues/5 */ write(p: Uint8Array): Promise { - // 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); } } diff --git a/writer_test.ts b/writer_test.ts index 038b7f3..7b4c434 100644 --- a/writer_test.ts +++ b/writer_test.ts @@ -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); @@ -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])); }, );