Skip to content

Avoid bound method allocation in PyFile_WriteObject - #160803

Open
methane wants to merge 1 commit into
python:mainfrom
methane:codex/file-writeobject-vectorcall
Open

methane wants to merge 1 commit into
python:mainfrom
methane:codex/file-writeobject-vectorcall

Conversation

@methane

@methane methane commented Oct 28, 2026

Copy link
Copy Markdown
Member

This simplifies the implementation of PyFile_WriteObject() while also improving its performance.

ref: #160727 (comment)

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Conversion can destroy a borrowed stream before it is used, and delaying the write lookup changes observable behavior.

2 open issues
What changed in this PR

This PR simplifies CPython’s PyFile_WriteObject() and aims to reduce write-call overhead by avoiding bound-method allocation.

Changes:

  • Replaces explicit write attribute lookup and invocation with PyObject_CallMethodOneArg().
  • Simplifies string conversion and removes writer-reference cleanup.
File Description
Objects/​fileobject.c Streamlines value conversion and the write call in PyFile_WriteObject().

Review effort: Balanced

Comment thread Objects/fileobject.c
else
value = PyObject_Repr(v);

PyObject *value = (flags & Py_PRINT_RAW) ? PyObject_Str(v) : PyObject_Repr(v);
Comment thread Objects/fileobject.c
return -1;
}
result = PyObject_CallOneArg(writer, value);
PyObject *result = PyObject_CallMethodOneArg(f, &_Py_ID(write), value);
@methane
methane force-pushed the codex/file-writeobject-vectorcall branch from d7498af to a026992 Compare October 28, 2026 07:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants