Skip to content

Commit

Permalink
Optimize BinaryWriter memory usage (#591)
Browse files Browse the repository at this point in the history
- Replace continuous buffer concatenation with a buffer array to reduce intermediate buffer creations.
- This approach mitigates potential memory leaks and improves performance, especially for large files.
  • Loading branch information
keef3ar authored Oct 3, 2023
1 parent b36f7c5 commit 468fd51
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions gramjs/extensions/BinaryWriter.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
export class BinaryWriter {
private _stream: Buffer;
private readonly _buffers: Buffer[];

constructor(stream: Buffer) {
this._stream = stream;
this._buffers = [stream];
}

write(buffer: Buffer) {
this._stream = Buffer.concat([this._stream, buffer]);
this._buffers.push(buffer);
}

getValue() {
return this._stream;
getValue(): Buffer {
return Buffer.concat(this._buffers);
}
}

0 comments on commit 468fd51

Please sign in to comment.