GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-150942: Speed up re.findall and re.sub/subn result building by eendebakpt · Pull Request #150943 · python/cpython

Conversation

@eendebakpt

@eendebakpt eendebakpt commented Jun 5, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Append result items to the output list with _PyList_AppendTakeRef instead of PyList_Append followed by Py_DECREF, removing a reference-count round-trip per appended item (and a per-append lock on the free-threaded build).

Microbenchmarks

Benchmark main this PR speedup
re_sub 1.93 ms 1.73 ms 1.11×
re_subn 1.92 ms 1.73 ms 1.11×
re_findall 1.89 ms 1.80 ms 1.05×
re_findall_groups 4.01 ms 3.87 ms 1.04×
geomean 1.06×
Benchmark script
"""Microbenchmark re.findall / split / sub / subn (result-list building)."""
import re
import pyperf

WORDS = "the quick brown fox jumps over the lazy dog " * 2000
CSVISH = "field1,field2,field3,field4,field5,field6,field7,field8\n" * 3000

word_re = re.compile(r"\w+")
group_re = re.compile(r"(\w)(\w+)")
split_re = re.compile(r"[,\n]")
sub_re = re.compile(r"\w+")


def b_findall():        return word_re.findall(WORDS)
def b_findall_groups(): return group_re.findall(WORDS)   # list of tuples
def b_sub():            return sub_re.sub("X", WORDS)
def b_subn():           return sub_re.subn("X", WORDS)


if __name__ == "__main__":
    runner = pyperf.Runner()
    runner.bench_func("re_findall", b_findall)
    runner.bench_func("re_findall_groups", b_findall_groups)
    runner.bench_func("re_sub", b_sub)
    runner.bench_func("re_subn", b_subn)
  • Issue: Improve performance by using reference stealing methods #150942

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@eendebakpt @claude
gh-XXXXX: Speed up re.findall and re.sub/subn result building
191730a
Append result items to the output list with _PyList_AppendTakeRef instead
of PyList_Append followed by Py_DECREF, removing a reference-count
round-trip per appended item (and a per-append lock on the free-threaded
build).  Applied to the result lists built by findall and the sub/subn
helper, where the append is a meaningful share of the work.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@bedevere-app bedevere-app Bot added the awaiting review label Jun 5, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Jun 5, 2026
Improve performance by using reference stealing methods #150942
Open
@eendebakpt @claude
pythongh-150942: Set issue number on the re speedup news entry
bf25fc5
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
sergey-miryanov
sergey-miryanov approved these changes Jun 5, 2026
View reviewed changes

@sergey-miryanov sergey-miryanov left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Looks good to me

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot added awaiting core review and removed awaiting review labels Jun 5, 2026
corona10
corona10 approved these changes Jun 6, 2026
View reviewed changes

@corona10 corona10 left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM. The code looks simpler, and it also brings a performance improvement.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Jun 6, 2026
@corona10
corona10 merged commit 0f7dc2f into python:main Jun 7, 2026
64 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Jun 7, 2026
philthompson10 pushed a commit to philthompson10/cpython that referenced this pull request Jun 17, 2026
@eendebakpt @philthompson10
pythongh-150942: Speed up re.findall and re.sub/subn result building (p…
a0d73f3
…ythongh-150943)
@snurf198 snurf198 mentioned this pull request Aug 17, 2026
gh-150942: Speed up re.split result building #155940
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@sergey-miryanov sergey-miryanov sergey-miryanov approved these changes
@corona10 corona10 corona10 approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@eendebakpt @sergey-miryanov @corona10
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.