Skip to content

gh-158660: Avoid locking for new lists with preallocated space - #160156

Draft
ZeroIntensity wants to merge 1 commit into
python:mainfrom
ZeroIntensity:tricky-list
Draft

ZeroIntensity wants to merge 1 commit into
python:mainfrom
ZeroIntensity:tricky-list

Conversation

@ZeroIntensity

@ZeroIntensity ZeroIntensity commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

The idea here is that when a list is created with PyList_New(n) where n is greater than zero, the list cannot be shared across threads, because it's unusable as an object (due to the NULL pointers that it would try to access upon use), and therefore we can skip locking.

This was generated by AI without my review; I'm putting this up as a proof of concept that we can use for benchmarking and such.

@github-project-automation github-project-automation Bot moved this to Todo in Sprint Oct 22, 2026
@ZeroIntensity ZeroIntensity changed the title gh-158660: Avoid locking for newly created lists gh-158660: Avoid locking for new lists with preallocated space Oct 22, 2026
@ZeroIntensity

Copy link
Copy Markdown
Member Author

Benchmarks, using Nathan's fill_list script from the issue:

  • This PR, free-threaded: 3.4 ns/item
  • Main, free-threaded: 4.6 ns/item
  • This PR, GIL-icious: 2.5 ns/item
  • Main, GIL-icious: 2.5 ns/item

So, with this fix, PyList_SetItem is now about 35% slower than the default build, in contrast to about 84% slower without it. I'll look into ways to optimize this further tomorrow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

1 participant