Skip to content

[performance] Reduce per-request overhead in hot path#2559

Merged
tipsy merged 4 commits into
masterfrom
javalin-performance-v2
Mar 13, 2026
Merged

[performance] Reduce per-request overhead in hot path#2559
tipsy merged 4 commits into
masterfrom
javalin-performance-v2

Conversation

@tipsy

@tipsy tipsy commented Mar 13, 2026

Copy link
Copy Markdown
Member

Summary

Surgical optimizations to Javalin's request lifecycle hot path — 8 files changed, +55/-19 lines, all internal with no public API modifications.

  • Cache route match resultfindHttpHandlerEntries is called once per request instead of 3-4 times, cached in a private field on JavalinServletContext (not request attributes)
  • Single-pass path param extraction — new matchAndExtract() combines match + extract in one regex execution instead of three
  • Zero-allocation content-type checksignoreCase = true instead of lowercase() string allocation
  • ArrayList over LinkedList — better cache locality for the small endpoint stack

Test plan

  • Full test suite passes (mvn test -pl javalin — 972 tests, 0 failures)

🤖 Generated with Claude Code

Cache route match result across lifecycle phases using private fields
on JavalinServletContext, add single-pass matchAndExtract() to PathParser,
replace lowercase() allocations with ignoreCase comparisons, and swap
LinkedList for ArrayList in Endpoints. All changes are internal — no
public API modifications.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@codecov

codecov Bot commented Mar 13, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.30435% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.24%. Comparing base (1e59eda) to head (fb05b96).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
javalin/src/main/java/io/javalin/http/Context.kt 50.00% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #2559      +/-   ##
============================================
- Coverage     86.29%   86.24%   -0.05%     
+ Complexity     1509     1507       -2     
============================================
  Files           155      155              
  Lines          4348     4356       +8     
  Branches        523      525       +2     
============================================
+ Hits           3752     3757       +5     
- Misses          363      366       +3     
  Partials        233      233              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

- Add `inline` to cachedHttpHandler/cachedWillMatch to eliminate lambda allocations
- Use primitive Int (0/1/-1) for boolean cache to avoid boxing overhead
- Use @PublishedApi for UNSET sentinel to support inline access

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@tipsy tipsy force-pushed the javalin-performance-v2 branch from 08bb8a4 to d8cf339 Compare March 13, 2026 21:21
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@tipsy

tipsy commented Mar 13, 2026

Copy link
Copy Markdown
Member Author

augment review

@augmentcode

augmentcode Bot commented Mar 13, 2026

Copy link
Copy Markdown
🤖 Augment PR Summary

Summary: Reduces per-request overhead in Javalin’s request lifecycle hot path by avoiding repeated route lookups and string allocations.

Changes:

  • Caches the matched ParsedEndpoint and the willMatch decision on JavalinServletContext to reuse results across lifecycle phases
  • Derives route roles directly from the cached handler metadata (removing extra router queries)
  • Uses case-insensitive prefix checks for content-type and accept-encoding parsing to avoid lowercase() allocations
  • Switches the internal endpoint stack from LinkedList to a small pre-sized ArrayList

Tests: Adds a lifecycle test intended to cover cache hit and miss paths.

🤖 Was this summary useful? React with 👍 or 👎

@augmentcode augmentcode Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review completed. 1 suggestion posted.

Fix All in Augment

Comment augment review to trigger a new review at any time.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@tipsy

tipsy commented Mar 13, 2026

Copy link
Copy Markdown
Member Author

augment review

@augmentcode augmentcode Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review completed. 1 suggestion posted.

Fix All in Augment

Comment augment review to trigger a new review at any time.

