Skip to content

perf: memoize import path resolution in CachedImporter#880

Open
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:perf/import-resolve-cache
Open

perf: memoize import path resolution in CachedImporter#880
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:perf/import-resolve-cache

Conversation

@He-Pin
Copy link
Copy Markdown
Contributor

@He-Pin He-Pin commented May 30, 2026

Motivation

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. On multi-file configs the same imports resolve repeatedly, re-running those stats.

Modification

  • Memoize resolution by (docBase, importName) in CachedImporter (mirrors the existing read cache). Resolution is deterministic within a run, so repeated imports / re-evaluated import exprs become a HashMap lookup, eliminating redundant filesystem stats. No semantic change.

Result

Scala Native, kube-prometheus (148 files), 25 interleaved runs, cooled:

min median mean
master 152.4 ms 156.6 ms 159.1 ms
this PR 149.2 ms 153.7 ms 156.3 ms

→ ~1.7–2% faster (all percentiles lower); output byte-identical.

Test plan

  • ./mill __.reformat
  • ./mill 'sjsonnet.jvm[3.3.7]'.test — 518/518 pass

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