From 83fd83ffa5cbc74d28e6d13de1713de8c1c700c3 Mon Sep 17 00:00:00 2001 From: gmega Date: Mon, 30 Jun 2025 20:10:56 -0300 Subject: [PATCH] feat: allow futures to be returned out-of-order to decrease memory consumption --- codex/blockexchange/engine/engine.nim | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/codex/blockexchange/engine/engine.nim b/codex/blockexchange/engine/engine.nim index bb46b62f..e3af0593 100644 --- a/codex/blockexchange/engine/engine.nim +++ b/codex/blockexchange/engine/engine.nim @@ -258,6 +258,7 @@ proc requestBlocks*( self: BlockExcEngine, addresses: seq[BlockAddress] ): SafeAsyncIter[Block] = var handles: seq[BlockHandle] + # Adds all blocks to pendingBlocks before calling the first downloadInternal. This will # ensure that we don't send incomplete want lists. for address in addresses: @@ -276,7 +277,13 @@ proc requestBlocks*( # Be it success or failure, we're completing this future. let value = try: - success await handles[completed] + # FIXME: this is super expensive. We're doing several linear scans, + # not to mention all the copying and callback fumbling in `one`. + let + handle = await one(handles) + i = handles.find(handle) + handles.del(i) + success await handle except CancelledError as err: warn "Block request cancelled", addresses, err = err.msg raise err