perf: memoize derived path/query projections on Uri and Url#52
Merged
Conversation
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.
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.
Summary
UriandUrlare 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 withby lazy. This backs the remaining projections with the same lazy caching.Changes
Uri.queryParameters()re-parsed the raw query (QueryParameters.parseOrEmpty) on every call — soget, thengetAll, thenhasre-parsed three times. Now reads a lazy backing snapshot.Uri.encodedPathrebuilt the path string via aStringBuilderjoin over every segment on each access. Now lazy.Url.pathSegmentspercent-decoded every segment on each access. BecausefileName()readspathSegmentsandfileExtension()callsfileName(), reading both decoded the path twice. Now lazy, so both share one decode.Url.encodedPathandUrl.queryParametersrecomputed on each access as well, and are cached here too so both value types are consistent.Each projection is now a private
by lazyfield, mirroring the existingcanonicalUri/canonicalHrefcaches. Public signatures are unchanged.Why it's safe
Every cached value is immutable: a
String, a read-only segmentList, or aQueryParameterssnapshot 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