Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-139103: Use borrowed references for positional args in _PyStack_UnpackDict#144407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
colesbury wants to merge1 commit intopython:main
base:main
Choose a base branch
Loading
fromcolesbury:gh-139103-stack-unpack-dict-borrowed

Conversation

@colesbury
Copy link
Contributor

@colesburycolesbury commentedFeb 2, 2026
edited
Loading

The positional arguments passed to _PyStack_UnpackDict are already kept alive by the caller, so we can avoid the extra reference count operations by using borrowed references instead of creating new ones.

This reduces reference count contention in the free-threaded build when calling functions with keyword arguments. In particular, this avoids contention on the type argument to__new__ when instantiating namedtuples with keyword arguments.

…ack_UnpackDictThe positional arguments passed to _PyStack_UnpackDict are alreadykept alive by the caller, so we can avoid the extra reference countoperations by using borrowed references instead of creating new ones.This reduces reference count contention in the free-threaded buildwhen calling functions with keyword arguments. In particular, thisavoids contention on the type argument to `__new__` when instantiatingnamedtuples with keyword arguments.
@colesburycolesbury changed the titlegh-139103: Use borrowed references for positional args in _PyStack_Un…gh-139103: Use borrowed references for positional args in _PyStack_UnpackDictFeb 2, 2026
Copy link
Member

@vstinnervstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

LGTM.

The positional arguments are borrowed references from the input array
(which must be kept alive by the caller). The keyword argument values
are new references.

IMO it's a reasonable assumption. If there are bugs, the caller should be modified to hold a strong reference to its arguments.

_PyStack_UnpackDict() callers:

  • _PyObject_VectorcallDictTstate():PyObject *const *args
  • _PyVectorcall_Call():PyObject *tuple
  • _PyEvalFramePushAndInit_Ex():PyObject *callargs (tuple)

_PyObject_VectorcallDictTstate() callers:

  • PyObject_VectorcallDict():PyObject *const *args
  • PyEval_CallObjectWithKeywords(): passNULL forargs
  • _PyObject_Call_Prepend():PyObject *args (tuple)

Most calls take their positional arguments from a tuple (safe). The most risky code path are calls to PyObject_VectorcallDict(): the change makes the assumption that this function is only called with strong references.

@colesbury
Copy link
ContributorAuthor

The most risky code path are calls to PyObject_VectorcallDict(): the change makes the assumption that this function is only called with strong references

FWIW,PyObject_VectorcallDict already implicitly has this assumption because we don't incref theargs in the no-keyword case:

cpython/Objects/call.c

Lines 133 to 136 in29acc08

PyObject*res;
if (kwargs==NULL||PyDict_GET_SIZE(kwargs)==0) {
res=func(callable,args,nargsf,NULL);
}

vstinner reacted with thumbs up emoji

@colesburycolesbury marked this pull request as ready for reviewFebruary 2, 2026 21:44
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@vstinnervstinnervstinner approved these changes

@markshannonmarkshannonAwaiting requested review from markshannonmarkshannon is a code owner

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@colesbury@vstinner

[8]ページ先頭

©2009-2026 Movatter.jp