Skip to content

Commit 470b86a

Browse files
committed
fix: remove unused dtypeToOid import + eliminate double textEncoder.encode in dataRow
handler.ts imported dtypeToOid but never used it. protocol.ts dataRow() called textEncoder.encode() twice per non-null value (once for size calc, once for writing) — now pre-encodes into an array for single-pass write.
1 parent 3df99e6 commit 470b86a

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/pg-wire/handler.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
dataRow,
2323
commandComplete,
2424
errorResponse,
25-
dtypeToOid,
2625
type FrontendMessage,
2726
} from "./protocol.js";
2827

src/pg-wire/protocol.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,18 @@ export function rowDescription(columns: { name: string; oid: number }[]): Uint8A
186186

187187
/** 'D' — DataRow */
188188
export function dataRow(values: (string | null)[]): Uint8Array {
189+
// Pre-encode to avoid double textEncoder.encode() per value
190+
const encoded = new Array<Uint8Array | null>(values.length);
189191
let bodyLen = 2; // field count
190-
for (const v of values) {
192+
for (let i = 0; i < values.length; i++) {
191193
bodyLen += 4; // length prefix per field
192-
if (v !== null) bodyLen += textEncoder.encode(v).length;
194+
if (values[i] !== null) {
195+
const bytes = textEncoder.encode(values[i]!);
196+
encoded[i] = bytes;
197+
bodyLen += bytes.length;
198+
} else {
199+
encoded[i] = null;
200+
}
193201
}
194202
const buf = new Uint8Array(1 + 4 + bodyLen);
195203
const dv = new DataView(buf.buffer);
@@ -198,11 +206,11 @@ export function dataRow(values: (string | null)[]): Uint8Array {
198206
dv.setInt16(5, values.length);
199207

200208
let pos = 7;
201-
for (const v of values) {
202-
if (v === null) {
209+
for (let i = 0; i < values.length; i++) {
210+
const bytes = encoded[i];
211+
if (bytes === null) {
203212
dv.setInt32(pos, -1); pos += 4; // NULL
204213
} else {
205-
const bytes = textEncoder.encode(v);
206214
dv.setInt32(pos, bytes.length); pos += 4;
207215
buf.set(bytes, pos); pos += bytes.length;
208216
}

0 commit comments

Comments
 (0)