Vec::extend/from_iter: accumulate in bulk instead of per-element push_back#1893
Draft
leighmcculloch wants to merge 1 commit into
Draft
Vec::extend/from_iter: accumulate in bulk instead of per-element push_back#1893leighmcculloch wants to merge 1 commit into
leighmcculloch wants to merge 1 commit into
Conversation
…_back The `Extend` impl for `Vec` (used by `Vec::from_iter` and `.extend(..)`) appended items by calling `vec_push_back` once per element, which is one host call per item and allocates a new intermediate vec object on the host every time. Accumulate items into a fixed-size stack buffer and flush each full buffer with a single bulk `vec_new_from_slice` + append instead. This mirrors the zero-copy improvement made to `BytesN::from` in #1888. Also updates the `from_iter` doc note that claimed per-element host calls, and adds a unit test covering empty, sub-chunk, exact-chunk-multiple and cross-chunk lengths. https://claude.ai/code/session_0168wkXopix1LPcuzi6AS4WF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
The
Extendimpl forVec(used byVec::from_iterand.extend(..))appended items by calling
vec_push_backonce per element. Each call is a hostcall that also allocates a new intermediate vec object on the host. This change
accumulates items into a fixed-size stack buffer and flushes each full buffer
with a single bulk
vec_new_from_slice+ append.Why
Same class of inefficiency that was fixed for
BytesN::fromin #1888(per-element host calls where a bulk host op exists).
Net cost (baseline subtracted), measured on the guest budget with a WASM
compute-budget bench built the same way as the #1888 bench (
baseline_*twinsubtracted;
from_arrayshown as the already-bulk reference):vec_from_iter_192Bench contract and harness
Contract (one
test_*cdylib crate):Harness (in
soroban-sdk/src/tests,#[ignore]d): register the wasm,reset_unlimited(), callclient.(), then printcpu_instruction_cost()/memory_bytes_cost().Run with
make build-test-wasmsthencargo test --release -p soroban-sdk --lib --features testutils -- --ignored --nocapture.Notes
The chunked approach works for any iterator and uses a fixed-size stack buffer
of
Vals, so it works inno_stdwithoutalloc(the common contract case).The
from_iterdoc note that claimed per-element host calls is updated. A unittest (
test_vec_from_iter_chunking) covers empty, sub-chunk,exact-chunk-multiple and cross-chunk lengths.
Following the precedent of #1888, only the fix + correctness unit test are
committed here; the bench lives in this description rather than in the tree.
One of three related findings from sweeping the SDK for per-element host-call
patterns (see also:
Vec::from_slice#1891,Vec::extend_from_slice#1892).