Skip to content

Make insert_many fallback linear with empty and single fast paths - #10

Open
charliermarsh wants to merge 3 commits into
v1from
charlie/codex-v1-insert-many
Open

Make insert_many fallback linear with empty and single fast paths#10
charliermarsh wants to merge 3 commits into
v1from
charlie/codex-v1-insert-many

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Sep 8, 2026

Copy link
Copy Markdown
Member

Summary

When an iterator produces more elements than its lower size hint, insert_many shifts the existing tail once for every extra element. We now append the excess elements and rotate them into position once, avoiding that repeated movement. Empty and single-element inputs have fast paths, and a guard puts successfully appended elements in place if iteration panics.

Across five native Rust benchmark pairs on x86_64 Linux, inserting 1,024 elements before a 1,024-element tail dropped from about 90 µs to 0.65 µs. Empty and single insertions stayed around 14 ns and 21 ns, respectively; a short insertion with a weak positive size hint was about 6% slower. The large gain comes from avoiding repeated tail movement in that workload.

The added native benchmarks were removed in a follow-up commit and remain in the PR history.

@charliermarsh charliermarsh changed the title Make insert_many linear for underestimated iterator hints Make insert_many fallback linear with empty and single fast paths Sep 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant