Skip to content

Conversation

@e5l
Copy link
Member

@e5l e5l commented Jan 13, 2026

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.

  • Convert handleRoute to Unit return, eliminate Double boxing from suspend
  • Use parallel arrays (traitRoutes, traitParameters, traitQualities) for state
  • Reuse ParametersBuilder field to avoid allocation in findBestRoute
  • Create Success objects only when tracing enabled
  • Optimize PathSegmentTailcardRouteSelector to single-pass iteration
  • All algorithms and semantics preserved; all tests passing

@e5l e5l requested a review from Copilot January 13, 2026 15:59
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 13, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

Copilot AI left a 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 handleRoute from returning Double quality values to using a field-based state machine with Unit return type
  • Replaced ArrayList<Success> with parallel arrays (traitRoutes, traitParameters, traitQualities) to eliminate Success object allocations during backtracking
  • Optimized PathSegmentTailcardRouteSelector to use single-pass iteration instead of drop().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.

@bjhham
Copy link
Contributor

bjhham commented Jan 13, 2026

Looks like there's an increase in RoutingResolveContext and we may have missed a HttpProtocolVersion increase from earlier changes due to leniency in the assertion.

    Request consumes 20.24 KB, expected 18.81 KB. 
      Difference: 1464 < 2311 (allowed)
      Consumed 20.24 KB on request
      Expected 18.81 KB on request
      Extra    1464 bytes on request
    (See stdout + build/allocations/* files for details)
    
    Increased locations:
      HttpProtocolVersion.kt                         816    (816          -->         1632)
      RoutingResolveContext.kt                       632    (1432         -->         2064)
      RoutingRoot.kt                                  40    (344          -->          384)
      BaseApplicationEngine.kt                        16    (248          -->          264)
      BaseApplicationResponse.kt                       8    (248          -->          256)
    
    Decreased locations:
      RouteSelector.kt                               -48    (128          -->           80)

@e5l
Copy link
Member Author

e5l commented Jan 14, 2026

thanks, let me investigate other sites as well

@bjhham
Copy link
Contributor

bjhham commented Jan 15, 2026

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]>
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.

3 participants