Skip to content

Commit 317ddc1

Browse files
committed
Fix perf
1 parent ab75253 commit 317ddc1

File tree

3 files changed

+91
-54
lines changed

3 files changed

+91
-54
lines changed

packages/seroval/src/binary/deserializer.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ import type { Plugin } from './plugin';
3838

3939
export interface DeserializerContextOptions {
4040
read(): Promise<Uint8Array | undefined>;
41+
onError(error: unknown): void;
4142
refs: Map<number, { value: unknown }>;
4243
plugins?: Plugin<any, any>[];
4344
}
4445

4546
export interface DeserializerContext {
4647
read(): Promise<Uint8Array | undefined>;
48+
onError(error: unknown): void;
4749
refs: Map<number, { value: unknown }>;
4850
plugins?: Plugin<any, any>[];
4951
root: PromiseConstructorResolver;
@@ -61,6 +63,7 @@ export function createDeserializerContext(
6163
buffer: new Uint8Array(),
6264
root: PROMISE_CONSTRUCTOR(),
6365
read: options.read,
66+
onError: options.onError,
6467
refs: options.refs,
6568
marker: new Map(),
6669
resolvers: new Map(),
@@ -600,8 +603,6 @@ async function drain(ctx: DeserializerContext) {
600603
}
601604

602605
export function deserializeStart(ctx: DeserializerContext) {
603-
void drain(ctx).catch(error => {
604-
console.error(error);
605-
});
606+
void drain(ctx).catch(ctx.onError.bind(ctx));
606607
return ctx.root.p;
607608
}

packages/seroval/src/binary/encoder.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,12 @@ export function encodeString(value: string): Uint8Array {
5050
}
5151

5252
export function decodeNumber(value: Uint8Array): number {
53-
const view = new DataView(
54-
value.buffer.slice(value.byteOffset, value.byteOffset + value.byteLength),
55-
);
53+
const view = new DataView(value.buffer, value.byteOffset, value.byteLength);
5654
return view.getFloat64(0, true);
5755
}
5856

5957
export function decodeInteger(value: Uint8Array): number {
60-
const view = new DataView(
61-
value.buffer.slice(value.byteOffset, value.byteOffset + value.byteLength),
62-
);
58+
const view = new DataView(value.buffer, value.byteOffset, value.byteLength);
6359
return view.getUint32(0, true);
6460
}
6561

packages/seroval/src/binary/serializer.ts

Lines changed: 85 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,50 @@ function serializeArray(ctx: SerializerContext, value: unknown[]) {
205205
return id;
206206
}
207207

208+
function serializeStreamNext(
209+
ctx: SerializerContext,
210+
depth: number,
211+
id: Uint8Array,
212+
value: unknown,
213+
) {
214+
if (ctx.alive) {
215+
const serialized = serializeWithError(ctx, depth, value);
216+
if (serialized) {
217+
onSerialize(ctx, [SerovalNodeType.StreamNext, id, serialized]);
218+
}
219+
}
220+
}
221+
222+
function serializeStreamThrow(
223+
ctx: SerializerContext,
224+
depth: number,
225+
id: Uint8Array,
226+
value: unknown,
227+
) {
228+
if (ctx.alive) {
229+
const serialized = serializeWithError(ctx, depth, value);
230+
if (serialized) {
231+
onSerialize(ctx, [SerovalNodeType.StreamThrow, id, serialized]);
232+
}
233+
}
234+
popPendingState(ctx);
235+
}
236+
237+
function serializeStreamReturn(
238+
ctx: SerializerContext,
239+
depth: number,
240+
id: Uint8Array,
241+
value: unknown,
242+
) {
243+
if (ctx.alive) {
244+
const serialized = serializeWithError(ctx, depth, value);
245+
if (serialized) {
246+
onSerialize(ctx, [SerovalNodeType.StreamReturn, id, serialized]);
247+
}
248+
}
249+
popPendingState(ctx);
250+
}
251+
208252
function serializeStream(ctx: SerializerContext, current: Stream<unknown>) {
209253
const id = createID(ctx, current);
210254
pushPendingState(ctx);
@@ -213,32 +257,9 @@ function serializeStream(ctx: SerializerContext, current: Stream<unknown>) {
213257
const prevDepth = CURRENT_DEPTH;
214258

215259
current.on({
216-
next: value => {
217-
if (ctx.alive) {
218-
const serialized = serializeWithError(ctx, prevDepth, value);
219-
if (serialized) {
220-
onSerialize(ctx, [SerovalNodeType.StreamNext, id, serialized]);
221-
}
222-
}
223-
},
224-
throw: value => {
225-
if (ctx.alive) {
226-
const serialized = serializeWithError(ctx, prevDepth, value);
227-
if (serialized) {
228-
onSerialize(ctx, [SerovalNodeType.StreamThrow, id, serialized]);
229-
}
230-
}
231-
popPendingState(ctx);
232-
},
233-
return: value => {
234-
if (ctx.alive) {
235-
const serialized = serializeWithError(ctx, prevDepth, value);
236-
if (serialized) {
237-
onSerialize(ctx, [SerovalNodeType.StreamReturn, id, serialized]);
238-
}
239-
}
240-
popPendingState(ctx);
241-
},
260+
next: serializeStreamNext.bind(null, ctx, prevDepth, id),
261+
throw: serializeStreamThrow.bind(null, ctx, prevDepth, id),
262+
return: serializeStreamReturn.bind(null, ctx, prevDepth, id),
242263
});
243264
return id;
244265
}
@@ -368,7 +389,12 @@ function serializeBoxed(ctx: SerializerContext, value: object) {
368389
function serializeArrayBuffer(ctx: SerializerContext, value: ArrayBuffer) {
369390
const id = createID(ctx, value);
370391
const arr = new Uint8Array(value);
371-
onSerialize(ctx, [SerovalNodeType.ArrayBuffer, id, encodeInteger(arr.length), arr]);
392+
onSerialize(ctx, [
393+
SerovalNodeType.ArrayBuffer,
394+
id,
395+
encodeInteger(arr.length),
396+
arr,
397+
]);
372398
return id;
373399
}
374400

@@ -436,30 +462,44 @@ function serializeSet(ctx: SerializerContext, value: Set<unknown>) {
436462
return id;
437463
}
438464

465+
function serializePromiseSuccess(
466+
ctx: SerializerContext,
467+
depth: number,
468+
id: Uint8Array,
469+
value: unknown,
470+
) {
471+
if (ctx.alive) {
472+
const serialized = serializeWithError(ctx, depth, value);
473+
if (serialized) {
474+
onSerialize(ctx, [SerovalNodeType.PromiseSuccess, id, serialized]);
475+
}
476+
}
477+
popPendingState(ctx);
478+
}
479+
480+
function serializePromiseFailure(
481+
ctx: SerializerContext,
482+
depth: number,
483+
id: Uint8Array,
484+
value: unknown,
485+
) {
486+
if (ctx.alive) {
487+
const serialized = serializeWithError(ctx, depth, value);
488+
if (serialized) {
489+
onSerialize(ctx, [SerovalNodeType.PromiseFailure, id, serialized]);
490+
}
491+
}
492+
popPendingState(ctx);
493+
}
494+
439495
function serializePromise(ctx: SerializerContext, value: Promise<unknown>) {
440496
const id = createID(ctx, value);
441497
onSerialize(ctx, [SerovalNodeType.Promise, id]);
442498
const prevDepth = CURRENT_DEPTH;
443499
pushPendingState(ctx);
444500
value.then(
445-
val => {
446-
if (ctx.alive) {
447-
const serialized = serializeWithError(ctx, prevDepth, val);
448-
if (serialized) {
449-
onSerialize(ctx, [SerovalNodeType.PromiseSuccess, id, serialized]);
450-
}
451-
}
452-
popPendingState(ctx);
453-
},
454-
val => {
455-
if (ctx.alive) {
456-
const serialized = serializeWithError(ctx, prevDepth, val);
457-
if (serialized) {
458-
onSerialize(ctx, [SerovalNodeType.PromiseFailure, id, serialized]);
459-
}
460-
}
461-
popPendingState(ctx);
462-
},
501+
serializePromiseSuccess.bind(null, ctx, prevDepth, id),
502+
serializePromiseFailure.bind(null, ctx, prevDepth, id),
463503
);
464504
return id;
465505
}

0 commit comments

Comments
 (0)