From f7bccb093fc41e22f1e259f3ce19d90f5bbf03f8 Mon Sep 17 00:00:00 2001 From: Lorenzo Gabriele Date: Sun, 16 Jul 2023 16:47:06 +0200 Subject: [PATCH 1/2] Increase initialCapacity for HashSet in ExtractDependencies.scala Growing that HashSet seem to take 1.27% of the allocations when compiling dotty itself. --- compiler/src/dotty/tools/dotc/sbt/ExtractDependencies.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/sbt/ExtractDependencies.scala b/compiler/src/dotty/tools/dotc/sbt/ExtractDependencies.scala index 5e7bedba5e4b..eabcd6ec65ec 100644 --- a/compiler/src/dotty/tools/dotc/sbt/ExtractDependencies.scala +++ b/compiler/src/dotty/tools/dotc/sbt/ExtractDependencies.scala @@ -447,7 +447,7 @@ private class ExtractDependenciesCollector extends tpd.TreeTraverser { thisTreeT // Avoid cycles by remembering both the types (testcase: // tests/run/enum-values.scala) and the symbols of named types (testcase: // tests/pos-java-interop/i13575) we've seen before. - val seen = new mutable.HashSet[Symbol | Type] + val seen = new mutable.HashSet[Symbol | Type](initialCapacity = 128, loadFactor = mutable.HashSet.defaultLoadFactor) def traverse(tp: Type): Unit = if (!seen.contains(tp)) { seen += tp tp match { From 26630e888a9cbe4b4e0448167bda02994324202d Mon Sep 17 00:00:00 2001 From: Lorenzo Gabriele Date: Mon, 17 Jul 2023 11:03:05 +0200 Subject: [PATCH 2/2] Reduce initialCapacity to 64 64 is enough to cover almost 99% of the cases so it's a good trade-off between memory consumed and allocations count. --- compiler/src/dotty/tools/dotc/sbt/ExtractDependencies.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/sbt/ExtractDependencies.scala b/compiler/src/dotty/tools/dotc/sbt/ExtractDependencies.scala index eabcd6ec65ec..9f0dd60d1dce 100644 --- a/compiler/src/dotty/tools/dotc/sbt/ExtractDependencies.scala +++ b/compiler/src/dotty/tools/dotc/sbt/ExtractDependencies.scala @@ -447,7 +447,7 @@ private class ExtractDependenciesCollector extends tpd.TreeTraverser { thisTreeT // Avoid cycles by remembering both the types (testcase: // tests/run/enum-values.scala) and the symbols of named types (testcase: // tests/pos-java-interop/i13575) we've seen before. - val seen = new mutable.HashSet[Symbol | Type](initialCapacity = 128, loadFactor = mutable.HashSet.defaultLoadFactor) + val seen = new mutable.HashSet[Symbol | Type](initialCapacity = 64, loadFactor = mutable.HashSet.defaultLoadFactor) def traverse(tp: Type): Unit = if (!seen.contains(tp)) { seen += tp tp match {