Commit f4a13b1
committed
gh-158790: Improve performance for list.insert() and del list[i] in free-threaded builds
`ins1()`(used by `list.insert()`) and `list_ass_item_lock_held()` shift the list with one atomic release store per element in free-threaded builds. Those atomic stores prevent the compiler from vectorizing the loops, making them much slower than `memmove()` for large shifts. I propose using the existing `ptr_wise_atomic_memmove()` helper here. It uses `memmove()` as a fast path when the list is owned by the current thread and is not shared, and keeps the atomic element-wise stores for lists that other threads can observe.
The default (GIL) build keeps the current loops because the compiler can optimize them directly in the calling function, and they perform better in benchmarks.
pyperf, free-threaded release build (--disable-gil, -O3):
del l[0] 2.26x faster
del l[len(l)//2] 2.14x faster
l.insert(0, x) 1.95x faster
l.insert(len(l)//2, x) 1.86x faster
insert(0)+del[0], n=1000 2.86x faster
sliding window, n=1000 2.46x faster
Compiling both versions in GIL mode produces the same code for `ins1()` and `list_ass_item_lock_held()`, so the default build is not affected.
This continues gh-129069, which introduced `ptr_wise_atomic_memmove()`.1 parent 9d22a53 commit f4a13b1
2 files changed
Lines changed: 22 additions & 2 deletions
File tree
- Misc/NEWS.d/next/Core_and_Builtins
- Objects
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
478 | 478 | | |
479 | 479 | | |
480 | 480 | | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
481 | 484 | | |
482 | 485 | | |
483 | 486 | | |
484 | | - | |
| 487 | + | |
485 | 488 | | |
486 | 489 | | |
487 | 490 | | |
| |||
500 | 503 | | |
501 | 504 | | |
502 | 505 | | |
503 | | - | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
504 | 515 | | |
| 516 | + | |
505 | 517 | | |
506 | 518 | | |
507 | 519 | | |
| |||
1145 | 1157 | | |
1146 | 1158 | | |
1147 | 1159 | | |
| 1160 | + | |
| 1161 | + | |
| 1162 | + | |
| 1163 | + | |
| 1164 | + | |
| 1165 | + | |
1148 | 1166 | | |
1149 | 1167 | | |
1150 | 1168 | | |
| 1169 | + | |
1151 | 1170 | | |
1152 | 1171 | | |
1153 | 1172 | | |
| |||
0 commit comments