-
-
Notifications
You must be signed in to change notification settings - Fork 422
Limit parallel Scala.js linking jobs to avoid high memory pressure #6260
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
Open
lefou
wants to merge
7
commits into
com-lihaoyi:main
Choose a base branch
from
lefou:tr-scalajs-linker-parallel
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+77
−5
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e85906b
Limit parallel Scala.js linking jobs to avoid high memory pressure
lefou d2aa908
Clenaup
lefou 8fe304b
typo
lefou 5e8d01b
Clear the linker state after each run
lefou fa300b4
Revert "Clear the linker state after each run"
lefou ae3fd39
Merge branch 'main' into tr-scalajs-linker-parallel
lefou 20fbd87
Merge branch 'main' into tr-scalajs-linker-parallel
lefou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
libs/scalajslib/src/mill/scalajslib/worker/ParallelismLimiter.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package mill.scalajslib.worker | ||
|
|
||
| import java.util.concurrent.Semaphore | ||
|
|
||
| /** | ||
| * Limit the parallelism of jobs run via [[runLimited]]. | ||
| * @param maxJobs The maximal parallelism | ||
| */ | ||
| class ParallelismLimiter(maxJobs: Int) { | ||
|
|
||
| private val linkerJobsSemaphore = Semaphore(maxJobs) | ||
|
|
||
| def runLimited[T](thunk: => T): T = { | ||
| linkerJobsSemaphore.acquire() | ||
| try { | ||
| thunk | ||
| } finally { | ||
| linkerJobsSemaphore.release() | ||
| } | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
libs/scalajslib/test/src/mill/scalajslib/worker/ParallelismLimiterTests.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package mill.scalajslib.worker | ||
|
|
||
| import utest.* | ||
|
|
||
| import java.util.concurrent.atomic.AtomicInteger | ||
|
|
||
| class ParallelismLimiterTests extends TestSuite { | ||
|
|
||
| override def tests = Tests { | ||
| test("limitedJobs") { | ||
|
|
||
| val maxJobs = 3 | ||
| val limiter = ParallelismLimiter(maxJobs) | ||
|
|
||
| val concurrentCount = new AtomicInteger(0) | ||
| val maxObserved = new AtomicInteger(0) | ||
|
|
||
| def work(i: Int, workTimeMs: Int): Unit = { | ||
| val before = concurrentCount.incrementAndGet() | ||
| maxObserved.updateAndGet(v => Math.max(v, before)) | ||
|
|
||
| Thread.sleep(workTimeMs) | ||
|
|
||
| val after = concurrentCount.decrementAndGet() | ||
| assert(after >= 0) | ||
| } | ||
|
|
||
| val tasks = (1 to 10).map { i => | ||
| new Thread(() => | ||
| limiter.runLimited { | ||
| work(i, 50) | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| tasks.foreach(_.start()) | ||
| tasks.foreach(_.join()) | ||
|
|
||
| assert(maxObserved.get() <= maxJobs) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should pass
linkerJobsinstead ofjobstoSince we are running two linker jobs at a time to save memory, if we store 8 different ones in memory, we aren't saving as much memory as we want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My working hypothesis was, that the high memory usage is required while the linking is in process, but most of it gets freed afterwards. That means, by delaying/synchronizing linking jobs, we already reduce the memory pressure. #6260 (comment) seems to support or at least not counter support this hypothesis.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider that the test was performed with the code to clear the linker after every link step, which makes sense to clean the linker memory afterwards. If we keep the linkers in memory and do not clear them, the test could give different results.