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
Domain: github.com
{"@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-profiling | 0 |
| react-import-map | react,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-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:9a00db6b-4475-0f6c-2f58-22105950a6d9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9B6A:34DB8F:33D0732:453FBE5:6B02095B |
| html-safe-nonce | 3c847ea873851963443c217f24807b623520868dc6400e5a539fab5501a98124 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QjZBOjM0REI4RjozM0QwNzMyOjQ1M0ZCRTU6NkIwMjA5NUIiLCJ2aXNpdG9yX2lkIjoiNjg2MjQ3MDU2ODA2MzY2NjUyMyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 8e01915f2bc0ddd3f41bb24c2bfde618053ca208bc1138c98be46a3859ddaf61 |
| hovercard-subject-tag | issue:4291607540 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/148762/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4fdd807cf0b0803f40289a12036ef91724967030acfbc54a42ad9ffea959e3de/python/cpython/issues/148762 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4fdd807cf0b0803f40289a12036ef91724967030acfbc54a42ad9ffea959e3de/python/cpython/issues/148762 |
| og:image:alt | 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... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | haampie |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width