Skip to content

Conversation

@retronym
Copy link
Member

@retronym retronym commented Mar 30, 2021

Multithreaded users of the compiler can experience lock contention in
classpath reading. This occurs because a) scalac by default reuses a
ZipFile instance for a given JAR across concurrent compilers to reduce the
memory footprint, and b) because j.u.ZipFile contains does not support
concurrent reads.

Instead of having a 1-1 relationship between ZipArchive and j.u.ZipFile,
this PR uses an internal pool of ZipFile instances, which can be expanded
as 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 -release compiler option as part of the
cache key. This is now fixed. I also included Java 9+ classpath reposotories
(JrtClassPath and CtSymClassPath in 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 name in ClassFileEntryImpl which was forced anyway on
other code paths.

@scala-jenkins scala-jenkins added this to the 2.12.14 milestone Mar 30, 2021
@dwijnand
Copy link
Member

% scalac t6591_3.scala
% <in process execution of run/t6591_3.scala> > t6591_3-run.log
% diff /home/jenkins/workspace/scala-2.12.x-validate-main/test/files/run/t6591_3.check /home/jenkins/workspace/scala-2.12.x-validate-main/test/files/run/t6591_3-run.log
@@ -1 +1,13 @@
 Block(List(ValDef(Modifiers(), TermName("v"), Select(This(TypeName("A")), TypeName("I")), Apply(Select(New(Select(This(TypeName("A")), TypeName("I"))), termNames.CONSTRUCTOR), List()))), Ident(TermName("v")))
+Uncaught exception on thread Thread[Timer-0,5,main]: java.nio.file.NoSuchFileException: t6502-run.obj/test1.jar
+java.nio.file.NoSuchFileException: t6502-run.obj/test1.jar
+	at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
+	at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
+	at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
+	at sun.nio.fs.UnixPath.toRealPath(UnixPath.java:837)
+	at com.sun.nio.zipfs.ZipFileSystemProvider.removeFileSystem(ZipFileSystemProvider.java:322)
+	at com.sun.nio.zipfs.ZipFileSystem.close(ZipFileSystem.java:313)
+	at scala.tools.nsc.classpath.ZipFsClassPath.close(DirectoryClassPath.scala:220)
+	at scala.tools.nsc.classpath.FileBasedCache$$anon$1$$anon$2.run(ZipAndJarFileLookupFactory.scala:249)
+	at java.util.TimerThread.mainLoop(Timer.java:555)
+	at java.util.TimerThread.run(Timer.java:505)

@retronym retronym force-pushed the faster/zipfs branch 2 times, most recently from bc4baed to 80a942d Compare March 31, 2021 00:08
@smarter
Copy link
Member

smarter commented Mar 31, 2021

What are the downsides of this option that prevent using it by default?

@retronym
Copy link
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.

@retronym
Copy link
Member Author

retronym commented Apr 1, 2021

A clarification: java.nio.zipfs.ZipFileSystem does not actually support concurerent reads of data, but it does support concurrent reads of metadata.

@retronym retronym marked this pull request as draft April 1, 2021 03:44
@retronym
Copy link
Member Author

retronym commented Apr 1, 2021

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.

@retronym
Copy link
Member Author

Update:

retronym added 4 commits May 4, 2021 16:07
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`).
@retronym retronym changed the title JAR reading: option to use zipfs, other fixes JAR reading: more accurate and widespread caching, less read contention May 4, 2021
@retronym retronym requested a review from lrytz May 17, 2021 20:21
@retronym retronym marked this pull request as ready for review May 17, 2021 20:22
@lrytz lrytz merged commit 21a8a7a into scala:2.12.x 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-notes worth highlighting in next release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants