Repository navigation
Description
Problem to Solve
I measured where a minimal Serverpod 4.1 app spends its CPU per request: the Serverpod entry of backend-benchmark, with Note.db.findById, Note.db.find (20 rows), Note.db.insertRow and an endpoint without the database. I compiled it AOT, counted retired CPU instructions per request with the CPU's counters (proc_pid_rusage), and attributed cycles to functions from symbolized samples (Instruments' CPU Profiler + package:native_stack_traces).
Some per-request costs buy nothing unless something is logged or fails:
Cost per findById call |
Share of CPU | Where |
|---|---|---|
StackTrace.current per statement, captured by the driver for its errors |
~10% | postgres driver, enabled by default |
StackTrace.current per query for the session log, mostly dropped |
~2% | PostgresDatabaseConnection._logQuery |
Column and table SQL strings rebuilt per query, plus a new RegExp per column (hasBaseTable) |
~4% | SelectQueryBuilder |
| Rows copied through three maps | ~2% | _mappedResultsQuery |
| Secure-random session UUID, session log open event | ~1-2% | Session, SessionLogManager |
StackTrace.current is expensive in AOT: the VM walks the async awaiter chain. RegExps run in an interpreter in AOT.
Under overload, once latency passes the 1 s slow-session threshold, every session and query is logged. VmServiceSessionLogWriter then builds maps, timestamps and stack trace strings for events that postEvent drops when there's no VM service. In one overload profile that was ~16% of CPU, which makes the overload worse.
Proposal
Each change is small and keeps behaviour:
- Successful queries pass
StackTrace.emptytologQuery. The session log captures the trace only when it records the query; it runs synchronously from the query, so the caller's frames are kept. - The pool sets the driver's
ignoreStackTraces(postgres >= 3.5.19)._querytakesStackTrace.currentin its catch blocks instead, which is the awaiter chain of the code that ran the query. Errors keep the caller's frames, and only failed queries pay. A new test asserts this. - Column/table strings are built once (columns and tables don't change once created), and
hasBaseTablebecomes a prefix comparison. - Rows are mapped once.
- The session id, the session log open event and VM-service log events are created only when used.
Use Case
Every Serverpod app that uses the database: these costs are on every method call and every query.
Retired instructions per request (AOT, macOS arm64, mean of 3 shuffled runs; noise about 2%):
| Request | 4.1.1 main |
All five changes | Change |
|---|---|---|---|
| no database | 402k | 386k | -3.9% |
findById |
975k | 831k | -14.8% |
find, 20 rows |
1,667k | 1,457k | -12.6% |
insertRow |
1,008k | 902k | -10.5% |
Throughput in the benchmark's Docker harness (app on 2 vCPUs, highest rate held at p99 < 100 ms), 3 rounds, medians, items 1 and 2 alone:
| Scenario | main |
Items 1+2 | Change |
|---|---|---|---|
| no database | 21,000 | 21,000 | 0% |
findById |
5,750 | 6,250 | +8.7% |
insertRow |
6,250 | 7,000 | +12.0% |
| mixed | 6,000 | 6,750 | +12.5% |
Alternatives
Making StackTrace.current cheaper in the VM helps too (I'm proposing a VM change that cuts it by ~23%), but not capturing an unused trace is still cheaper. The postgres driver and Relic have their own costs on this path, which I'm proposing to those projects.
Additional context
PRs, one per change set, each with its own measurements:
- perf: Stop capturing a stack trace for every database query #6003 Query stack traces (items 1 and 2)
- perf: Build ORM query strings and result rows once #6004 Query strings and rows (items 3 and 4)
- perf: Skip session work that is only needed when a session is logged #6005 Lazy session work (item 5)
Are you interested in working on a PR for this?
- I want to work on this
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsInbox 📫
Activity