python / python/cpython

Minor optimization for set object, swap bodies

Open
#142,659 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core performance type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Based on this observation:

https://github.com/python/cpython/pull/132290#discussion_r2403137517

set_swap_bodies is only called in two locations in this file. For both cases the second argument b is a newly created temporary which is discarded afterwards. So we could replace part (all?) of these branches with asserts. Part of the other work (e.g. copying back to b) might not be needed for the same reason.

We should investigate this idea and see if it's worth it.

Linked PRs
  • gh-148155

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading set_swap_bodies and its two call sites in the referenced file, then review the linked discussion in PR 132290. Check whether the temporary second argument is always discarded and whether copying back is unnecessary. Done means determining and documenting whether the proposed simplification preserves behavior; PR 148155 indicates work is already underway.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.