Skip to content

fast path for BTreeMap::Iter::next - #163521

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
ywxt:btree_map_iter
Oct 11, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
ywxt:btree_map_iter

Conversation

@ywxt

@ywxt ywxt commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

BTreeMap::Iter currently obtains the next kv through leaf navigation even when the next kv is in the same node. This PR adds fast paths for BTreeMap::Iter and BTreeMap::Range: when advancing does not cross a node boundary, the next edge and kv handle are constructed directly from their indexes.

Result on aarch64:

  • map::iteration
benchmark baseline ns/iter new ns/iter change
map::iteration_20 362.74 ± 3.80 116.60 ± 1.46 -67.9%
map::iteration_1000 18743.31 ± 166.97 7181.88 ± 91.56 -61.7%
map::iteration_100000 2048028.00 ± 38646.50 893023.00 ± 6601.05 -56.4%
map::iteration_rev_20 398.15 ± 6.02 111.56 ± 1.24 -72.0%
map::iteration_rev_1000 19650.95 ± 120.38 6719.33 ± 33.38 -65.8%
map::iteration_rev_100000 2045171.75 ± 88630.03 826631.00 ± 7210.95 -59.6%
map::iteration_mut_20 332.97 ± 3.07 116.83 ± 9.19 -64.9%
map::iteration_mut_1000 17282.91 ± 142.66 7105.18 ± 64.92 -58.9%
map::iteration_mut_100000 1872307.50 ± 116359.10 930589.50 ± 11501.75 -50.3%
map::iteration_mut_rev_20 314.35 ± 5.69 99.88 ± 4.46 -68.2%
map::iteration_mut_rev_1000 16153.08 ± 77.67 6333.19 ± 9.81 -60.8%
map::iteration_mut_rev_100000 1863366.00 ± 90111.10 832836.00 ± 13949.30 -55.3%
  • map::range_iteration
Benchmark Baseline (ns/iter) New (ns/iter) Change
map::range_iteration_20 380.62 ± 4.55 99.54 ± 1.49 -73.8%
map::range_iteration_1000 18,545.75 ± 193.17 5,870.29 ± 66.97 -68.3%
map::range_iteration_100000 1,886,226.50 ± 7,167.85 606,092.00 ± 5,516.90 -67.9%
map::range_iteration_rev_20 354.25 ± 3.59 98.63 ± 4.56 -72.2%
map::range_iteration_rev_1000 16,976.08 ± 186.23 5,604.28 ± 104.99 -67.0%
map::range_iteration_rev_100000 1,717,588.50 ± 6,813.65 579,875.00 ± 6,914.20 -66.2%
map::range_iteration_mut_20 336.01 ± 14.14 94.51 ± 2.88 -71.9%
map::range_iteration_mut_1000 15,517.95 ± 218.64 5,095.03 ± 44.77 -67.2%
map::range_iteration_mut_100000 1,604,837.00 ± 12,474.30 510,776.50 ± 1,607.21 -68.2%
map::range_iteration_mut_rev_20 293.22 ± 4.60 86.87 ± 0.85 -70.4%
map::range_iteration_mut_rev_1000 13,705.48 ± 126.54 5,035.34 ± 34.81 -63.3%
map::range_iteration_mut_rev_100000 1,393,440.00 ± 5,983.90 517,439.50 ± 4,770.85 -62.9%
  • map::first_and_last
Benchmark Baseline (ns/iter) New (ns/iter) Change
map::first_and_last_0_nightly 21.06 ± 0.44 21.05 ± 0.22 -0.0%
map::first_and_last_0_stable 42.63 ± 1.34 42.53 ± 0.94 -0.2%
map::first_and_last_100_nightly 47.77 ± 1.22 46.62 ± 0.60 -2.4%
map::first_and_last_100_stable 302.27 ± 1.00 126.24 ± 4.89 -58.2%
map::first_and_last_10k_nightly 73.27 ± 1.01 73.22 ± 0.92 -0.1%
map::first_and_last_10k_stable 368.89 ± 4.71 172.73 ± 2.44 -53.2%
  • set
