Commit bf3a87c
authored
Rollup merge of #160813 - fereidani:linked_list_optimization, r=clarfonthey
Optimize linked list iterator performance
This PR optimizes the linked list iterator performance by removing an extra branch check.
This resulted in less instructions for single access(`last()`, `next()`, etc.) and optimal iterator optimization(results in longer instructions) as the compiler requires only check for `len==0` exit, I expect slight binary-size increase for much better performance.
I did my best to find a counter-example where `len > 0` but head or tail are not initialized, I couldn't. Could you please double-check the claim just in case I missed something?
I tested it using `alloctests` available benchmarks too, It seems that compiler is optimizing the benchmark test away, results seems weird(too good), I tried using `black_box` around the object but results stayed the same(0.18ns/iter from 60.49ns/iter):
```rust
#[bench]
fn bench_iter(b: &mut Bencher) {
let v = &[0; 128];
let m: LinkedList<_> = v.iter().cloned().collect();
b.iter(|| {
assert!(black_box(&m).iter().count() == 128);
})
}
```
*9950x results:*
Current:
```
linked_list::bench_collect_into 396.14ns/iter +/- 3.12
linked_list::bench_iter 60.49ns/iter +/- 0.91
linked_list::bench_iter_mut 60.32ns/iter +/- 0.97
linked_list::bench_iter_mut_rev 61.72ns/iter +/- 0.06
linked_list::bench_iter_rev 59.61ns/iter +/- 0.08
linked_list::bench_push_back 10.06ns/iter +/- 0.33
linked_list::bench_push_back_pop_back 4.54ns/iter +/- 0.12
linked_list::bench_push_front 2.87ns/iter +/- 0.05
linked_list::bench_push_front_pop_front 4.52ns/iter +/- 0.05
```
New:
```
linked_list::bench_collect_into 391.06ns/iter +/- 24.49
linked_list::bench_iter 0.18ns/iter +/- 0.00
linked_list::bench_iter_mut 0.18ns/iter +/- 0.00
linked_list::bench_iter_mut_rev 0.18ns/iter +/- 0.00
linked_list::bench_iter_rev 0.18ns/iter +/- 0.00
linked_list::bench_push_back 10.11ns/iter +/- 0.18
linked_list::bench_push_back_pop_back 4.35ns/iter +/- 0.10
linked_list::bench_push_front 3.27ns/iter +/- 0.46
linked_list::bench_push_front_pop_front 4.66ns/iter +/- 0.04
```2 files changed
Lines changed: 72 additions & 50 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
| 18 | + | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| |||
1201 | 1201 | | |
1202 | 1202 | | |
1203 | 1203 | | |
1204 | | - | |
1205 | | - | |
1206 | | - | |
1207 | | - | |
1208 | | - | |
1209 | | - | |
1210 | | - | |
1211 | | - | |
1212 | | - | |
| 1204 | + | |
1213 | 1205 | | |
| 1206 | + | |
| 1207 | + | |
| 1208 | + | |
| 1209 | + | |
| 1210 | + | |
| 1211 | + | |
| 1212 | + | |
| 1213 | + | |
| 1214 | + | |
| 1215 | + | |
1214 | 1216 | | |
1215 | 1217 | | |
1216 | 1218 | | |
| |||
1229 | 1231 | | |
1230 | 1232 | | |
1231 | 1233 | | |
1232 | | - | |
1233 | | - | |
1234 | | - | |
1235 | | - | |
1236 | | - | |
1237 | | - | |
1238 | | - | |
1239 | | - | |
1240 | | - | |
| 1234 | + | |
1241 | 1235 | | |
| 1236 | + | |
| 1237 | + | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
1242 | 1246 | | |
1243 | 1247 | | |
1244 | 1248 | | |
| |||
1248 | 1252 | | |
1249 | 1253 | | |
1250 | 1254 | | |
| 1255 | + | |
| 1256 | + | |
| 1257 | + | |
1251 | 1258 | | |
1252 | 1259 | | |
1253 | 1260 | | |
| |||
1269 | 1276 | | |
1270 | 1277 | | |
1271 | 1278 | | |
1272 | | - | |
1273 | | - | |
1274 | | - | |
1275 | | - | |
1276 | | - | |
1277 | | - | |
1278 | | - | |
1279 | | - | |
1280 | | - | |
| 1279 | + | |
1281 | 1280 | | |
| 1281 | + | |
| 1282 | + | |
| 1283 | + | |
| 1284 | + | |
| 1285 | + | |
| 1286 | + | |
| 1287 | + | |
| 1288 | + | |
| 1289 | + | |
| 1290 | + | |
1282 | 1291 | | |
1283 | 1292 | | |
1284 | 1293 | | |
| |||
1297 | 1306 | | |
1298 | 1307 | | |
1299 | 1308 | | |
1300 | | - | |
1301 | | - | |
1302 | | - | |
1303 | | - | |
1304 | | - | |
1305 | | - | |
1306 | | - | |
1307 | | - | |
1308 | | - | |
| 1309 | + | |
1309 | 1310 | | |
| 1311 | + | |
| 1312 | + | |
| 1313 | + | |
| 1314 | + | |
| 1315 | + | |
| 1316 | + | |
| 1317 | + | |
| 1318 | + | |
| 1319 | + | |
| 1320 | + | |
1310 | 1321 | | |
1311 | 1322 | | |
1312 | 1323 | | |
| |||
1316 | 1327 | | |
1317 | 1328 | | |
1318 | 1329 | | |
| 1330 | + | |
| 1331 | + | |
| 1332 | + | |
1319 | 1333 | | |
1320 | 1334 | | |
1321 | 1335 | | |
| |||
2032 | 2046 | | |
2033 | 2047 | | |
2034 | 2048 | | |
| 2049 | + | |
| 2050 | + | |
| 2051 | + | |
2035 | 2052 | | |
2036 | 2053 | | |
2037 | 2054 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
47 | 55 | | |
48 | 56 | | |
49 | | - | |
50 | | - | |
| 57 | + | |
51 | 58 | | |
52 | | - | |
| 59 | + | |
53 | 60 | | |
54 | 61 | | |
| 62 | + | |
55 | 63 | | |
56 | 64 | | |
57 | | - | |
58 | | - | |
| 65 | + | |
59 | 66 | | |
60 | | - | |
| 67 | + | |
61 | 68 | | |
62 | 69 | | |
63 | 70 | | |
64 | 71 | | |
65 | | - | |
66 | | - | |
| 72 | + | |
67 | 73 | | |
68 | | - | |
| 74 | + | |
69 | 75 | | |
70 | 76 | | |
71 | 77 | | |
72 | 78 | | |
73 | | - | |
74 | | - | |
| 79 | + | |
75 | 80 | | |
76 | | - | |
| 81 | + | |
77 | 82 | | |
78 | 83 | | |
0 commit comments