Commit e12f43f
authored
feat!: add lazy BTreeMap iterator for improved performance (breaking API change) (#375)
This PR changes the `BTreeMap` iterator's `Item` type from `(K, V)` to
`LazyEntry` to enable lazy evaluation of keys and values.
This is a **BREAKING API CHANGE**, as it deviates from the canonical
`(K, V)` pattern of `std::collections::BTreeMap`.
The key motivation is **performance**: previously, each `next()` call
eagerly deserialized and cloned key and value, even if the consumer
discarded them. With `LazyEntry`, key and value access is deferred until
explicitly requested, significantly reducing unnecessary allocations and
cloning in cases where only part of the entry is needed or iteration is
short-circuited.
Code change summary:
- Introduced `LazyEntry` with on-demand access to key/value.
- Replaced `Box<Node<K>>` with `Rc<Node<K>>` to allow shared ownership
between iterator and entries.
- Updated all internal iterator consumers to explicitly call
`.into_pair()` where eager materialization is needed.
Performance change summary:
- `BTreeSet`: 68 improvements, no regressions. Median instruction count
dropped by **-6.61%**, best case **-17.24%**
- `BTreeMap`: 1 small regression (+2.09%), 10 improvements, with up to
**-98.33%** fewer instructions in range-heavy cases1 parent efc567e commit e12f43f
File tree
10 files changed
+504
-395
lines changed- benchmarks
- btreemap
- src
- btreeset
- nns
- src
- src
- btreemap
- tests
10 files changed
+504
-395
lines changedLarge diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
777 | 777 | | |
778 | 778 | | |
779 | 779 | | |
780 | | - | |
| 780 | + | |
781 | 781 | | |
782 | 782 | | |
783 | 783 | | |
| |||
794 | 794 | | |
795 | 795 | | |
796 | 796 | | |
797 | | - | |
798 | | - | |
| 797 | + | |
| 798 | + | |
799 | 799 | | |
800 | 800 | | |
801 | 801 | | |
| |||
0 commit comments