Benchmark Baseline (ns/iter) New (ns/iter) Change
set::difference_random_100_vs_100 2,932.50 ± 5.08 1,377.14 ± 20.87 -53.0%
set::difference_random_100_vs_10k 2,864.22 ± 9.71 2,294.26 ± 6.83 -19.9%
set::difference_random_10k_vs_100 193,483.00 ± 1,447.92 94,580.40 ± 1,345.45 -51.1%
set::difference_random_10k_vs_10k 296,728.67 ± 2,560.70 145,264.67 ± 1,775.17 -51.0%
set::difference_staggered_100_vs_100 2,896.54 ± 10.72 1,477.26 ± 14.43 -49.0%
set::difference_staggered_100_vs_10k 3,443.02 ± 72.13 2,715.43 ± 25.13 -21.1%
set::difference_staggered_10k_vs_10k 293,658.33 ± 897.93 152,474.25 ± 2,005.09 -48.1%
set::intersection_100_neg_vs_100_pos 14.39 ± 0.04 14.12 ± 0.14 -1.9%
set::intersection_100_neg_vs_10k_pos 17.77 ± 0.06 17.63 ± 0.06 -0.8%
set::intersection_100_pos_vs_100_neg 14.44 ± 0.06 13.99 ± 0.14 -3.1%
set::intersection_100_pos_vs_10k_neg 17.72 ± 0.22 17.38 ± 0.03 -1.9%
set::intersection_10k_neg_vs_100_pos 17.21 ± 0.07 17.16 ± 0.21 -0.3%
set::intersection_10k_neg_vs_10k_pos 19.56 ± 0.11 19.38 ± 0.18 -0.9%
set::intersection_10k_pos_vs_100_neg 17.08 ± 0.07 16.94 ± 0.17 -0.8%
set::intersection_10k_pos_vs_10k_neg 19.42 ± 0.05 19.20 ± 0.22 -1.1%
set::intersection_random_100_vs_100 2,765.31 ± 19.46 1,250.69 ± 7.81 -54.8%
set::intersection_random_100_vs_10k 3,149.91 ± 11.38 2,536.46 ± 43.26 -19.5%
set::intersection_random_10k_vs_100 3,140.24 ± 40.56 2,549.25 ± 46.99 -18.8%
set::intersection_random_10k_vs_10k 280,063.67 ± 1,732.87 135,042.57 ± 1,463.94 -51.8%
set::intersection_staggered_100_vs_100 2,911.66 ± 21.58 1,556.65 ± 19.25 -46.5%
set::intersection_staggered_100_vs_10k 3,082.33 ± 6.55 2,555.09 ± 18.25 -17.1%
set::intersection_staggered_10k_vs_10k 294,074.33 ± 2,705.90 164,741.10 ± 650.57 -44.0%
set::is_subset_100_vs_100 2,804.09 ± 6.16 1,025.71 ± 9.23 -63.4%
set::is_subset_100_vs_10k 2,343.67 ± 21.98 1,832.94 ± 30.82 -21.8%
set::is_subset_10k_vs_100 2.35 ± 0.01 2.46 ± 0.07 +4.7%
set::is_subset_10k_vs_10k 274,705.33 ± 381.00 102,497.67 ± 1,043.56 -62.7%

@ywxt

ywxt commented Sep 30, 2026

Copy link
Copy Markdown
Contributor Author

r? @Amanieu

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 30, 2026
@rustbot

rustbot commented Sep 30, 2026

Copy link
Copy Markdown
Collaborator

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 12 candidates
  • Random selection from Darksonn, JohnTitor, Mark-Simulacrum, clarfonthey, jhpratt

@rustbot rustbot assigned Amanieu and unassigned Mark-Simulacrum Sep 30, 2026
@Kobzol

Kobzol commented Sep 30, 2026

Copy link
Copy Markdown
Member

@bors try @rust-timer queue

Probably won't show anything, but still doesn't hurt to try.

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Sep 30, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Sep 30, 2026
fast path for BTreeMap::Iter::next
@rust-bors

rust-bors Bot commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 1f5c7be (1f5c7be770821512319689a83053c14ecda71a4b)
Base parent: 7d2cd0f (7d2cd0fbc092625ea371da704f15f80216d2220e)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (1f5c7be): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

@bors rollup=never rustc-perf
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.0% [-2.0%, -0.4%] 6
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -1.0% [-2.0%, -0.4%] 6

Max RSS (memory usage)

