Skip to content

perf: cut ~38 µs of CPU per request by building ShieldMiddleware once - #49

Merged
thetutlage merged 1 commit into
9.xfrom
perf/singleton-shield-middleware
Oct 12, 2026
Merged

thetutlage merged 1 commit into
9.xfrom
perf/singleton-shield-middleware

Conversation

@RomainLanz

Copy link
Copy Markdown
Member

Hi! 👋

Router middleware are resolved from the container on every request. The provider registered ShieldMiddleware with container.bind, so each request read the shield config, resolved the encryption service, ran await import('edge.js') and rebuilt every guard. The problem was found while profiling the AdonisJS Inertia React starter kit with 0x.

This PR registers the middleware with container.singleton. The middleware and its guards keep no per-request state. They only read and write the HTTP context. @adonisjs/session uses the same approach for SessionMiddleware.

Benchmark (Node 26.10, 2 vCPU, no profiler attached, 7 interleaved rounds, median)

before after change
resolver.make(ShieldMiddleware) 9.16 µs 1.06 µs −88%
GET through session + shield, server CPU 173.8 µs/req 136.2 µs/req −21.7%
GET through session + shield, throughput 5951 req/s 7520 req/s +26.4%

On the starter kit (authenticated Inertia navigation, ~800 µs CPU/req), the gain was ~33 µs per request (~4%).

Tests

tests/provider.spec.ts checks that:

  • the container and a per-request resolver return the same instance, and the middleware is built once;
  • across real HTTP requests, the middleware is built once, a GET gets the security headers and a CSRF token, and a POST without a token gets a 403.

Both tests fail with container.bind.

Behavior change

The shield config and the encryption service are now read on the first resolution only. If you change shield config with app.config.set() or swap encryption after the first request, the change has no effect. No test in this PR covers this case. The config is meant to be read at boot.

Router middleware are resolved from the container on every request.
Because the provider used `container.bind`, each request read the
shield config, resolved the encryption service, ran a dynamic
`import('edge.js')` and rebuilt every guard.

The middleware and its guards keep no per-request state (everything
goes through the HTTP context), so build them once with
`container.singleton`. On a minimal route that goes through session
and shield, server CPU drops from ~174 to ~136 µs per request.

The shield config and the encryption service are now read on the
first resolution only. Changing them at runtime after the first
request has no effect.
@thetutlage
thetutlage merged commit 428808f into 9.x Oct 12, 2026
10 checks passed
@thetutlage
thetutlage deleted the perf/singleton-shield-middleware branch October 12, 2026 12:18
@thetutlage

Copy link
Copy Markdown
Member

😀

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