Skip to content

Improve performance by using reference stealing methods #150942

@eendebakpt

Description

@eendebakpt

Feature or enhancement

Proposal:

We can improve performance of cpython by using stealing methods (_PyList_AppendTakeRef, _PyDict_SetItem_Take2, PyTuple_FromPairSteal etc.) instead of the normal ones. Using stealing methods we typically avoid an incref/decref pair and sometimes error handling is simplified. We should only make changes where it makes a performance impact, the code is not more complex and the modules or methods affected are performance critical.

A list of possible cases (there might be more). The issue is open for anyone to work on.

Location Module / context Helper
Modules/_csv.c:688 csv reader, per field _PyList_AppendTakeRef
Modules/pyexpat.c:472 pyexpat StartElement, per attribute _PyDict_SetItem_Take2
Modules/_elementtree.c:3351 ElementTree C accel attrib dict _PyDict_SetItem_Take2
Modules/_io/bytesio.c:577 io.BytesIO.readlines _PyList_AppendTakeRef
Modules/_io/iobase.c:756 io.IOBase.readlines (generic) _PyList_AppendTakeRef
Objects/frameobject.c:639 & 658 frame f_locals / vars() / tracing _PyList_AppendTakeRef
Modules/_pickle.c:5138 pickle Pickler memo copy _PyDict_SetItem_Take2
Modules/_io/textio.c:2337 & 2343 io.TextIOWrapper readline (multi-chunk) _PyList_AppendTakeRef
Modules/_asynciomodule.c:1213 asyncio future done-callback compaction _PyList_AppendTakeRef

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    easyextension-modulesC modules in the Modules dirperformancePerformance or resource usagetype-featureA feature request or enhancement
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions