Skip to content
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

Backport #2605 to 2.10.x: SI-7149 Use a WeakHashSet for type uniqueness #2901

Merged
merged 3 commits into from Sep 4, 2013

Conversation

gkossakowski
Copy link
Member

This is backport of #2605.

The issue of uniques accumulating types during the whole compilation run turned out to be especially bad for ScalaTest tests. Scala 2.10.2 requires at least 6GB to compile Scalatest tests. With this patch, 800MB is enough.

Such a dramatic difference in memory consumption and low-risk of this patch makes it appealing to backport it to 2.10.x.

Review by @JamesIry.

gkossakowski and others added 3 commits September 3, 2013 17:47
Replaces scala.reflect.internal.WeakHashSet with a version that
* extends the mutable.Set trait
* doesn't leak WeakReferences
* is unit tested
Currently type uniqueness is done via a HashSet[Type], but
that means the Types live through an entire compile session, even
ones that are used once. The result is a huge amount of unnecessarily
retained memory. This commit uses a WeakHashSet instead so that Types
and their WeakReferences are cleaned up when no longer in use.
perRunCaches was using a HashMap of WeakReferences which meant it would
accumulate WeakReferences over time. This commit uses a WeakHashSet
instead so that the references are cleaned up.
@gkossakowski
Copy link
Member Author

I fixed the binary compatibility errors. Hopefully tests will pass now.

@retronym
Copy link
Member

retronym commented Sep 4, 2013

LGTM. I re-reviewed the change itself to re-convince myself that this can't hurt us.

@gkossakowski
Copy link
Member Author

@retronym: thanks for the review. I'll go and merge it then.

gkossakowski added a commit that referenced this pull request Sep 4, 2013
Backport #2605 to 2.10.x: SI-7149 Use a WeakHashSet for type uniqueness
@gkossakowski gkossakowski merged commit 068b9a3 into scala:2.10.x Sep 4, 2013
@gkossakowski gkossakowski deleted the backport-uniques-memory-fix branch February 7, 2014 10:08
smarter added a commit to dotty-staging/dotty that referenced this pull request Jun 25, 2021
This mimics what Scala 2 has been doing for a long time now and serves
the same purpose: it considerably reduces peak memory usage when
compiling some projects, for example previously compiling the Scalatest
tests required a heap of at least 11 GB, but now it fits in about 4 GB.

This required changing the implementation of WeakHashSet to have
overridable `hash` and `isEqual` methods just like HashSet, it also
required making various private methods protected since NamedTypeUniques
and AppliedUniques contain an inlined implementation of `put`.

This commit also changes the default load factor of a WeakHashSet from
0.75 to 0.5 to match the load factor we use for HashSets, though note that
Scala 2 has always been using 0.75.

For a history of the usage of WeakHashSet in Scala 2 see:
- scala/scala#247
- scala/scala#2605
- scala/scala#2901
xuwei-k pushed a commit to xuwei-k/scala3 that referenced this pull request Jul 6, 2021
This mimics what Scala 2 has been doing for a long time now and serves
the same purpose: it considerably reduces peak memory usage when
compiling some projects, for example previously compiling the Scalatest
tests required a heap of at least 11 GB, but now it fits in about 4 GB.

This required changing the implementation of WeakHashSet to have
overridable `hash` and `isEqual` methods just like HashSet, it also
required making various private methods protected since NamedTypeUniques
and AppliedUniques contain an inlined implementation of `put`.

This commit also changes the default load factor of a WeakHashSet from
0.75 to 0.5 to match the load factor we use for HashSets, though note that
Scala 2 has always been using 0.75.

For a history of the usage of WeakHashSet in Scala 2 see:
- scala/scala#247
- scala/scala#2605
- scala/scala#2901
BarkingBad pushed a commit to BarkingBad/dotty that referenced this pull request Jul 23, 2021
This mimics what Scala 2 has been doing for a long time now and serves
the same purpose: it considerably reduces peak memory usage when
compiling some projects, for example previously compiling the Scalatest
tests required a heap of at least 11 GB, but now it fits in about 4 GB.

This required changing the implementation of WeakHashSet to have
overridable `hash` and `isEqual` methods just like HashSet, it also
required making various private methods protected since NamedTypeUniques
and AppliedUniques contain an inlined implementation of `put`.

This commit also changes the default load factor of a WeakHashSet from
0.75 to 0.5 to match the load factor we use for HashSets, though note that
Scala 2 has always been using 0.75.

For a history of the usage of WeakHashSet in Scala 2 see:
- scala/scala#247
- scala/scala#2605
- scala/scala#2901
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants