-
Notifications
You must be signed in to change notification settings - Fork 529
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
Thread local fallback weak bag #2844
Merged
djspiewak
merged 7 commits into
typelevel:series/3.3.x
from
vasilmkd:thread-local-weak-bag
Feb 27, 2022
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1c9eb00
Use thread local weak bags as a fallback instead of locking
vasilmkd 8fcb7dd
Remove unused class
vasilmkd 971469a
Add a mima exclusion for the deleted class
vasilmkd b7e1462
External runtime benchmark
vasilmkd 21ef59e
Explicit types are needed for implicit values
vasilmkd 385a14d
Add a synchronization point on insert
vasilmkd 4c8f391
Guard against memory leaks in strange executors
vasilmkd 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
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
benchmarks/src/main/scala/cats/effect/benchmarks/ThreadLocalBenchmark.scala
This file contains 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,58 @@ | ||
/* | ||
* Copyright 2020-2022 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cats.effect.benchmarks | ||
|
||
import cats.effect.unsafe.FiberMonitor | ||
|
||
import org.openjdk.jmh.annotations._ | ||
|
||
import scala.concurrent.{Await, ExecutionContext, Future} | ||
import scala.concurrent.duration._ | ||
|
||
import java.util.concurrent.TimeUnit | ||
|
||
/** | ||
* To run the benchmark from within sbt: | ||
* | ||
* jmh:run -i 10 -wi 10 -f 2 -t 1 cats.effect.benchmarks.ThreadLocalBenchmark | ||
* | ||
* Which means "10 iterations", "10 warm-up iterations", "2 forks", "1 thread". Please note that | ||
* benchmarks should be usually executed at least in 10 iterations (as a rule of thumb), but | ||
* more is better. | ||
*/ | ||
@State(Scope.Thread) | ||
@BenchmarkMode(Array(Mode.Throughput)) | ||
@OutputTimeUnit(TimeUnit.SECONDS) | ||
class ThreadLocalBenchmark { | ||
|
||
final implicit val executionContext: ExecutionContext = ExecutionContext.global | ||
|
||
@Param(Array("2000")) | ||
var size: Int = _ | ||
|
||
@Benchmark | ||
def contention() = { | ||
val monitor = new FiberMonitor(null) | ||
|
||
def future(): Future[Unit] = Future { | ||
monitor.monitorSuspended(null) | ||
() | ||
} | ||
|
||
Await.result(Future.sequence(List.fill(size)(future())), Duration.Inf) | ||
} | ||
} |
This file contains 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
This file contains 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
35 changes: 0 additions & 35 deletions
35
core/jvm/src/main/scala/cats/effect/unsafe/SynchronizedWeakBag.scala
This file was deleted.
Oops, something went wrong.
This file contains 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
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.
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.
@vasilmkd sorry, I had a follow-up question about this change.
Is it possible that we could lose track of suspended fibers, if the threads that they were suspended from no longer exist? Is that even a realistic situation 😆
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.
That's a possibility, yes. The change was made with the intention that having an already inaccurate reporting mechanism remain that way is better than a memory leak. If people disagree, PRs are welcome.
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.
That's fair, thanks.
Probably over-complicated but I wonder if we could use a
PhantomReference
to "evacuate" the contents of the bag when its owning thread gets GCed.Btw, since the WSTP also dynamically adds/removes threads, how is this problem handled there?
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.
The WSTP does not use this code path. I'm open to exploring Phantom References.
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.
Right :) but it still uses a thread-local fiber bag right? And the threads may be added/removed as the WSTP resizes itself? So it seems like it's a very similar problem.
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.
Yeah, I wasn't sure if it's worth it :) instead of a dedicated thread, is this something we can schedule on the runtime itself?
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.
That's what we had before. It requires solving mapping of threads to bags, which was done using locking. If we come up with a concurrent weak bag/hash map, then sure. But not even JCTools has that afaik. It's a big undertaking.
Edit: I misunderstood your comment and answered something completely different.
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.
Scheduling on the runtime requires answering how often do to run it, which to me doesn't seem like a good strategy for something considered to be memory beneficial/critical. And ReferenceQueue is not too smart of an interface either. You can poll it in a non-blocking way, and when it returns null, when do you try again? The proper way IMO is to block on it and run cleanup on each expiry.
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.
No it doesn't seem very elegant :) I feel like in practice, there must be some reasonable rate at which we can check the
ReferenceQueue
... if an application is adding/removing threads too fast seems like its performance would be bounded by other factors anyway. But I don't really know about such things :)After thinking about this more, seems like it could be important. A situation in which there is a deadlock seems like exactly the situation when a dynamically resizing threadpool would start culling threads due to lack of work, which could cause GC of the fiber bag holding the fibers would help diagnose the deadlock.
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.
@djspiewak 👆🏻