[2.x] Intern directory item conversions in MappedFileConverter#1751
Merged
Conversation
MappedFileConverter
hoangmaihuy
force-pushed
the
perf/file-interning
branch
from
July 23, 2026 09:58
fc28c0d to
f1bb914
Compare
Member
|
Thanks for the contribution! |
eed3si9n
reviewed
Jul 23, 2026
Every consumer of a shared classpath converts the same entries independently; for a directory entry the conversion wraps every contained file, so a shared classes directory is re-materialized - value-identical MappedVirtualFiles, encoded-path strings included - once per consumer. On large multi-project builds these duplicates multiply into a significant heap cost held across the consumers' CompileOptions. Cache the per-item conversions in MappedFileConverter, keyed by the file's (lastModified, size) and held through a SoftReference, following the metadata-keyed caching of ClasspathCache: - Consumers sharing a classpath now share one MappedVirtualFile per contained file. A cached item is served only while its (lastModified, size) is unchanged, so its memoized content hashes are never served across a detected content change. - Directory conversions are still rebuilt from a fresh listing on every call, so directory membership stays current; only the items are shared. - The SoftReference bounds retention: while a live conversion holds an item the reference cannot clear, so simultaneously-held conversions keep sharing one instance, but once no conversion holds it the GC may reclaim it under memory pressure (it is rebuilt on the next conversion). - Top-level conversions are intentionally left uncached: consumers hold a source conversion across edits and rely on its lazy content hash reading the content of first access, so those keep fresh-instance semantics. Adds MappedFileConverterInterningSpec and a FileConverterBenchmark measuring conversion time and retained heap. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
hoangmaihuy
force-pushed
the
perf/file-interning
branch
from
July 24, 2026 02:39
de0a1fb to
7fd60a3
Compare
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.
Disclaimer: Developed with Claude Code and human review in the loop
Fixes #1750
Problem
MappedFileConverter.toVirtualFileexpands a directory classpath entry into oneMappedVirtualFileper contained file. Because every consumer converts its classpathindependently and the
FileConverteris shared build-wide, a directory that appears on manyconsumers' classpaths is re-materialized once per consumer — producing large numbers of
value-identical
MappedVirtualFileinstances (encoded-path strings included) retainedsimultaneously. See #1750 for the full write-up.
Change
MappedFileConverter, keyed by the file's(lastModified, size), mirroring the metadata-keyed caching already used byClasspathCache. Consumers sharing a classpath now share oneMappedVirtualFilepercontained file instead of each re-materializing a copy.
items are shared. Directory membership is therefore always current (added/removed files
are reflected), and each item is revalidated against its
(lastModified, size)before reuse.SoftReference: while any live conversion references an item thereference cannot clear (simultaneously-held conversions keep sharing one instance); once no
conversion holds it, the GC may reclaim it under memory pressure, bounding the retention of a
long-lived shared converter. A reclaimed item is simply rebuilt on the next conversion.
conversion across edits and rely on its lazy
contentHashreading the content of firstaccess; interning those would fuse instances whose laziness must stay independent and break
incremental source change detection.
Why it's safe
contentHashonly on top-level source files (fresh instances). Classpath/library changedetection stamps by path (
forHashInRootPaths→FarmHash.ofPath), re-resolving classfiles rather than reading
MappedDirectory.items— even for directory classpath entries.So sharing item instances does not affect incremental correctness.
(lastModified, size)is unchanged, so a memoizedcontentHash/contentHashStris never served across a detected content change (the sameheuristic
ClasspathCachealready relies on).is bounded by the converter's lifetime, and further bounded under pressure by the soft
references.
Results
Micro-benchmark (
FileConverterBenchmark, JMH; 2000-file directory, 100 consumers holdingconversions simultaneously): ~12% faster average conversion and ~4.5× less retained heap
(26.8 MB → 5.9 MB) for a shared converter vs a fresh converter per consumer.
Macro (real clean compile — fresh empty action cache, wiped target + boot, ~300-subproject
build, 132,362 class files):
jstatsample distribution corroborates the ordering (the interning build never exceeded16 GB; stock touched 16.4 GB).
Caveat: one run per build; wall time and momentary heap peaks are both timing-noisy (treat
exact figures as ±a few percent), but the direction — lower peak heap and lower wall time — is
consistent across in-JVM peak, jstat peak, the distribution, and wall clock.
Tests
MappedFileConverterInterningSpeccovers:🤖 Generated with Claude Code