Skip to content

perf: reduce allocation in expression binding and template parsing#297

Merged
zantvoort merged 1 commit into
mainfrom
perf/projection-template-allocation
Jul 20, 2026
Merged

perf: reduce allocation in expression binding and template parsing#297
zantvoort merged 1 commit into
mainfrom
perf/projection-template-allocation

Conversation

@zantvoort

Copy link
Copy Markdown
Collaborator

Part of #294 (the issue stays open for the remaining tail).

What

Follow-up to #296, targeting the two allocation sites JFR ranked highest after that merge.

Expression binding. bindObjectExpression allocated an ArrayList plus a name-keyed LinkedHashMap for every bound value, although single-column expressions, the vast majority, only need the one value. A reusable collector binds the scalar directly and materializes the map only when a second column appears (only multi-column expressions need it); the multi-values list is likewise created on demand. Version-aware binding locates the version column with a plain loop.

Template fragment parsing. Fragment lists are a pure function of the raw template string; interpolated values travel separately as \0 placeholders, so the key space is bounded by the application's distinct template shapes. Parsed fragments are now cached in a bounded LRU (shared immutable lists), in a package-private helper so nothing leaks into the TemplateBuilder interface surface.

Measured effect

JMH allocation profiler, per operation:

workload before this PR after cumulative vs pre-#296
singleRowById 6,944 B 6,546 B −38%
updateById 12,548 B 11,606 B −45%
multiStatement 12,486 B 11,586 B −44%
dynamic 23,299 B 22,249 B −17%

Precise counters on the by-id build phase: 4,131 → 3,693 B/op; the dynamically assembled projection's build: 6,682 → 5,796 B/op.

Testing

All suites green against their real databases: storm-core 2307 · H2 158 · SQLite 131 · PostgreSQL 178 · MySQL 175 · MariaDB 158, plus a full reactor test-compile. No API changes; binding semantics (including version-aware error paths and null-value comparison) are unchanged.

Object-expression binding allocated a list and a name-keyed map for every
bound value, although single-column expressions, the vast majority, only
need the one value. A reusable collector now binds the scalar directly and
materializes the map only when a second column appears; version-aware
binding locates the version column without stream machinery.

Template fragment lists are a pure function of the raw template string, and
interpolated values travel separately as placeholders, so parsed fragments
are now cached per template shape in a bounded LRU cache.

Allocation per operation drops a further 6-9% on the by-id read,
read-modify-update, and create-then-amend workloads and 5% on the
dynamically assembled projection, bringing the cumulative reduction since
the start of the build-path work to 38-45% and 17% respectively.

Refs #294
@zantvoort zantvoort added this to the 1.13.0 milestone Jul 20, 2026
@zantvoort
zantvoort merged commit 4a78680 into main Jul 20, 2026
7 checks passed
@zantvoort
zantvoort deleted the perf/projection-template-allocation branch July 20, 2026 19:04
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