Skip to content

Commit

Permalink
Improve uint8array serializing perf (#13)
Browse files Browse the repository at this point in the history
Signed-off-by: Marcos Candeia <[email protected]>
  • Loading branch information
mcandeia authored Nov 15, 2024
1 parent 615b507 commit 5fa77a7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/actors/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { ActorStorage } from "./storage.ts";
import { DenoKvActorStorage } from "./storage/denoKv.ts";
import { S3ActorStorage } from "./storage/s3.ts";
import { EVENT_STREAM_RESPONSE_HEADER } from "./stream.ts";
import { serializeUint8Array } from "./util/buffers.ts";
import { isUpgrade, makeWebSocket } from "./util/channels/channel.ts";
import {
type ServerSentEventMessage,
Expand Down Expand Up @@ -190,7 +191,7 @@ export class ActorRuntime {
for await (const content of res) {
controller.enqueue({
data: encodeURIComponent(
JSON.stringify(content),
JSON.stringify(content, serializeUint8Array),
),
id: Date.now().toString(),
event: "message",
Expand Down
5 changes: 4 additions & 1 deletion src/actors/stream.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { deserializeUint8Array } from "./util/buffers.ts";

export const EVENT_STREAM_RESPONSE_HEADER: string = "text/event-stream";
export async function* readFromStream<T>(
response: Response,
Expand Down Expand Up @@ -28,7 +30,7 @@ export async function* readFromStream<T>(

try {
const chunk = data.replace("data:", "");
yield JSON.parse(decodeURIComponent(chunk));
yield JSON.parse(decodeURIComponent(chunk), deserializeUint8Array);
} catch (_err) {
console.log("error parsing data", _err, data);
continue;
Expand All @@ -41,6 +43,7 @@ export async function* readFromStream<T>(
try {
yield JSON.parse(
decodeURIComponent(buffer.replace("data:", "")),
deserializeUint8Array,
);
} catch (_err) {
console.log("error parsing data", _err, buffer);
Expand Down
28 changes: 28 additions & 0 deletions src/actors/util/buffers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export interface SerializedUint8Array {
__uint8array: number[];
}

const isSerializedUint8Array = (
s: unknown | SerializedUint8Array,
): s is SerializedUint8Array => {
return typeof s === "object" &&
(s as SerializedUint8Array)?.__uint8array instanceof Array;
};
export const serializeUint8Array = (_: string, value: unknown) => {
if (value instanceof Uint8Array) {
return {
__uint8array: Array.from(value), // Mark arrays that should be Uint8Array
};
}
return value;
};

export const deserializeUint8Array = (
_: string,
value: unknown | SerializedUint8Array,
) => {
if (isSerializedUint8Array(value)) {
return new Uint8Array(value.__uint8array);
}
return value;
};

0 comments on commit 5fa77a7

Please sign in to comment.