-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Optimize routing resolution allocations with state machine refactoring #5294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR optimizes routing resolution in Ktor by reducing per-request memory allocations through a state machine refactoring. The optimization converts recursive parameter passing to field-based state management and replaces object allocations with primitive array operations.
Changes:
- Converted
handleRoutefrom returningDoublequality values to using a field-based state machine withUnitreturn type - Replaced
ArrayList<Success>with parallel arrays (traitRoutes,traitParameters,traitQualities) to eliminate Success object allocations during backtracking - Optimized
PathSegmentTailcardRouteSelectorto use single-pass iteration instead ofdrop().mapIndexed()chains
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| ktor-server/ktor-server-core/common/src/io/ktor/server/routing/RoutingResolveContext.kt | Core refactoring from recursive return values to state machine using parallel arrays for backtracking state; Success objects now created only when tracing is enabled |
| ktor-server/ktor-server-core/common/src/io/ktor/server/routing/RouteSelector.kt | Optimized PathSegmentTailcardRouteSelector to avoid intermediate list allocations using direct ArrayList construction |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Looks like there's an increase in |
|
thanks, let me investigate other sites as well |
|
Allocations are looking pretty good now 😄 |
…d IO improvements - Add ReusableSuspension for efficient coroutine suspension handling across platforms - Introduce RoutingResolveStatePool for object pooling to reduce allocations during routing - Optimize ByteChannel with improved buffer management - Enhance HttpProtocolVersion with optimized parsing - Improve StaticContentResolution with caching and path handling - Optimize NettyHttpResponsePipeline with better flush handling - Add CaseInsensitiveMap and Text util improvements - Optimize DateJvm parsing with cached formatters - Add tests for Netty pipeline bottleneck detection Co-Authored-By: Claude Opus 4.5 <[email protected]>
Reduce per-request allocations in RoutingResolveContext by converting from parameter/return recursion to field-based state machine with Unit return type. Replace ArrayList with parallel arrays for backtracking state. Optimize tailcard selector to eliminate intermediate list allocations.