-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Optimize Queue#shutdown
#9770
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
Optimize Queue#shutdown
#9770
Conversation
| else { | ||
| val as = unsafePollAll(queue) | ||
| if (as.nonEmpty) { | ||
| if (!as.isEmpty) { |
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.
Note that I noticed a few nonEmpty calls. This is a micro-optimization as isEmpty is implemented directly on Chunk but nonEmpty requires an interface method dispatch which is very slow
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 we need to override knownSize in Chunk.
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.
Actually, IndexedSeq overrides knownSize: https://github.com/scala/scala/blob/483b4ee34aa8957709f5ab8bc63a69a069949e4b/src/library/scala/collection/IndexedSeq.scala#L118
| def surplusSize: Int | ||
|
|
||
| def shutdown(implicit trace: Trace): UIO[Unit] | ||
| def shutdown(fiberId: FiberId)(implicit trace: Trace, unsafe: Unsafe): Unit |
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.
could this be protected?
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.
Not really, since it's called by Queue. But it's OK because Strategy is a private class
I noticed that
Queue#shutdownwas quite unoptimized while looking at a different PR.At the moment
Queue#shutdownis quite heavy for the calling thread, as it involves a lot of ZIO's in addition to completing Promises in parallel unnecessarily. With this PR we avoid all of this which should make queue shutdowns a lot faster. While most workflows won't see much of a performance benefit from this change, certain workflows that rely on creation of queues for streams will definitely benefit from these changes.