Skip to content

Commit

Permalink
Lazily create encoder for IncomingMessage
Browse files Browse the repository at this point in the history
Approximately 2/3 of the initialization time of `new IncomingMessage` is taken
by the `createEncoder` call within IncomingMessage's constructor.

The vast majority of IncomingMessage usage is to decode the incoming message
first to determine if it is relevant--i.e. to determine what document it
belongs to.

This change improves performance by avoiding the `createEncoder` call until
we know it is actually necessary.
  • Loading branch information
canadaduane committed Aug 9, 2023
1 parent 4160dd7 commit c981614
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/server/src/IncomingMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,23 @@ export class IncomingMessage {
/**
* Access to the reply.
*/
encoder: Encoder
private encoderInternal?: Encoder

constructor(input: any) {
if (!(input instanceof Uint8Array)) {
input = new Uint8Array(input)
}

this.encoder = createEncoder()
this.decoder = createDecoder(input)
}

get encoder() {
if (!this.encoderInternal) {
this.encoderInternal = createEncoder()
}
return this.encoderInternal
}

readVarUint8Array() {
return readVarUint8Array(this.decoder)
}
Expand Down

0 comments on commit c981614

Please sign in to comment.