Skip to content

perf(pg-protocol): decode short ascii values without buffer.toString() - #3773

Closed
nigrosimone wants to merge 1 commit into
brianc:masterfrom
nigrosimone:perf-decode-short-values
Closed

nigrosimone wants to merge 1 commit into
brianc:masterfrom
nigrosimone:perf-decode-short-values

Conversation

@nigrosimone

Copy link
Copy Markdown
Contributor

Every field of every row goes through BufferReader.string(), which calls buffer.toString('utf-8'). That call has a fixed cost in the C++ boundary that is bigger than the decoding itself when the value is short, and most values in a result set are short: ids, flags, small numbers, dates.

So for values up to 16 bytes that are all ASCII the string is now built with String.fromCharCode. Anything longer, or with a byte above 127, still goes to buffer.toString().

Parser only, 500 rows per message, median of 5 runs on node 26 (ms, lower is better):

5 short columns (id, fk, bool, int, 'ok'):   25.1 ->  8.3
5 columns with dates and codes:              28.4 -> 10.4
8 mixed columns (names, emails, timestamp):  39.1 -> 24.2
4 long text columns (37-80 chars):           22.2 -> 22.8
3 short non ascii columns:                   27.0 -> 27.6

End to end on localhost, SELECT of 5000 rows, 5 short columns: 9.1ms -> 6.7ms per query.

The test in inbound-parser.test.ts parses a data row with a value of every length from 0 to 19 plus accented, chinese and emoji values, and fails if the ascii check is removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant