Rewrote parTraverseN and parTraverseN_ for better performance#4451
Rewrote parTraverseN and parTraverseN_ for better performance#4451djspiewak wants to merge 20 commits intotypelevel:series/3.6.xfrom
parTraverseN and parTraverseN_ for better performance#4451Conversation
|
Pros and cons on performance, though I think it's possible to do better here. It's a little bit slower than the previous implementation in the happy path, but it's several orders of magnitude faster in the error path so I'll call that a win. BeforeAfter |
|
So I haven't golfed the failure down yet, but it really looks like we're hitting a bug in Scala.js, probably stemming from the "null safe" test. @durban you may be amused I think we could just remove the null safe test now since we're not using an |
|
Well, "amused" is one word for it :-) So it's not a bug in Scala.js, as in, it behaves as documented: dereferencing |
|
Well that's fun. I actually thought we had some special checking for when the |
|
That we do check. It's this line: https://github.com/typelevel/cats-effect/blob/series/3.x/core/shared/src/main/scala/cats/effect/IO.scala#L2024 (and |
|
Ahhhhhhh that makes sense. Okay, by that token, I think it's fair to say that a lot of our combinators just aren't |
|
It's annoying, because in Scala they are null safe. (The test passed before, it just failed on JS.) We'd have to do something like this (everywhere), to make it work on JS: def combinator(fa: F[A], ...) = {
if (fa eq null) throw new NullPointerException
}Which is (1) annoying, (2) very redundant, except on Scala.js, and (3) apparently has performance problems in Scala.js (or maybe that's only the linker setting?). I don't propose we do this. There is a Scala.js linker setting which fixes the problem. In Scala and Scala Native it works by default. |
kernel/shared/src/main/scala/cats/effect/kernel/GenConcurrent.scala
Outdated
Show resolved
Hide resolved
kernel/shared/src/main/scala/cats/effect/kernel/GenConcurrent.scala
Outdated
Show resolved
Hide resolved
kernel/shared/src/main/scala/cats/effect/kernel/GenConcurrent.scala
Outdated
Show resolved
Hide resolved
kernel/shared/src/main/scala/cats/effect/kernel/GenConcurrent.scala
Outdated
Show resolved
Hide resolved
|
(Just some context about the |
|
Could this fix hit 3.6.4? |
There are a couple failing tests related to early termination that I'm still trying to track down. Am trying to find the spare time needed to push on it. Help definitely welcome! Otherwise I'll probably get to it within the next few weeks. Sorry :( |
|
I see that last CI is green, do you mean that you want to reintroduce the tests removed in this commit? 599b790 |
fc113cb to
584ce3b
Compare
|
@durban Would you mind taking another look here? I think I got this all sorted out. |
|
Yes, I'll take another look (hopefully sometime this week). |
durban
left a comment
There was a problem hiding this comment.
@djspiewak I've pushed 2 failing tests. The idea is the same for them: let every fiber start; then one of them self-cancels/fails; let awaitAll observe this through preempt; and the problem is, awaitAll succeeds in this case, and thus the fibers will not be cancelled.
(I've also added another comment.)
| case Outcome.Errored(_) | Outcome.Canceled() => preempt.complete(None) *> cancelAll | ||
| } | ||
|
|
||
| work *> resurface |
There was a problem hiding this comment.
Minor thing, but this means we can get cancelled when we're already done. If work completes successfully, i.e., we're literally done, there is no reason really, to observe a cancellation. (It feels a little bit like the timeout changes.)
There was a problem hiding this comment.
This is also a correctness issue actually because we could lose data. I'll correct it.
|
Also: I thought the idea was, that there is no need to release the semaphore in case there is an error/cancel, because we're shutting down all the things anyway... This latest versions seems to release the semaphore always. (Also: sorry for pushing to your branch, I've just didn't feel like going through the ceremony of opening a PR-for-the-PR. You can obviously roll back if I've messed up something.) |
It was initially easier to structure it that way, but this is a good point. I should move this back to the happy path exclusively.
No worries at all! I think that's totally reasonable |
This shifts to a fully bespoke implementation of
parTraverseNand such. There are a few things left to clean up, such as a few more tests and running some comparative benchmarks, but early results are very promising. In particular, the failure case from #4434 appears to be around two to three orders of magnitude faster with this implementation (which makes sense, since it handles early abort correctly). Kudos to @SystemFw for the core idea which makes this possible.One of the things I'm doing here is giving up entirely on universal fairness and merely focusing on in-batch fairness. A simpler way of saying this is that we are hardened against head of line blocking, both for actions and cancelation.
Fixes #4434