Repository navigation
Conversation
list() allocated its result with PyType_GenericAlloc(), bypassing the freelist that PyList_New() and the [] display use. For an exact list, allocate with PyList_New(0) instead. The object handed to list.__init__() is in the same state either way: empty, with no item array, and tracked by the GC. Subclasses still use PyType_GenericAlloc(). pyperf, PGO+LTO, base 19270d3, two interleaved rounds per side (a third round, disturbed by machine load, was dropped): | Benchmark | main | branch | |----------------|:-------:|:---------------------:| | list() | 21.2 ns | 15.3 ns: 1.38x faster | | list(d) | 36.1 ns | 27.4 ns: 1.32x faster | | list(t) | 30.3 ns | 23.9 ns: 1.27x faster | | list(l) | 32.0 ns | 25.7 ns: 1.24x faster | | list(r) | 43.6 ns | 36.0 ns: 1.21x faster | | x + 1 | 10.4 ns | 9.70 ns: 1.07x faster | | len(l) | 9.87 ns | 9.53 ns: 1.03x faster | | l[0] | 8.24 ns | 8.00 ns: 1.03x faster | | L(t) | 48.2 ns | 47.4 ns: 1.02x faster | | tuple(l) | 15.4 ns | 16.0 ns: 1.04x slower | | Geometric mean | (ref) | 1.13x faster | Not significant: []. x + 1, len(l), l[0], L(t) and tuple(l) don't run the changed code; their movement is layout noise, within the up-to-1.07x spread between two runs of the same main build. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
cmaloney
commented
Oct 29, 2026
|
|
||
| PyObject *list = PyType_GenericAlloc(_PyType_CAST(type), 0); | ||
| /* use PyList_New() to make exact lists from the freelist. */ | ||
| PyObject *list = (type == (PyObject *)&PyList_Type |
Contributor
Author
There was a problem hiding this comment.
This should always be true because tp_vectorcall is not inherited but with the handwritten vector call, it isn't asserted in debug builds
corona10
reviewed
Oct 29, 2026
Comment on lines
+1
to
+2
| Speed up :class:`list` construction by reusing list objects from the free | ||
| list, as ``[]`` already does. |
Member
There was a problem hiding this comment.
Does not need to be known to end user.
methane
approved these changes
Oct 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
list() allocated its result with PyType_GenericAlloc(), bypassing the freelist that PyList_New() and the [] display use. For an exact list, allocate with PyList_New(0) instead. The object handed to list.init() is in the same state either way: empty, with no item array, and tracked by the GC. Subclasses still use PyType_GenericAlloc().
pyperf, PGO+LTO, base 19270d3 :
Not significant: []. x + 1, len(l), l[0], L(t) and tuple(l) don't run the changed code; their movement is layout noise, within the up-to-1.07x spread between two runs of the same main build.
list()to use the existing freelist #160858