Repository navigation
perf: cut ~38 µs of CPU per request by building ShieldMiddleware once - #49
Merged
Merged
Conversation
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.
Member
|
😀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi! 👋
Router middleware are resolved from the container on every request. The provider registered
ShieldMiddlewarewithcontainer.bind, so each request read the shield config, resolved the encryption service, ranawait 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/sessionuses the same approach forSessionMiddleware.Benchmark (Node 26.10, 2 vCPU, no profiler attached, 7 interleaved rounds, median)
resolver.make(ShieldMiddleware)On the starter kit (authenticated Inertia navigation, ~800 µs CPU/req), the gain was ~33 µs per request (~4%).
Tests
tests/provider.spec.tschecks that: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
shieldconfig withapp.config.set()or swapencryptionafter 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.