Commit f08e936
perf: Optimize BTree search using binary search (rust-lang#767)
* perf: Optimize BTree search using binary search
This commit optimizes the search function in the BTree implementation
by replacing the linear search with a binary search algorithm. This
change significantly improves the search performance, especially for
large trees.
Implementation details:
- Modified the `search` method in the `BTree` struct
- Replaced the while loop with `binary_search` method on the `keys` vector
Complexity analysis:
- Previous implementation: O(n) per node, where n is the number of keys
- New implementation: O(log n) per node
Benchmark results:
- Environment: MacOS 14.5, Apple M1 Pro, 32 GB RAM
- Dataset: 1,000,000 random integers for insertion
- Search: 1,000,000 searches for the key 500,000
- Before:
- Insertion: 3.002587333s
- Search: 2.334683584s
- After:
- Insertion: 2.998482583s
- Search: 288.659458ms
Note: Insertion time remains largely unchanged, as expected. All
existing tests pass with the new implementation.
Benchmark code is not included in this commit.
* tests: expand existing `test_search`
---------
Co-authored-by: Piotr Idzik <[email protected]>1 parent 65ca19d commit f08e936
1 file changed
+50
-27
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
146 | 146 | | |
147 | 147 | | |
148 | 148 | | |
149 | | - | |
150 | 149 | | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
163 | 159 | | |
164 | 160 | | |
165 | 161 | | |
| |||
169 | 165 | | |
170 | 166 | | |
171 | 167 | | |
172 | | - | |
173 | | - | |
174 | | - | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
186 | 209 | | |
187 | 210 | | |
0 commit comments