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.
Summary
Adds a
preserveOrder
option topMapIterable
, which indicatesAddresses #72.
This implementation a bit more complicated than I expected, and I discovered several edge cases and race conditions along the way, so I encourage careful review and the suggestion/addition of more appropriate test cases.
Implementation considerations
In order to
promises
as an unordered pool for use withPromise.race
andO(min(concurrency, backpressure))
promises.indexOf
to know whichpromise
to remove frompromises
,the promise itself must return some identifying information. The promise's
inputIndex
is good for this purpose as it is a stable id that will not change when shufflingpromises
around. However,inputIndex
alone is not enough to determine which member ofpromises
has returned, so we also introduce extra bookkeepingpromisesIndexFromInputIndex
information. In order to correctly recordpromisesIndexFromInputIndex
duringmapNext
, we "reserve" a spot in thepromises
array duringtrySpawn
(from recursivetrySpawn
calls in particular, before we would've had a chance topromises.push(mapNext())
, since the innermapNext()
expression executes additionaltrySpawn
s before we can perform the outer.push
expression) by incrementing its length, so that we can usepromises.length
to determinepromisesIndex
.Then, to avoid an
O(min(concurrency, backpressure))
promises.splice
to remove the resolved promise, we instead swap the resolved promise withpromises[promises.length - 1]
. We could also overwrite the resolved member ofpromises
in-place, but it seemed slightly cleaner to do it this way so we could reuse this logic in thepMapSkip + preserveOrder: true
case where we currentlyindexOf
+splice
. In order to make the aforementioned swap, though, we need to update ourpromisesIndexFromInputIndex
ledger: however, we cannot knowpromises[promises.length - 1]
's input index without extra information, so we introduce an additionalinputIndexFromPromisesIndex
record.Speaking of which, to unify the
preserveOrder: false
logic with thepreserveOrder: true
case, for the latter we still treat thepromises
array in the same pool-fashion, where the result of mapping anyinputIndex
might end up anywhere inpromises
, but bookkeep an additionaloutputIndex
datum to use in conjunction withpromisesIndexFromInputIndex
to determine which promise is next in sequence (and soawait
and process). As mentioned earlier, this pool-based pattern also allows us to handle thepMapSkip
case in anO(1)
manner, compared to the existingindexOf
+splice
strategy.Further,
pMapSkip
is now handled within one of thepromises
itself, instead of in the mainwhile
loop. This is done by breaking out the IIFE assigned topromise
into amapNext
function that can then be called recursively whenpMapSkip
is returned. However, we must also consider the case thatpreserveOrder: true
and the current promise is beingawait
ed already: in this case, ifconcurrency > 1
and we were tomapNext
, the current promise would return the result of mapping aniterable
element manyinputIndex
es in the future, failing topreserveOrder
. Thus, we check if themyInputIndex + 1
mapper is running, and if so, return its promise after removing our current promise frompromises
: thus when the current promise returns we will process and remove themyInputIndex + 1
promise, as if we hadawait promises[promisesIndexFromInputIndex[myInputIndex + 1]]
ed. But now ouroutputIndex
is behind (it think we should return the promise formyInputIndex + 1
next, but we skipped that one, so we should return the promise formyInputIndex + 2
instead), so to compensate, we skip indices withpromisesIndexFromInputIndex[outputIndex] === undefined
.As a last piece of miscellany, when
preserveOrder: false && (await nextPromise()).result.done === true
, stopping the async iterable is no longer safe, since otherpromises
may still be pending in the array. So, wecontinue
in this case.Finally, I made other small optimizations where I spotted an opportunity, like only
await
ing when necessary andtrySpawn
ing beforeawait
ing the inputiterable
's.next
response, in case we can start another promise chugging, too.