Skip to content

Commit e23b8f5

Browse files
committed
fix: resolve memory leak in parser buffer management with threshold-based shrinking
Implement smart buffer shrinking that only reallocates when there's significant waste: - Only shrink if waste > max(4x remaining data, 64KB) - When shrinking, allocate 2x remaining data for growth room - Fall back to cursor strategy for normal cases This prevents memory leaks from large buffers while avoiding excessive allocations in high-throughput scenarios, addressing both memory efficiency and performance concerns.
1 parent 65bc3d4 commit e23b8f5

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

packages/pg-protocol/src/parser.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,25 @@ export class Parser {
112112
this.bufferLength = 0
113113
this.bufferOffset = 0
114114
} else {
115-
// Adjust the cursors of remainingBuffer
116-
this.bufferLength = bufferFullLength - offset
117-
this.bufferOffset = offset
115+
// A partial message remains.
116+
// Only shrink the buffer if it's significantly larger than needed to prevent memory leaks
117+
// while avoiding excessive allocations in high-throughput scenarios.
118+
const remainingLength = bufferFullLength - offset
119+
const bufferWaste = this.buffer.byteLength - remainingLength
120+
const wasteThreshold = Math.max(remainingLength * 4, 1024 * 64) // 4x remaining data or 64KB, whichever is larger
121+
122+
if (bufferWaste > wasteThreshold) {
123+
// Significant waste detected - create a new appropriately-sized buffer
124+
const newBuffer = Buffer.allocUnsafe(remainingLength * 2) // Give some room for growth
125+
this.buffer.copy(newBuffer, 0, offset, offset + remainingLength)
126+
this.buffer = newBuffer
127+
this.bufferOffset = 0
128+
this.bufferLength = remainingLength
129+
} else {
130+
// Not enough waste to justify reallocation - use existing cursor strategy
131+
this.bufferLength = remainingLength
132+
this.bufferOffset = offset
133+
}
118134
}
119135
}
120136

0 commit comments

Comments
 (0)