Skip to content

Per-request CPU overhead in method calls and the Postgres ORM #6002

Open

Description

@abdelaziz-mahdy

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:

  1. Successful queries pass StackTrace.empty to logQuery. 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.
  2. The pool sets the driver's ignoreStackTraces (postgres >= 3.5.19). _query takes StackTrace.current in 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.
  3. Column/table strings are built once (columns and tables don't change once created), and hasBaseTable becomes a prefix comparison.
  4. Rows are mapped once.
  5. 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:

Are you interested in working on a PR for this?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions