Skip to content

Make hot compilation 2x faster by properly reusing classloaders#393

Merged
lihaoyi merged 1 commit intocom-lihaoyi:masterfrom
smarter:better-compiler-caching
Jul 27, 2018
Merged

Make hot compilation 2x faster by properly reusing classloaders#393
lihaoyi merged 1 commit intocom-lihaoyi:masterfrom
smarter:better-compiler-caching

Conversation

@smarter
Copy link
Contributor

@smarter smarter commented Jul 27, 2018

So far, Mill was caching ScalaInstance which contains a classloader but
this is not enough: Zinc creates its own classloader by combining the
ScalaInstance classloader with the path to the compiler-bridge. Zinc
takes care of caching this classloader in each instance of
ScalaCompiler.

We can take advantage of this by caching an instance of Compilers since
it contains everything we want to cache (ScalaCompiler and
ScalaInstance).

This significantly reduces the amount of classloading that happens
at each compilation step, as measured by running:

export _JAVA_OPTIONS="-XX:+UnlockDiagnosticVMOptions -XX:+TraceClassLoading -XX:+TraceClassUnloading"
mill -i foo.compile

Then looking at the output of out/mill-worker-1/logs while adding a
new line in a source file in foo and running mill -i foo.compile again.

The speedup is going to depend on the project, but I measured a ~2x
improvement when running on the Mill build time(core.compile()) and
rm -rf out/core/compile/ in a loop until results stabilized. See also
the results that were obtained when a very similar issue was fixed in
sbt itself: sbt/sbt#2754

So far, Mill was caching ScalaInstance which contains a classloader but
this is not enough: Zinc creates its own classloader by combining the
ScalaInstance classloader with the path to the compiler-bridge. Zinc
takes care of caching this classloader in each instance of
ScalaCompiler.

We can take advantage of this by caching an instance of Compilers since
it contains everything we want to cache (ScalaCompiler and
ScalaInstance).

This significantly reduces the amount of classloading that happens
at each compilation step, as measured by running:

export _JAVA_OPTIONS="-XX:+UnlockDiagnosticVMOptions -XX:+TraceClassLoading -XX:+TraceClassUnloading"
mill -i foo.compile

Then looking at the output of `out/mill-worker-1/logs` while adding a
new line in a source file in `foo` and running `mill -i foo.compile` again.

The speedup is going to depend on the project, but I measured a ~2x
improvement when running on the Mill build `time(core.compile())` and
`rm -rf out/core/compile/` in a loop until results stabilized. See also
the results that were obtained when a very similar issue was fixed in
sbt itself: sbt/sbt#2754
@lihaoyi lihaoyi merged commit fa6987b into com-lihaoyi:master Jul 27, 2018
@lefou lefou added this to the 0.2.6 milestone Apr 29, 2019
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.

3 participants