fun `router heavily exercises caching across lifecycle phases`() = TestUtil.test(Javalin.create { cfg ->
// Setup before/after filters to force the router to evaluate the endpoint multiple times
// during the same request lifecycle, triggering the cache hits.
cfg.routes.before("/*") { ctx -> ctx.attribute("req_start", true) }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test sets up routes.before/routes.after (BEFORE/AFTER), which won’t necessarily evaluate the new cachedWillMatch logic that’s only used in BEFORE_MATCHED/AFTER_MATCHED. If the goal is to exercise caching “across lifecycle phases”, consider ensuring the test actually triggers BEFORE_MATCHED/AFTER_MATCHED behavior so the cache paths are covered.

Severity: low

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

@tipsy tipsy merged commit f6a1e40 into master Mar 13, 2026
16 checks passed
@tipsy tipsy deleted the javalin-performance-v2 branch March 13, 2026 22:05
mergify Bot added a commit to robfrank/linklift that referenced this pull request Mar 19, 2026
Bumps `javalin.version` from 7.0.1 to 7.1.0.
Updates `io.javalin:javalin-bundle` from 7.0.1 to 7.1.0
Release notes

*Sourced from [io.javalin:javalin-bundle's releases](https://github.com/javalin/javalin/releases).*

> 7.1.0
> -----
>
> What's Changed
> --------------
>
> * Bump com.fasterxml.jackson.core:jackson-core from 2.21.0 to 2.21.1 by [`@​dependabot`](https://github.com/dependabot)[bot] in [javalin/javalin#2543](https://redirect.github.com/javalin/javalin/pull/2543)
> * [deps]: Bump the dependencies group across 1 directory with 17 updates by [`@​dependabot`](https://github.com/dependabot)[bot] in [javalin/javalin#2545](https://redirect.github.com/javalin/javalin/pull/2545)
> * [websocket] Fix WebSocket over HTTP/2 Extended Connect (RFC 8441) by [`@​tipsy`](https://github.com/tipsy) in [javalin/javalin#2547](https://redirect.github.com/javalin/javalin/pull/2547)
> * fix(README): update artifactId for SSL plugin in `README.md` by [`@​yvasyliev`](https://github.com/yvasyliev) in [javalin/javalin#2556](https://redirect.github.com/javalin/javalin/pull/2556)
> * Add `wsExceptionHandler` to `MicrometerPlugin` for WebSocket exception tagging by [`@​Copilot`](https://github.com/Copilot) in [javalin/javalin#2557](https://redirect.github.com/javalin/javalin/pull/2557)
> * [rendering] jte - introduce directoryTemplateEngine for (6.x-like), rename default to classPathTemplateEngine by [`@​elwin013`](https://github.com/elwin013) in [javalin/javalin#2551](https://redirect.github.com/javalin/javalin/pull/2551)
> * Add javalin-bom module by [`@​Copilot`](https://github.com/Copilot) in [javalin/javalin#2558](https://redirect.github.com/javalin/javalin/pull/2558)
> * [performance] Reduce per-request overhead in hot path by [`@​tipsy`](https://github.com/tipsy) in [javalin/javalin#2559](https://redirect.github.com/javalin/javalin/pull/2559)
> * [performance] Eliminate per-request regex and allocation overhead by [`@​tipsy`](https://github.com/tipsy) in [javalin/javalin#2560](https://redirect.github.com/javalin/javalin/pull/2560)
>
> New Contributors
> ----------------
>
> * [`@​yvasyliev`](https://github.com/yvasyliev) made their first contribution in [javalin/javalin#2556](https://redirect.github.com/javalin/javalin/pull/2556)
> * [`@​elwin013`](https://github.com/elwin013) made their first contribution in [javalin/javalin#2551](https://redirect.github.com/javalin/javalin/pull/2551)
>
> **Full Changelog**: <javalin/javalin@javalin-parent-7.0.1...javalin-parent-7.1.0>


Commits

* [`e458dba`](javalin/javalin@e458dba) [maven-release-plugin] prepare for next development iteration
* [`9766d2f`](javalin/javalin@9766d2f) [maven-release-plugin] prepare release javalin-parent-7.1.0
* [`12acf0b`](javalin/javalin@12acf0b) [performance] Eliminate per-request regex and allocation overhead ([#2560](https://redirect.github.com/javalin/javalin/issues/2560))
* [`f6a1e40`](javalin/javalin@f6a1e40) [performance] Reduce per-request overhead in hot path
* [`1e59eda`](javalin/javalin@1e59eda) [pom] Add javalin-bom module ([#2558](https://redirect.github.com/javalin/javalin/issues/2558))
* [`b43f37a`](javalin/javalin@b43f37a) [rendering] jte - introduce directoryTemplateEngine for (6.x-like behavior)
* [`72b2320`](javalin/javalin@72b2320) [micrometer] Add `wsExceptionHandler` to `MicrometerPlugin`
* [`37c91c4`](javalin/javalin@37c91c4) [readme] Update artifactId for SSL plugin
* [`9acf45e`](javalin/javalin@9acf45e) [cleanup] Use Header constants
* [`0289084`](javalin/javalin@0289084) [headers] Add missing headers to Header.kt
* Additional commits viewable in [compare view](javalin/javalin@javalin-parent-7.0.1...7.1.0)
  
Updates `io.javalin.community.openapi:javalin-openapi-plugin` from 7.0.1 to 7.1.0
Release notes

*Sourced from [io.javalin.community.openapi:javalin-openapi-plugin's releases](https://github.com/javalin/javalin-openapi/releases).*

> 7.1.0
> -----
>
> **Changes**
>
> * [javalin/javalin-openapi#275](https://redirect.github.com/javalin/javalin-openapi/issues/275) [Make ClassLoader used for loading resources configurable](javalin/javalin-openapi@de1b6b7)
> * Support Javalin 7.1.0
>
> **Sponsors**
> Thanks to everyone who supported me this month 💜
>
> **Minimal requirements**
>
> * Java 17+ / Kotlin 2.3+
> * Javalin 7.1.0


Commits

* [`5388862`](javalin/javalin-openapi@5388862) [GH-275](https://redirect.github.com/javalin/javalin-openapi/issues/275) Cover missing resource loaders and release 7.1.1-rc.1
* [`cbbc8d0`](javalin/javalin-openapi@cbbc8d0) [GH-276](https://redirect.github.com/javalin/javalin-openapi/issues/276) Release 7.1.0 (Resolves [#276](https://redirect.github.com/javalin/javalin-openapi/issues/276))
* [`de1b6b7`](javalin/javalin-openapi@de1b6b7) [GH-275](https://redirect.github.com/javalin/javalin-openapi/issues/275) Make ClassLoader used for loading resources configurable (Resolves [#275](https://redirect.github.com/javalin/javalin-openapi/issues/275))
* See full diff in [compare view](javalin/javalin-openapi@7.0.1...7.1.0)
  
Updates `io.javalin.community.openapi:javalin-swagger-plugin` from 7.0.1 to 7.1.0
Release notes

*Sourced from [io.javalin.community.openapi:javalin-swagger-plugin's releases](https://github.com/javalin/javalin-openapi/releases).*

> 7.1.0
> -----
>
> **Changes**
>
> * [javalin/javalin-openapi#275](https://redirect.github.com/javalin/javalin-openapi/issues/275) [Make ClassLoader used for loading resources configurable](javalin/javalin-openapi@de1b6b7)
> * Support Javalin 7.1.0
>
> **Sponsors**
> Thanks to everyone who supported me this month 💜
>
> **Minimal requirements**
>
> * Java 17+ / Kotlin 2.3+
> * Javalin 7.1.0


Commits

* [`5388862`](javalin/javalin-openapi@5388862) [GH-275](https://redirect.github.com/javalin/javalin-openapi/issues/275) Cover missing resource loaders and release 7.1.1-rc.1
* [`cbbc8d0`](javalin/javalin-openapi@cbbc8d0) [GH-276](https://redirect.github.com/javalin/javalin-openapi/issues/276) Release 7.1.0 (Resolves [#276](https://redirect.github.com/javalin/javalin-openapi/issues/276))
* [`de1b6b7`](javalin/javalin-openapi@de1b6b7) [GH-275](https://redirect.github.com/javalin/javalin-openapi/issues/275) Make ClassLoader used for loading resources configurable (Resolves [#275](https://redirect.github.com/javalin/javalin-openapi/issues/275))
* See full diff in [compare view](javalin/javalin-openapi@7.0.1...7.1.0)
  
Updates `io.javalin:javalin-micrometer` from 7.0.1 to 7.1.0
Release notes

*Sourced from [io.javalin:javalin-micrometer's releases](https://github.com/javalin/javalin/releases).*

> 7.1.0
> -----
>
> What's Changed
> --------------
>
> * Bump com.fasterxml.jackson.core:jackson-core from 2.21.0 to 2.21.1 by [`@​dependabot`](https://github.com/dependabot)[bot] in [javalin/javalin#2543](https://redirect.github.com/javalin/javalin/pull/2543)
> * [deps]: Bump the dependencies group across 1 directory with 17 updates by [`@​dependabot`](https://github.com/dependabot)[bot] in [javalin/javalin#2545](https://redirect.github.com/javalin/javalin/pull/2545)
> * [websocket] Fix WebSocket over HTTP/2 Extended Connect (RFC 8441) by [`@​tipsy`](https://github.com/tipsy) in [javalin/javalin#2547](https://redirect.github.com/javalin/javalin/pull/2547)
> * fix(README): update artifactId for SSL plugin in `README.md` by [`@​yvasyliev`](https://github.com/yvasyliev) in [javalin/javalin#2556](https://redirect.github.com/javalin/javalin/pull/2556)
> * Add `wsExceptionHandler` to `MicrometerPlugin` for WebSocket exception tagging by [`@​Copilot`](https://github.com/Copilot) in [javalin/javalin#2557](https://redirect.github.com/javalin/javalin/pull/2557)
> * [rendering] jte - introduce directoryTemplateEngine for (6.x-like), rename default to classPathTemplateEngine by [`@​elwin013`](https://github.com/elwin013) in [javalin/javalin#2551](https://redirect.github.com/javalin/javalin/pull/2551)
> * Add javalin-bom module by [`@​Copilot`](https://github.com/Copilot) in [javalin/javalin#2558](https://redirect.github.com/javalin/javalin/pull/2558)
> * [performance] Reduce per-request overhead in hot path by [`@​tipsy`](https://github.com/tipsy) in [javalin/javalin#2559](https://redirect.github.com/javalin/javalin/pull/2559)
> * [performance] Eliminate per-request regex and allocation overhead by [`@​tipsy`](https://github.com/tipsy) in [javalin/javalin#2560](https://redirect.github.com/javalin/javalin/pull/2560)
>
> New Contributors
> ----------------
>
> * [`@​yvasyliev`](https://github.com/yvasyliev) made their first contribution in [javalin/javalin#2556](https://redirect.github.com/javalin/javalin/pull/2556)
> * [`@​elwin013`](https://github.com/elwin013) made their first contribution in [javalin/javalin#2551](https://redirect.github.com/javalin/javalin/pull/2551)
>
> **Full Changelog**: <javalin/javalin@javalin-parent-7.0.1...javalin-parent-7.1.0>


Commits

* [`e458dba`](javalin/javalin@e458dba) [maven-release-plugin] prepare for next development iteration
* [`9766d2f`](javalin/javalin@9766d2f) [maven-release-plugin] prepare release javalin-parent-7.1.0
* [`12acf0b`](javalin/javalin@12acf0b) [performance] Eliminate per-request regex and allocation overhead ([#2560](https://redirect.github.com/javalin/javalin/issues/2560))
* [`f6a1e40`](javalin/javalin@f6a1e40) [performance] Reduce per-request overhead in hot path
* [`1e59eda`](javalin/javalin@1e59eda) [pom] Add javalin-bom module ([#2558](https://redirect.github.com/javalin/javalin/issues/2558))
* [`b43f37a`](javalin/javalin@b43f37a) [rendering] jte - introduce directoryTemplateEngine for (6.x-like behavior)
* [`72b2320`](javalin/javalin@72b2320) [micrometer] Add `wsExceptionHandler` to `MicrometerPlugin`
* [`37c91c4`](javalin/javalin@37c91c4) [readme] Update artifactId for SSL plugin
* [`9acf45e`](javalin/javalin@9acf45e) [cleanup] Use Header constants
* [`0289084`](javalin/javalin@0289084) [headers] Add missing headers to Header.kt
* Additional commits viewable in [compare view](javalin/javalin@javalin-parent-7.0.1...7.1.0)
  
Updates `io.javalin:javalin-testtools` from 7.0.1 to 7.1.0
Release notes

*Sourced from [io.javalin:javalin-testtools's releases](https://github.com/javalin/javalin/releases).*

> 7.1.0
> -----
>
> What's Changed
> --------------
>
> * Bump com.fasterxml.jackson.core:jackson-core from 2.21.0 to 2.21.1 by [`@​dependabot`](https://github.com/dependabot)[bot] in [javalin/javalin#2543](https://redirect.github.com/javalin/javalin/pull/2543)
> * [deps]: Bump the dependencies group across 1 directory with 17 updates by [`@​dependabot`](https://github.com/dependabot)[bot] in [javalin/javalin#2545](https://redirect.github.com/javalin/javalin/pull/2545)
> * [websocket] Fix WebSocket over HTTP/2 Extended Connect (RFC 8441) by [`@​tipsy`](https://github.com/tipsy) in [javalin/javalin#2547](https://redirect.github.com/javalin/javalin/pull/2547)
> * fix(README): update artifactId for SSL plugin in `README.md` by [`@​yvasyliev`](https://github.com/yvasyliev) in [javalin/javalin#2556](https://redirect.github.com/javalin/javalin/pull/2556)
> * Add `wsExceptionHandler` to `MicrometerPlugin` for WebSocket exception tagging by [`@​Copilot`](https://github.com/Copilot) in [javalin/javalin#2557](https://redirect.github.com/javalin/javalin/pull/2557)
> * [rendering] jte - introduce directoryTemplateEngine for (6.x-like), rename default to classPathTemplateEngine by [`@​elwin013`](https://github.com/elwin013) in [javalin/javalin#2551](https://redirect.github.com/javalin/javalin/pull/2551)
> * Add javalin-bom module by [`@​Copilot`](https://github.com/Copilot) in [javalin/javalin#2558](https://redirect.github.com/javalin/javalin/pull/2558)
> * [performance] Reduce per-request overhead in hot path by [`@​tipsy`](https://github.com/tipsy) in [javalin/javalin#2559](https://redirect.github.com/javalin/javalin/pull/2559)
> * [performance] Eliminate per-request regex and allocation overhead by [`@​tipsy`](https://github.com/tipsy) in [javalin/javalin#2560](https://redirect.github.com/javalin/javalin/pull/2560)
>
> New Contributors
> ----------------
>
> * [`@​yvasyliev`](https://github.com/yvasyliev) made their first contribution in [javalin/javalin#2556](https://redirect.github.com/javalin/javalin/pull/2556)
> * [`@​elwin013`](https://github.com/elwin013) made their first contribution in [javalin/javalin#2551](https://redirect.github.com/javalin/javalin/pull/2551)
>
> **Full Changelog**: <javalin/javalin@javalin-parent-7.0.1...javalin-parent-7.1.0>


Commits

* [`e458dba`](javalin/javalin@e458dba) [maven-release-plugin] prepare for next development iteration
* [`9766d2f`](javalin/javalin@9766d2f) [maven-release-plugin] prepare release javalin-parent-7.1.0
* [`12acf0b`](javalin/javalin@12acf0b) [performance] Eliminate per-request regex and allocation overhead ([#2560](https://redirect.github.com/javalin/javalin/issues/2560))
* [`f6a1e40`](javalin/javalin@f6a1e40) [performance] Reduce per-request overhead in hot path
* [`1e59eda`](javalin/javalin@1e59eda) [pom] Add javalin-bom module ([#2558](https://redirect.github.com/javalin/javalin/issues/2558))
* [`b43f37a`](javalin/javalin@b43f37a) [rendering] jte - introduce directoryTemplateEngine for (6.x-like behavior)
* [`72b2320`](javalin/javalin@72b2320) [micrometer] Add `wsExceptionHandler` to `MicrometerPlugin`
* [`37c91c4`](javalin/javalin@37c91c4) [readme] Update artifactId for SSL plugin
* [`9acf45e`](javalin/javalin@9acf45e) [cleanup] Use Header constants
* [`0289084`](javalin/javalin@0289084) [headers] Add missing headers to Header.kt
* Additional commits viewable in [compare view](javalin/javalin@javalin-parent-7.0.1...7.1.0)
  
Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.
[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)
---
Dependabot commands and options
  
You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot show  ignore conditions` will show all of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
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.

1 participant