Repository navigation
feat: make it easier to eagerly consume completed tasks - #1
Conversation
878bb0b to
6db21f5
Compare
9706862 to
c16524c
Compare
There was a problem hiding this comment.
Before this PR, we vended out permits from the semaphore which lived alongside the tasks, and were dropped after the spawned future completed.
After this PR, the permit is no longer dropped, but returned from the completed task itself. This means, I think, that the permit only "dropped" after someone reclaims it explicitly by joining the next task in the set.
I think this means that the permits are now tightly coupled with the size of the internal JoinSet. Does this mean we don't actually need to use a semaphore/permit at all anymore?
Could we change spawn to the following:
// "max" is whatever size the semaphore used to be.
// but here there's no semaphore anymore
let output = if self.set.len() >= self.max {
self.set.join_next().await
} else {
None
};
self.set.spawn(future)(this obviously removes the need for the (Permit, T) tuple too, if we think this is legit?)
There was a problem hiding this comment.
I think this means that the permits are now tightly coupled with the size of the internal
JoinSet. Does this mean we don't actually need to use a semaphore/permit at all anymore?Could we change spawn to the following:
I had also wondered about this. I wasn't sure about whether I wanted to do that as part of this change or not, but it's certainly much simpler that way...
There was a problem hiding this comment.
Okay, I went ahead and did this, and...wow, it's a lot simpler now. Good call.
Auto-merge was disabled for another pull request in the stack
This branch makes a large, breaking API change to the
ParallelTaskSetAPI asimported from the
omicronrepo, for a purpose that is somewhat hard tosummarize in a one-line commit message title. This is all intended to help avoid
an issue @iliana ran into while using this API, which is that when tasks
complete faster than they are spawned, the completed tasks remain in memory
until the entire
ParallelTaskSetis drained. This means that the heap used bythose tasks is not freed, and if they have acquired RAII resources, they do not
release those resources until they are consumed. In order to prevent resource
exhaustion, this branch makes two main changes to
ParallelTaskSet:Rather than returning an
Option<T>containing the output of one task,ParallelTaskSet::spawnnow returns a newCompletedTasksiterator. Thisiterator will return both the output of any task that was awaited in order to
spawn a new one, and will continue to return the output of any tasks that
can be joined without awaiting. This way, it's easier for callers to ensure
that all currently completed tasks are joined more eagerly, freeing any
memory or other resources those tasks are holding. The iterator is marked as
must_useto try to encourage callers to ensure it is always drained fully.In addition, the parallelism limit continues to count tasks until they are
fully consumed, by having the spawned tasks return their
Semaphorepermitwhen they are joined, rather than dropping it as soon as the inner future
completes. This way, the parallelism limit bounds the memory and/or resources
held by tasks regardless of whether they are currently running or have
completed but have not yet been consumed.
This also allows us to make a smallish optimization to
spawnwhere were-use the permit held by the task that we await in order to spawn a new one.
This way, we don't have to do a few atomic increment/decrement operations
(and potentially lock the semaphore) in order to free the permit and then
immediately acquire a new one. This probably doesn't matter a whole lot, but
I thought it was cute.