Skip to content

directly drop the subtree instead of traversing every element when BTreemap dropping - #162931

Open
ywxt wants to merge 1 commit into
rust-lang:mainfrom
ywxt:btree_map_drop
Open

ywxt wants to merge 1 commit into
rust-lang:mainfrom
ywxt:btree_map_drop

Conversation

@ywxt

@ywxt ywxt commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

When both types of BTreeMap keys and values don't have destructors, currently it converts the tree to an iterator to traverse while dropping. However it's unnecessary. We instead can deallocate nodes of the tree directly.

Results on aarch64:
btree::map

benchmark baseline ns/iter new ns/iter change
clone_fat_val_100 10849.14 7433.38 -31.5%
clone_fat_val_100_and_clear 11021.05 7606.91 -31.0%
clone_fat_val_100_and_drain_all 57652.20 55026.07 -4.6%
clone_fat_val_100_and_drain_half 27258.25 23993.80 -12.0%
clone_fat_val_100_and_into_iter 15149.81 12289.52 -18.9%
clone_fat_val_100_and_pop_all 56717.62 54045.12 -4.7%
clone_fat_val_100_and_remove_all 64333.00 61907.57 -3.8%
clone_fat_val_100_and_remove_half 28440.43 25290.79 -11.1%
clone_slim_100 1978.26 687.38 -65.3%
clone_slim_100_and_clear 1908.35 687.58 -64.0%
clone_slim_100_and_drain_all 3983.43 4244.24 +6.5%
clone_slim_100_and_drain_half 4241.43 3551.33 -16.3%
clone_slim_100_and_into_iter 1805.39 1753.37 -2.9%
clone_slim_100_and_pop_all 3243.54 3247.17 +0.1%
clone_slim_100_and_remove_all 5203.97 5176.58 -0.5%
clone_slim_100_and_remove_half 2399.04 1818.85 -24.2%
clone_slim_10k 205051.50 74942.50 -63.5%
clone_slim_10k_and_clear 197575.50 74994.75 -62.0%
clone_slim_10k_and_drain_all 425268.00 424679.50 -0.1%
clone_slim_10k_and_drain_half 408679.00 358468.67 -12.3%
clone_slim_10k_and_into_iter 184573.50 180805.00 -2.0%
clone_slim_10k_and_pop_all 369476.00 370334.50 +0.2%
clone_slim_10k_and_remove_all 582219.44 581719.69 -0.1%
clone_slim_10k_and_remove_half 424686.50 373048.00 -12.2%
from_iter_rand_100 1832.99 1086.70 -40.7%
from_iter_rand_10_000 304447.00 231011.33 -24.1%
from_iter_seq_100 2045.17 750.12 -63.3%
from_iter_seq_10_000 211268.33 80514.29 -61.9%

btree::set

benchmark baseline ns/iter new ns/iter change
clone_100 1913.17 605.95 -68.3%
clone_100_and_clear 1813.35 608.09 -66.5%
clone_100_and_drain_all 3960.52 3965.28 +0.1%
clone_100_and_drain_half 4045.63 3408.90 -15.7%
clone_100_and_into_iter 1676.32 1600.11 -4.5%
clone_100_and_pop_all 2582.26 2562.19 -0.8%
clone_100_and_remove_all 4574.45 4520.84 -1.2%
clone_100_and_remove_half 2107.02 1533.22 -27.2%
clone_10k 192719.50 65933.38 -65.8%
clone_10k_and_clear 184669.60 66101.87 -64.2%
clone_10k_and_drain_all 418832.00 417920.00 -0.2%
clone_10k_and_drain_half 400446.00 348149.50 -13.1%
clone_10k_and_into_iter 170359.60 163481.80 -4.0%
clone_10k_and_pop_all 306021.67 307441.67 +0.5%
clone_10k_and_remove_all 510750.88 511582.56 +0.2%
clone_10k_and_remove_half 385672.00 337072.33 -12.6%

r? @Amanieu

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 18, 2026
@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Sep 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants