JAR reading: more accurate and widespread caching, less read contention#9557
Merged
lrytz merged 4 commits intoscala:2.12.xfrom May 18, 2021
Merged
JAR reading: more accurate and widespread caching, less read contention#9557lrytz merged 4 commits intoscala:2.12.xfrom
lrytz merged 4 commits intoscala:2.12.xfrom
Conversation
Member
|
bc4baed to
80a942d
Compare
Member
|
What are the downsides of this option that prevent using it by default? |
Member
Author
|
Nothing major other than the risk of new code. I'd like to battle test it to flush out further bugs and refine the performance. I'm not sure if it is shipped with non-OpenJDK based distros, so we might need the old implementation as a fallback. |
Member
Author
|
A clarification: |
Member
Author
|
My earlier analysis was wrong, I do see contention on reads in this implementation, which is visible in my multi-threaded compilation benchmark. The improvement that I thought I saw might have been due to #9549 instead. |
Member
Author
|
Update:
|
This prevents concurrent compilers with different values for this compiler option from seeing the incorrect API.
Classpath elements based on a) jrt:// file system (representing platform libraries of the current the Java 9+ instance) and b) ct.sym (the JEP 247 repository of the of previous JDK versions) are an immutable part of the JDK. The ClassPath entries we create are safe to share across concurrent or subsequent compilers in the same way we cache entries for regular JARs.
Classpath caching shares a single instance of ZipArchive across multiple threads. This can cause read contention as j.u.ZipFile internally serializes reads. Instead, maintain a pool of ZipFile instances to avoid sharing them across threads.
The current optimized version tries to avoid temporary strings. But it doesn't achieve this for classes based by jrt:// (or any `NioPath`, as the call to `AbstractFile.fileName` internally constructs a string each time. This commit uses `.name` (which is a `lazy val`).
lrytz
approved these changes
May 18, 2021
retronym
added a commit
to retronym/scala
that referenced
this pull request
Jun 22, 2021
The compiler has a per-classloader cache that backs the classpath lookups of individual instances of `Global`. Elements in the cache are used by `Global` instances that are concurrent (think parallel compilation of sub-projects) or are sequential within a small timeout. In scala#9557, this was extended to the classpath entry that backs the `scalac -release` compiler option ([JEP-247](https://openjdk.java.net/jeps/247) support for viewing the Java base library "as of" an older JDK version. This change was buggy -- it did not include the selected release in the cache key, which could lead to a compiler that specifies `-release X` seeing the results of another compiler using `-release Y`. This behaviour was tested by a JDK-9+ conditional test (`MultiReleaseJarTest`) which unfortunately is not part of our CI on the 2.12.x branch, so the regression went unnoticed. While in this area, I followed up on a TODO comment in the same test and discovered another bug in handling of multi-release JARs. Again, this bug could manifest when different values of `-release` were used in a build. It would manifest as an `IllegalArgumentException` in `ResuableDataReader` when it used the size of the non-versioned classfile when sizing buffers for the versioned classfile.
retronym
added a commit
to retronym/scala
that referenced
this pull request
Jun 22, 2021
The compiler has a per-classloader cache that backs the classpath lookups of individual instances of `Global`. Elements in the cache are used by `Global` instances that are concurrent (think parallel compilation of sub-projects) or are sequential within a small timeout. In scala#9557, this was extended to the classpath entry that backs the `scalac -release` compiler option ([JEP-247](https://openjdk.java.net/jeps/247) support for viewing the Java base library "as of" an older JDK version. This change was buggy -- it did not include the selected release in the cache key, which could lead to a compiler that specifies `-release X` seeing the results of another compiler using `-release Y`. This behaviour was tested by a JDK-9+ conditional test (`MultiReleaseJarTest`) which unfortunately is not part of our CI on the 2.12.x branch, so the regression went unnoticed. While in this area, I followed up on a TODO comment in the same test and discovered another bug in handling of multi-release JARs. Again, this bug could manifest when different values of `-release` were used in a build. It would manifest as an `IllegalArgumentException` in `ResuableDataReader` when it used the size of the non-versioned classfile when sizing buffers for the versioned classfile.
retronym
added a commit
to retronym/scala
that referenced
this pull request
Jun 22, 2021
The compiler has a per-classloader cache that backs the classpath lookups of individual instances of `Global`. Elements in the cache are used by `Global` instances that are concurrent (think parallel compilation of sub-projects) or are sequential within a small timeout. In scala#9557, this was extended to the classpath entry that backs the `scalac -release` compiler option ([JEP-247](https://openjdk.java.net/jeps/247) support for viewing the Java base library "as of" an older JDK version. This change was buggy -- it did not include the selected release in the cache key, which could lead to a compiler that specifies `-release X` seeing the results of another compiler using `-release Y`. This behaviour was tested by a JDK-9+ conditional test (`MultiReleaseJarTest`) which unfortunately is not part of our CI on the 2.12.x branch, so the regression went unnoticed. While in this area, I followed up on a TODO comment in the same test and discovered another bug in handling of multi-release JARs. Again, this bug could manifest when different values of `-release` were used in a build. It would manifest as an `IllegalArgumentException` in `ResuableDataReader` when it used the size of the non-versioned classfile when sizing buffers for the versioned classfile.
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.
Multithreaded users of the compiler can experience lock contention in
classpath reading. This occurs because a) scalac by default reuses a
ZipFileinstance for a given JAR across concurrent compilers to reduce thememory footprint, and b) because
j.u.ZipFilecontains does not supportconcurrent reads.
Instead of having a 1-1 relationship between
ZipArchiveandj.u.ZipFile,this PR uses an internal pool of
ZipFileinstances, which can be expandedas needed to service concurrent reads a given JAR file.
While investigating this, I noticed that our caching layer was not
considering the value of the
-releasecompiler option as part of thecache key. This is now fixed. I also included Java 9+ classpath reposotories
(
JrtClassPathandCtSymClassPathin the caching scheme.)Profiling showed my that the attempt to reduce allocations when converting
the classpath entry names into symbol names was counter productive as it
bypassed
lazy val nameinClassFileEntryImplwhich was forced anyway onother code paths.