-
Notifications
You must be signed in to change notification settings - Fork 3.1k
JAR reading: more accurate and widespread caching, less read contention #9557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
bc4baed to
80a942d
Compare
|
What are the downsides of this option that prevent using it by default? |
|
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. |
|
A clarification: |
|
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. |
|
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`).
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.
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.
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.
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.