Skip to content

perf: memoize derived path/query projections on Uri and Url#52

Merged
OmarAlJarrah merged 1 commit into
mainfrom
perf/cache-derived-projections
Jul 8, 2026
Merged

perf: memoize derived path/query projections on Uri and Url#52
OmarAlJarrah merged 1 commit into
mainfrom
perf/cache-derived-projections

Conversation

@OmarAlJarrah

Copy link
Copy Markdown
Member

Summary

Uri and Url are immutable value objects, but a handful of their derived path/query projections were recomputed on every access, while their siblings on the same types (canonicalUri / canonicalHref, decodedPath, decodedPathSegments) were already memoized with by lazy. This backs the remaining projections with the same lazy caching.

Changes

  • Uri.queryParameters() re-parsed the raw query (QueryParameters.parseOrEmpty) on every call — so get, then getAll, then has re-parsed three times. Now reads a lazy backing snapshot.
  • Uri.encodedPath rebuilt the path string via a StringBuilder join over every segment on each access. Now lazy.
  • Url.pathSegments percent-decoded every segment on each access. Because fileName() reads pathSegments and fileExtension() calls fileName(), reading both decoded the path twice. Now lazy, so both share one decode.
  • Url.encodedPath and Url.queryParameters recomputed on each access as well, and are cached here too so both value types are consistent.

Each projection is now a private by lazy field, mirroring the existing canonicalUri / canonicalHref caches. Public signatures are unchanged.

Why it's safe

Every cached value is immutable: a String, a read-only segment List, or a QueryParameters snapshot whose entries are copied on construction. The types are immutable value objects, so caching a derived value cannot go stale — the change is behavior-preserving and observable only as fewer allocations and less CPU on repeated reads of the same value.

Verification

./gradlew :kuri:jvmTest :kuri:ktlintCheck :kuri:detekt :kuri:apiCheck — all green. No public API change, so the binary-compatibility snapshot is untouched.

Closes #35

Uri and Url are immutable value objects, yet several derived projections
were recomputed on every access while their siblings (canonicalUri /
canonicalHref, decodedPath, decodedPathSegments) were already memoized
with `by lazy`:

- Uri.queryParameters() re-parsed the raw query on each call.
- Uri.encodedPath re-joined every path segment on each access.
- Url.pathSegments percent-decoded every segment on each access, so
  fileName() / fileExtension() decoded the path twice.
- Url.encodedPath and Url.queryParameters recomputed likewise.

Back each with a private `by lazy` field, mirroring the existing
canonical/decoded caches. Public signatures are unchanged and every
cached value is immutable (a String, a read-only segment list, or the
QueryParameters snapshot whose entries are copied), so the change is
behavior-preserving and observable only as fewer allocations on
repeated reads of the same value.
@OmarAlJarrah OmarAlJarrah merged commit babc746 into main Jul 8, 2026
10 checks passed
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.

Cache derived path/query projections on Uri and Url

1 participant