Results (primary 4.2%, secondary 2.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
4.2% [3.4%, 5.0%] 2
Regressions ❌
(secondary)
2.4% [2.4%, 2.4%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 4.2% [3.4%, 5.0%] 2

Cycles

Results (primary -2.4%, secondary -0.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.2% [2.2%, 2.3%] 2
Improvements ✅
(primary)
-2.4% [-2.4%, -2.4%] 1
Improvements ✅
(secondary)
-2.9% [-3.4%, -2.4%] 2
All ❌✅ (primary) -2.4% [-2.4%, -2.4%] 1

Binary size

Results (primary 0.1%, secondary -0.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.3% [0.1%, 1.2%] 9
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.1% [-0.2%, -0.0%] 13
Improvements ✅
(secondary)
-0.1% [-0.1%, -0.1%] 5
All ❌✅ (primary) 0.1% [-0.2%, 1.2%] 22

Bootstrap: 489.84s -> 489.542s (-0.06%)
Artifact size: 406.54 MiB -> 406.43 MiB (-0.03%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Sep 30, 2026
Comment thread library/alloc/src/collections/btree/navigate.rs Outdated
Comment thread library/alloc/src/collections/btree/navigate.rs Outdated

@Amanieu Amanieu left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comment nits, otherwise LGTM

Another nit: consistently use KV instead of kv in comments.

View changes since this review

Comment thread library/alloc/src/collections/btree/navigate.rs Outdated
Comment thread library/alloc/src/collections/btree/navigate.rs Outdated
@rustbot

rustbot commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@Amanieu

Amanieu commented Oct 11, 2026

Copy link
Copy Markdown
Member

@bors r+

@rust-bors

rust-bors Bot commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

📌 Commit cc65a07 has been approved by Amanieu

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 11, 2026
@rust-bors

This comment has been minimized.

@rust-bors rust-bors Bot added merged-by-bors This PR was explicitly merged by bors. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 11, 2026
@rust-bors

rust-bors Bot commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

☀️ Test successful - CI
Approved by: Amanieu
Duration: 2h 38m 38s
Pushing 0e75b0b to main...

@rust-bors
rust-bors Bot merged commit 0e75b0b into rust-lang:main Oct 11, 2026
15 checks passed
@rustbot rustbot added this to the 1.101.0 milestone Oct 11, 2026
@github-actions

Copy link
Copy Markdown
Contributor
What is this? This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.

Comparing 66b2934 (parent) -> 0e75b0b (this PR)

Test differences

Show 38 test diffs

Stage 1

  • btree::map::iteration_mut_rev_1000: [missing] -> pass (J0)
  • btree::map::iteration_mut_rev_100000: [missing] -> pass (J0)
  • btree::map::iteration_mut_rev_20: [missing] -> pass (J0)
  • btree::map::iteration_rev_1000: [missing] -> pass (J0)
  • btree::map::iteration_rev_100000: [missing] -> pass (J0)
  • btree::map::iteration_rev_20: [missing] -> pass (J0)
  • btree::map::range_iteration_1000: [missing] -> pass (J0)
  • btree::map::range_iteration_100000: [missing] -> pass (J0)
  • btree::map::range_iteration_20: [missing] -> pass (J0)
  • btree::map::range_iteration_mut_1000: [missing] -> pass (J0)
  • btree::map::range_iteration_mut_100000: [missing] -> pass (J0)
  • btree::map::range_iteration_mut_20: [missing] -> pass (J0)
  • btree::map::range_iteration_mut_rev_1000: [missing] -> pass (J0)
  • btree::map::range_iteration_mut_rev_100000: [missing] -> pass (J0)
  • btree::map::range_iteration_mut_rev_20: [missing] -> pass (J0)
  • btree::map::range_iteration_rev_1000: [missing] -> pass (J0)
  • btree::map::range_iteration_rev_100000: [missing] -> pass (J0)
  • btree::map::range_iteration_rev_20: [missing] -> pass (J0)

Stage 2

  • btree::map::iteration_mut_rev_1000: [missing] -> pass (J1)
  • btree::map::iteration_mut_rev_100000: [missing] -> pass (J1)
  • btree::map::iteration_mut_rev_20: [missing] -> pass (J1)
  • btree::map::iteration_rev_1000: [missing] -> pass (J1)
  • btree::map::iteration_rev_100000: [missing] -> pass (J1)
  • btree::map::iteration_rev_20: [missing] -> pass (J1)
  • btree::map::range_iteration_1000: [missing] -> pass (J1)
  • btree::map::range_iteration_100000: [missing] -> pass (J1)
  • btree::map::range_iteration_20: [missing] -> pass (J1)
  • btree::map::range_iteration_mut_1000: [missing] -> pass (J1)
  • btree::map::range_iteration_mut_100000: [missing] -> pass (J1)
  • btree::map::range_iteration_mut_20: [missing] -> pass (J1)
  • btree::map::range_iteration_mut_rev_1000: [missing] -> pass (J1)
  • btree::map::range_iteration_mut_rev_100000: [missing] -> pass (J1)
  • btree::map::range_iteration_mut_rev_20: [missing] -> pass (J1)
  • btree::map::range_iteration_rev_1000: [missing] -> pass (J1)
  • btree::map::range_iteration_rev_100000: [missing] -> pass (J1)
  • btree::map::range_iteration_rev_20: [missing] -> pass (J1)

Additionally, 2 doctest diffs were found. These are ignored, as they are noisy.

Job group index

Test dashboard

Run

cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard 0e75b0b0e6fe097f388bc49c80cf7fb2b7aa9f1a --output-dir test-dashboard

And then open test-dashboard/index.html in your browser to see an overview of all executed tests.

Job duration changes

  1. dist-s390x-linux: 1h 1m -> 1h 33m (+52.8%)
  2. test-x86_64-gnu-llvm-22-2: 1h 7m -> 1h 43m (+52.3%)
  3. test-x86_64-mingw-2: 2h 58m -> 1h 44m (-41.7%)
  4. test-x86_64-gnu-distcheck: 2h 22m -> 1h 23m (-41.5%)
  5. test-x86_64-gnu-next-trait-solver-polonius: 1h 3m -> 37m 16s (-41.4%)
  6. test-pr-check-2: 41m 38s -> 25m 1s (-39.9%)
  7. test-x86_64-gnu-aux: 2h 42m -> 1h 38m (-39.2%)
  8. dist-x86_64-msvc-alt: 2h 50m -> 1h 45m (-38.4%)
  9. test-x86_64-gnu-stdlib-semver-check: 16m 39s -> 10m 38s (-36.1%)
  10. test-i686-gnu-1: 2h 17m -> 1h 27m (-36.0%)
How to interpret the job duration changes?

Job durations can vary a lot, based on the actual runner instance
that executed the job, system noise, invalidated caches, etc. The table above is provided
mostly for t-infra members, for simpler debugging of potential CI slow-downs.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (0e75b0b): comparison URL.

Overall result: ✅ improvements - no action needed

@rustbot label: -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.7% [0.7%, 0.7%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.5% [-1.2%, -0.2%] 6
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.4% [-1.2%, 0.7%] 7

Max RSS (memory usage)

Results (primary 7.4%, secondary 8.7%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
7.4% [7.4%, 7.4%] 1
Regressions ❌
(secondary)
8.7% [6.3%, 11.0%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 7.4% [7.4%, 7.4%] 1

Cycles

Results (primary -0.1%, secondary 1.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.1% [2.1%, 2.1%] 1
Regressions ❌
(secondary)
5.7% [2.9%, 8.5%] 2
Improvements ✅
(primary)
-2.3% [-2.3%, -2.3%] 1
Improvements ✅
(secondary)
-3.2% [-3.7%, -2.8%] 2
All ❌✅ (primary) -0.1% [-2.3%, 2.1%] 2

Binary size

Results (primary 0.1%, secondary 0.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.2% [0.1%, 1.4%] 32
Regressions ❌
(secondary)
0.1% [0.0%, 0.1%] 34
Improvements ✅
(primary)
-0.1% [-0.2%, -0.0%] 5
Improvements ✅
(secondary)
-0.0% [-0.0%, -0.0%] 1
All ❌✅ (primary) 0.1% [-0.2%, 1.4%] 37

Bootstrap: 488.016s -> 488.837s (0.17%)
Artifact size: 407.15 MiB -> 406.51 MiB (-0.16%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged-by-bors This PR was explicitly merged by bors. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants