Skip to content

perf(pglite): reduce per-row allocation in parseResults#1050

Open
huntiezz wants to merge 1 commit into
electric-sql:mainfrom
huntiezz:perf/optimize-parse-results
Open

perf(pglite): reduce per-row allocation in parseResults#1050
huntiezz wants to merge 1 commit into
electric-sql:mainfrom
huntiezz:perf/optimize-parse-results

Conversation

@huntiezz

Copy link
Copy Markdown

Summary

Optimizes the hottest JS-side path in PGlite — converting PostgreSQL wire-protocol DataRow messages into JavaScript objects/arrays in parseResults().

Problem: Every row previously allocated intermediate structures:

  • Object mode (default): Object.fromEntries(msg.fields.map(...)) — builds a [key, value][] array per row, then iterates it again
  • Array mode: msg.fields.map(...) — callback-based allocation per row

Field names and type OIDs were also re-read from currentResultSet.fields[i] on every row even though they're constant for the entire result set.

Fix:

  1. Hoist fieldNames[] and fieldTypes[] once per result set on rowDescription
  2. Build object-mode rows with a tight indexed for loop (no Object.fromEntries)
  3. Preallocate array-mode rows with new Array(n) and fill by index
  4. Reset hoisted metadata on commandComplete

This is a behavior-preserving micro-optimization — a natural follow-up to #495, focused on large result-set workloads (#630). Does not overlap with maintainer drafts #973 (parse redesign) or #903 (streaming parse).

Test plan

  • New unit tests in packages/pglite/tests/parse.test.ts (9 cases: object/array modes, multi-result sets, INSERT/UPDATE/DELETE counts, custom parsers, blob attachment, describe helper)
  • pnpm vitest tests/parse.test.ts — 9 passed
  • pnpm test:basic — 278 passed (57 files)
  • pnpm stylecheck && pnpm typecheck — clean
  • Changeset added for @electric-sql/pglite patch

Hoist field names and type OIDs once per result set on rowDescription,
then build rows with indexed loops instead of Object.fromEntries and
per-row .map(). Adds unit tests for parseResults covering object/array
row modes, multi-result sets, and custom parsers.
@huntiezz huntiezz force-pushed the perf/optimize-parse-results branch from add5c8e to 2433e52 Compare July 12, 2026 11:56
@AntonOfTheWoods

Copy link
Copy Markdown
Contributor

@huntiezz you mention "optimize" - do you have any benchmarks showing what the actual gains might be?

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.

2 participants