Skip to content

perf: Build ORM query strings and result rows once - #6004

Open
abdelaziz-mahdy wants to merge 2 commits into
serverpod:mainfrom
abdelaziz-mahdy:perf/query-strings-and-rows
Open

abdelaziz-mahdy wants to merge 2 commits into
serverpod:mainfrom
abdelaziz-mahdy:perf/query-strings-and-rows

Conversation

@abdelaziz-mahdy

Copy link
Copy Markdown
Contributor

Each query rebuilt the same strings for every column of the table:

  • the quoted name (Column.toString);
  • queryAlias and fieldQueryAlias;
  • the table's queryPrefix.

hasBaseTable also compiled a new RegExp per column per query. Regular
expressions run in an interpreter in AOT. Columns and tables don't change once
created, so the strings are now built on first use. hasBaseTable is a
prefix comparison with the same matches.

Result rows went ResultRow.toColumnMap(), then a copy unwrapping
UndecodedBytes, then the alias-resolved map. The first two are now one map
built from the row values, with the same keys.

Part of #6002

Measurements

AOT, macOS arm64, retired CPU instructions per request, mean of 3 shuffled
runs, each change alone against main:

Request main strings once rows once
no database 402k -0.7% -0.3%
findById 975k -3.9% -0.4%
find, 20 rows 1,667k -3.7% -1.7%
insertRow 1,008k +1.0% (noise) -0.2%

Bold: every run below every run of main.

Tests

Full Serverpod test run on the combined branch (the PR branches are subsets): unit tests of serverpod, serverpod_serialization, serverpod_shared and serverpod_database pass; integration tests 1,683 (serial) + 529 (util/run_tests_integration_concurrently) pass; dart analyze is clean on this branch. No behaviour change, so no new tests. The existing query builder
and result parser tests cover the strings and maps.

How it was measured: the app is compiled AOT and run natively; for each request type, k6 sends 15 s at 1,000 req/s, and the server process's retired instructions (proc_pid_rusage(RUSAGE_INFO_V4)) before and after are divided by the number of requests. Builds of main and this PR were run alternately in shuffled order. I can share the scripts.

Pre-launch Checklist

  • I read the Contribute page and followed the process outlined there for submitting PRs.
  • This update contains only one single feature or bug fix and nothing else. (If you are submitting multiple fixes, please make multiple PRs.)
  • I read and followed the Dart Style Guide and formatted the code with dart format.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.
  • I added new tests to check the change I am making.
  • All existing and new tests are passing.
  • Any breaking changes are documented below.

Breaking changes

None.

Every query rebuilt the same strings for each column: its quoted name,
its aliases and its table's query prefix, and compiled a new RegExp per
column to check that it belongs to the queried table. Columns and tables
don't change once created, so the strings are now built on first use,
and the table check is a plain prefix comparison (same matches).
Rows went through ResultRow.toColumnMap(), then a copy that unwraps
UndecodedBytes, then the alias-resolved map. The first two are now one
map built from the row values with the same keys, and rows are
materialized once, so resolving includes doesn't rebuild them.
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