소스 검색

Optimize BinaryWriter memory usage (#591)

- 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.
keef3ar 1 년 전
부모
커밋
468fd515e6
1개의 변경된 파일5개의 추가작업 그리고 5개의 파일을 삭제
  1. 5 5
      gramjs/extensions/BinaryWriter.ts

+ 5 - 5
gramjs/extensions/BinaryWriter.ts

@@ -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);
     }
 }