René's URL Explorer Experiment


Title: Speed up multiline regexes anchored by `^`. · Issue #148762 · python/cpython · GitHub

Open Graph Title: Speed up multiline regexes anchored by `^`. · Issue #148762 · python/cpython

X Title: Speed up multiline regexes anchored by `^`. · Issue #148762 · python/cpython

Description: Feature or enhancement Proposal: I noticed that for regexes of the form regex = re.compile("^foo", re.MULTILINE) regex.search(...) there's a character by character loop calling SRE(match) every iteration. That's significantly slower than...

Open Graph Description: Feature or enhancement Proposal: I noticed that for regexes of the form regex = re.compile("^foo", re.MULTILINE) regex.search(...) there's a character by character loop calling SRE(match) every ite...

X Description: Feature or enhancement Proposal: I noticed that for regexes of the form regex = re.compile("^foo", re.MULTILINE) regex.search(...) there's a character by character loop calling SRE(ma...

Opengraph URL: https://github.com/python/cpython/issues/148762

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Speed up multiline regexes anchored by `^`.","articleBody":"# Feature or enhancement\n\n### Proposal:\n\nI noticed that for regexes of the form\n\n```python\nregex = re.compile(\"^foo\", re.MULTILINE)\nregex.search(...)\n```\n\nthere's a character by character loop calling `SRE(match)` every iteration. That's significantly slower than the regex \n\n```python\nregex = re.compile(\"foo...\")\nregex.search(...)\n```\n\nwhich does a special prefix scan for `\"foo\"` without having to call `SRE(match)` on each character:\n\nhttps://github.com/python/cpython/blob/42d645a7e81e0a5e6e0d35e222a8520450ac28ef/Modules/_sre/sre_lib.h#L1747\n\nhttps://github.com/python/cpython/blob/42d645a7e81e0a5e6e0d35e222a8520450ac28ef/Modules/_sre/sre_lib.h#L1775-L1776\n\nI would expect `^foo` and `foo` to have more or less identical performance. A simple patch like this fixes that.\n\n```diff\ndiff --git a/Modules/_sre/sre_lib.h b/Modules/_sre/sre_lib.h\nindex df377905bfa..70de4cccefd 100644\n--- a/Modules/_sre/sre_lib.h\n+++ b/Modules/_sre/sre_lib.h\n@@ -1855,6 +1855,18 @@ SRE(search)(SRE_STATE* state, SRE_CODE* pattern)\n             return 0;\n         }\n         while (status == 0 && ptr \u003c end) {\n+            if (pattern[0] == SRE_OP_AT &&\n+                pattern[1] == SRE_AT_BEGINNING_LINE &&\n+                (void*) ptr > state->beginning &&\n+                !SRE_IS_LINEBREAK((int) ptr[-1]))\n+            {\n+                /* fast-forward to the next newline character */\n+                while (ptr \u003c end && !SRE_IS_LINEBREAK((int) *ptr)) {\n+                    ptr++;\n+                }\n+                if (ptr >= end) {\n+                    return 0;\n+                }\n+            }\n             ptr++;\n             RESET_CAPTURE_GROUP();\n             TRACE((\"|%p|%p|SEARCH\\n\", pattern, ptr));\n\n```\n\n\n\n### Has this already been discussed elsewhere?\n\nThis is a minor feature, which does not need previous discussion elsewhere\n\n### Links to previous discussion of this feature:\n\n_No response_\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-148778\n* gh-152339\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/haampie","@type":"Person","name":"haampie"},"datePublished":"2026-04-19T18:05:39Z","url":"https://github.com/python/cpython/issues/148762"}

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:9a00db6b-4475-0f6c-2f58-22105950a6d9
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id9B6A:34DB8F:33D0732:453FBE5:6B02095B
html-safe-nonce3c847ea873851963443c217f24807b623520868dc6400e5a539fab5501a98124
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QjZBOjM0REI4RjozM0QwNzMyOjQ1M0ZCRTU6NkIwMjA5NUIiLCJ2aXNpdG9yX2lkIjoiNjg2MjQ3MDU2ODA2MzY2NjUyMyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac8e01915f2bc0ddd3f41bb24c2bfde618053ca208bc1138c98be46a3859ddaf61
hovercard-subject-tagissue:4291607540
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/148762/issue_layout
twitter:imagehttps://opengraph.githubassets.com/4fdd807cf0b0803f40289a12036ef91724967030acfbc54a42ad9ffea959e3de/python/cpython/issues/148762
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/4fdd807cf0b0803f40289a12036ef91724967030acfbc54a42ad9ffea959e3de/python/cpython/issues/148762
og:image:altFeature or enhancement Proposal: I noticed that for regexes of the form regex = re.compile("^foo", re.MULTILINE) regex.search(...) there's a character by character loop calling SRE(match) every ite...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamehaampie
hostnamegithub.com
expected-hostnamegithub.com
None87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456
turbo-cache-controlno-preview
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release423dbb54c5e7bfecc838eb387c1435eb3d0e760d
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/cpython/issues/148762#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F148762
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F148762
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/148762
Reloadhttps://github.com/python/cpython/issues/148762
Reloadhttps://github.com/python/cpython/issues/148762
pythonhttps://github.com/python
cpythonhttps://github.com/python/cpython
Codehttps://github.com/python/cpython
Issues5k+ (5k+)https://github.com/python/cpython/issues
Pull requests2.4k (2.4k)https://github.com/python/cpython/pulls
Actionshttps://github.com/python/cpython/actions
Projectshttps://github.com/python/cpython/projects
Security and qualityhttps://github.com/python/cpython/security
Insightshttps://github.com/python/cpython/insights
https://github.com/python/cpython
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/148762
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/148762
Speed up multiline regexes anchored by ^.https://github.com/python/cpython/issues/148762#top
extension-modulesC modules in the Modules dirhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22extension-modules%22
performancePerformance or resource usagehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22performance%22
topic-regexhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-regex%22
type-featureA feature request or enhancementhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-feature%22
https://github.com/haampie
haampiehttps://github.com/haampie
on Apr 19, 2026https://github.com/python/cpython/issues/148762#issue-4291607540
cpython/Modules/_sre/sre_lib.hhttps://github.com/python/cpython/blob/42d645a7e81e0a5e6e0d35e222a8520450ac28ef/Modules/_sre/sre_lib.h#L1747
42d645ahttps://github.com/python/cpython/commit/42d645a7e81e0a5e6e0d35e222a8520450ac28ef
cpython/Modules/_sre/sre_lib.hhttps://github.com/python/cpython/blob/42d645a7e81e0a5e6e0d35e222a8520450ac28ef/Modules/_sre/sre_lib.h#L1775-L1776
42d645ahttps://github.com/python/cpython/commit/42d645a7e81e0a5e6e0d35e222a8520450ac28ef
gh-148762: speed up SRE_AT_BEGINNING_LINE regexes #148778https://github.com/python/cpython/pull/148778
gh-148762: Speed up multiline regexes anchored by ^ #152339https://github.com/python/cpython/pull/152339
haampiehttps://github.com/haampie
type-featureA feature request or enhancementhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-feature%22
on Apr 19, 2026https://github.com/python/cpython/issues/148762#event-24653992238
picnixzhttps://github.com/picnixz
extension-modulesC modules in the Modules dirhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22extension-modules%22
topic-regexhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-regex%22
performancePerformance or resource usagehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22performance%22
on Apr 19, 2026https://github.com/python/cpython/issues/148762#event-24654498546
https://github.com/haampie
haampiehttps://github.com/haampie
on Apr 19, 2026https://github.com/python/cpython/issues/148762#issuecomment-4276818265
haampiehttps://github.com/haampie
on Apr 19, 2026https://github.com/python/cpython/issues/148762#event-24655953095
haampiehttps://github.com/haampie
on Apr 19, 2026https://github.com/python/cpython/issues/148762#event-24656185648
pythongh-148762: speed up SRE_AT_BEGINNING_LINE regexeshttps://github.com/haampie/cpython/commit/a59b8637170a2bf769c0864072959609e487b032
pythongh-148762https://github.com/python/cpython/issues/148762
a59b863https://github.com/haampie/cpython/commit/a59b8637170a2bf769c0864072959609e487b032
pythongh-148762: speed up caret match in regexeshttps://github.com/haampie/cpython/commit/22f498f42964d4a7c17b82ce6d2351efa161b459
pythongh-148762https://github.com/python/cpython/issues/148762
22f498fhttps://github.com/haampie/cpython/commit/22f498f42964d4a7c17b82ce6d2351efa161b459
https://github.com/haampie
haampiehttps://github.com/haampie
on Apr 23, 2026https://github.com/python/cpython/issues/148762#issuecomment-4305983945
#148733https://github.com/python/cpython/pull/148733
haampiehttps://github.com/haampie
on Apr 26, 2026https://github.com/python/cpython/issues/148762#event-24884687600
pythongh-148762: speed up caret match in regexeshttps://github.com/haampie/cpython/commit/74328f180fe293b0e27ed5a8a6b9c6b598a2b678
pythongh-148762https://github.com/python/cpython/issues/148762
74328f1https://github.com/haampie/cpython/commit/74328f180fe293b0e27ed5a8a6b9c6b598a2b678
https://github.com/eendebakpt
eendebakpthttps://github.com/eendebakpt
on Jun 24, 2026https://github.com/python/cpython/issues/148762#issuecomment-4791142026
@haampiehttps://github.com/haampie
https://github.com/haampie
haampiehttps://github.com/haampie
on Jun 26, 2026https://github.com/python/cpython/issues/148762#issuecomment-4812715447
@eendebakpthttps://github.com/eendebakpt
https://github.com/eendebakpt
eendebakpthttps://github.com/eendebakpt
on Jun 26, 2026https://github.com/python/cpython/issues/148762#issuecomment-4812887488
haampiehttps://github.com/haampie
on Jun 26, 2026https://github.com/python/cpython/issues/148762#event-27263150451
pythongh-148762: speed up caret match in regexeshttps://github.com/haampie/cpython/commit/ab4bf9f13d578685df0b32ddd5eabd46a8315ad3
pythongh-148762https://github.com/python/cpython/issues/148762
ab4bf9fhttps://github.com/haampie/cpython/commit/ab4bf9f13d578685df0b32ddd5eabd46a8315ad3
https://github.com/haampie
haampiehttps://github.com/haampie
on Jun 26, 2026https://github.com/python/cpython/issues/148762#issuecomment-4813347554
#152339https://github.com/python/cpython/pull/152339
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/cpython/issues/148762
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/cpython/issues/148762
extension-modulesC modules in the Modules dirhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22extension-modules%22
performancePerformance or resource usagehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22performance%22
topic-regexhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-regex%22
type-featureA feature request or enhancementhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-feature%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.