perf: memoize import path resolution in CachedImporter#880
Open
He-Pin wants to merge 1 commit into
Open
Conversation
CachedImporter.read is cached but resolve() was not — it delegated straight to parent.resolve on every call. resolve() runs on every visitImport (before the evaluator's by-path Val cache is consulted) and each call stats candidate paths (docBase + every jpath) via os.isFile. Resolution is deterministic within a run, so memoizing it by (docBase, importName) turns repeated imports and re-evaluated import exprs into a HashMap lookup, eliminating redundant filesystem stats. Mirrors the existing read cache. Result (Scala Native, kube-prometheus, 25 interleaved runs, cooled): total_time min 152.4 -> 149.2 ms, mean 159.1 -> 156.3 ms (~1.7-2%); output byte-identical, 518/518 tests pass.
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.
Motivation
CachedImporter.readis cached, butresolve()was not — it delegated straight toparent.resolveon every call.resolve()runs on everyvisitImport(before the evaluator's by-pathValcache is consulted), and each call stats candidate paths (docBase+ every--jpath) viaos.isFile. On multi-file configs the same imports resolve repeatedly, re-running those stats.Modification
(docBase, importName)inCachedImporter(mirrors the existing read cache). Resolution is deterministic within a run, so repeated imports / re-evaluated import exprs become aHashMaplookup, eliminating redundant filesystem stats. No semantic change.Result
Scala Native, kube-prometheus (148 files), 25 interleaved runs, cooled:
→ ~1.7–2% faster (all percentiles lower); output byte-identical.
Test plan
./mill __.reformat./mill 'sjsonnet.jvm[3.3.7]'.test— 518/518 pass