Skip to content

[2.x] Reduce number of long-living instances to speed up loading - #7866

Merged
adpi2 merged 8 commits into
sbt:developfrom
adpi2:2.x-reduce-heap
Nov 6, 2024
Merged

adpi2 merged 8 commits into
sbt:developfrom
adpi2:2.x-reduce-heap

Conversation

@adpi2

@adpi2 adpi2 commented Nov 4, 2024 •

Copy link
Copy Markdown
Member

After loading, instances of ScopedKey, Scope, Select or ProjectRef represent 25.6% of the total number of instances, and 27.3% of the heap size. The main idea of this PR is to reduce the number of duplicated instances using uniqueness caches.

Uniqueness cache on Scope

It reduces the number of Scope instances by 94%. On its own, it slows down the loading, because Scope.apply becomes more expensive and it is called too many times. This can be compensated by using temporary caches in Project.transform and delegates: Scope => Seq[Scope], to avoid calling Scope.apply to create too many short-lived instances.

It also reduces the number of Select by 92% and ProjectRef by 99.8%, which make them totally negligible.

Since we use a uniqueness cache we might as well pre-compute the hash code of each scope, which reduce the CPU time.

Uniqueness cache on ScopedKey (uncommited)

I think it is also worth mentioning that I tried a uniqueness cache on ScopedKey but it was not really better. Indeed, on one hand it reduced the number of ScopedKey by 59.6%, but on the other hand it was counterbalanced by the number of nodes in the cache itself.

Other changes

The first commit reduces the number of Init.GetValue by 48% for free.

Result

I am still using sttp to collect those metrics, on my 12 CPUs laptop, and JDK temurin 17 (G1GC).

It shows up a 20% speedup of the loading time compared to sbt 2.0.0-M2, and a 41% speedup compared to sbt 1.10.2. Those are rough estimations.

Compared to 2.0.0-M2

Settings Instances Used Heap size Load time
2.0.0-M2 212961 20,920 K 532 MB 7949 ms
This PR 212961 16,365 K 410 MB 6328 ms
100 % 78 % 77 % 80 %

Compare to 1.10.2

Settings Instances Used Heap size Load time
1.10.2 197031 23,524 K 675 MB 10629 ms
This PR 212961 16,365 K 410 MB 6328 ms
108 % 70 % 61 % 59 %

@adpi2
adpi2 requested a review from eed3si9n November 4, 2024 13:12
@eed3si9n

eed3si9n commented Nov 4, 2024

Copy link
Copy Markdown
Member

@adpi2 Nice work!

Comment thread util-collection/src/main/scala/sbt/internal/util/Settings.scala
Comment thread main-settings/src/main/scala/sbt/Scope.scala
@adpi2
adpi2 requested a review from eed3si9n November 5, 2024 10:40
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