René's URL Explorer Experiment


Title: Use `memchr` in SRE's prefix scanner · Issue #148729 · python/cpython · GitHub

Open Graph Title: Use `memchr` in SRE's prefix scanner · Issue #148729 · python/cpython

X Title: Use `memchr` in SRE's prefix scanner · Issue #148729 · python/cpython

Description: Feature or enhancement Proposal: Python's regex prefix scanning loops advance one byte at a time to find the first character of a literal prefix. For SIZEOF_SRE_CHAR == 1, memchr is a drop-in replacement that uses SIMD. The idea is to sk...

Open Graph Description: Feature or enhancement Proposal: Python's regex prefix scanning loops advance one byte at a time to find the first character of a literal prefix. For SIZEOF_SRE_CHAR == 1, memchr is a drop-in repla...

X Description: Feature or enhancement Proposal: Python's regex prefix scanning loops advance one byte at a time to find the first character of a literal prefix. For SIZEOF_SRE_CHAR == 1, memchr is a drop-in r...

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

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Use `memchr` in SRE's prefix scanner","articleBody":"# Feature or enhancement\n\n### Proposal:\n\nPython's regex prefix scanning loops advance one byte at a time to find the first character of a literal prefix. For `SIZEOF_SRE_CHAR == 1`, `memchr` is a drop-in replacement that uses SIMD. The idea is to skip say 32 bytes per iteration in the negative case (no match).\n\nThe two loops are at \n\nhttps://github.com/python/cpython/blob/7ce737ea11919aebf7eef174f910759e74d0ea50/Modules/_sre/sre_lib.h#L1756-L1759\n\nhttps://github.com/python/cpython/blob/7ce737ea11919aebf7eef174f910759e74d0ea50/Modules/_sre/sre_lib.h#L1789-L1792\n\nSimilar issues/prs are gh-145797 (`memchr` in `str.split`) and building on gh-57343, gh-57345.\n\nProposed patch:\n\n```diff\n--- a/Modules/_sre/sre_lib.h\n+++ b/Modules/_sre/sre_lib.h\n@@ -1753,10 +1753,19 @@\n         end = (SRE_CHAR *)state->end;\n         state->must_advance = 0;\n         while (ptr \u003c end) {\n+#if SIZEOF_SRE_CHAR == 1\n+            {\n+                SRE_CHAR *found = memchr(ptr, c, end - ptr);\n+                if (!found)\n+                    return 0;\n+                ptr = found;\n+            }\n+#else\n             while (*ptr != c) {\n                 if (++ptr >= end)\n                     return 0;\n             }\n+#endif\n             TRACE((\"|%p|%p|SEARCH LITERAL\\n\", pattern, ptr));\n             state->start = ptr;\n@@ -1786,10 +1795,19 @@\n         while (ptr \u003c end) {\n             SRE_CHAR c = (SRE_CHAR) prefix[0];\n+#if SIZEOF_SRE_CHAR == 1\n+            {\n+                SRE_CHAR *found = memchr(ptr, c, end - ptr);\n+                if (!found)\n+                    return 0;\n+                ptr = found + 1;\n+            }\n+#else\n             while (*ptr++ != c) {\n                 if (ptr >= end)\n                     return 0;\n             }\n+#endif\n             if (ptr >= end)\n                 return 0;\n```\n\nI ran into this while profiling a log parser (pretty much grepping for error and warning strings) that applies a handful of literal-prefix regexes to each line of a ~5MB log file. With the patch, the runtime is reduced by ~14% end-to-end. A similar use case in the same application is scanning a binary executable/library for strings with a pattern `/common/prefix/(foo|bar|baz)` where I would benefit even more from `memchr` to find the `/` anchors.\n\nMicrobenchmark on my M4 macbook (without PGO):\n\nSingle-char prefix `re.compile(r\"X(A|B)\")`, match at position `N` against `aaa...aXA`\n\n| match position | baseline | patched | speedup |\n|----------------|----------|---------|---------|\n| 0 | 55 ns | 58 ns | ~flat |\n| 50 | 70 ns | 53 ns | 1.3x |\n| 100 | 83 ns | 55 ns | 1.5x |\n| 500 | 217 ns | 62 ns | 3.5x |\n| 1,000 | 326 ns | 70 ns | 4.7x |\n| 10,000 | 2,379 ns | 219 ns | 10.9x |\n| 100,000 | 23,348 ns | 1,576 ns | 14.8x |\n\nMulti-char prefix `re.compile(r\"XXXX(A|B)\")`, match at position N against `aaaa...aXXXXA`\n\n| match position | baseline | patched | speedup |\n|----------------|----------|---------|---------|\n| 0 | 57 ns | 56 ns | ~flat |\n| 100 | 71 ns | 58 ns | 1.2x |\n| 500 | 142 ns | 65 ns | 2.2x |\n| 1,000 | 221 ns | 72 ns | 3.1x |\n| 10,000 | 1,652 ns | 225 ns | 7.3x |\n| 100,000 | 15,952 ns | 1,621 ns | 9.8x |\n\nThe reason the second benchmark is not as good as the first is that the apple clang compiler seems to have unrolled the second loop 4x while the first loop is not unrolled at all. Neither of them were auto-vectorized.\n\n\u003cdetails>\n\u003csummary>Synthetic benchmark\u003c/summary>\n\n```py\n\"\"\"Synthetic benchmark for SRE prefix scanning at various string lengths.\"\"\"\n\nimport re\nimport time\n\ndef bench(pat, text, iterations):\n    for _ in range(1000):\n        pat.search(text)\n    t0 = time.perf_counter()\n    for _ in range(iterations):\n        pat.search(text)\n    dt = time.perf_counter() - t0\n    ns_per_call = dt / iterations * 1e9\n    print(f\"  {ns_per_call:8.1f} ns/call\")\n\n# Single-char prefix: X(A|B) has prefix \"X\" (len=1)\npat1 = re.compile(r\"X(A|B)\")\nprint(\"Single-char prefix: X(A|B), match at position N\")\nfor n in [0, 5, 10, 50, 100, 500, 1000, 10000, 100000]:\n    text = \"a\" * n + \"XA\"\n    print(f\"  match at pos {n:>6d} (len={len(text):>6d})\", end=\"\")\n    bench(pat1, text, max(100000, 1000000 // max(n, 1)))\n\n# Multi-char prefix: XXXX(A|B) has prefix \"XXXX\" (len=4)\npat4 = re.compile(r\"XXXX(A|B)\")\nprint()\nprint(\"Multi-char prefix: XXXX(A|B), match at position N\")\nfor n in [0, 5, 10, 50, 100, 500, 1000, 10000, 100000]:\n    text = \"a\" * n + \"XXXXA\"\n    print(f\"  match at pos {n:>6d} (len={len(text):>6d})\", end=\"\")\n    bench(pat4, text, max(100000, 1000000 // max(n, 1)))\n```\n\n\u003c/details>\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-148733\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/haampie","@type":"Person","name":"haampie"},"datePublished":"2026-04-18T15:29:29Z","url":"https://github.com/python/cpython/issues/148729"}

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:1ebe806d-bfba-9935-848c-863269dbb12d
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idA468:1F2B:2DBF3:41BD6:6B020264
html-safe-nonce1821194459a39a1b49231e19880370a2186a1dbc1c9b75e213760270560b85c8
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNDY4OjFGMkI6MkRCRjM6NDFCRDY6NkIwMjAyNjQiLCJ2aXNpdG9yX2lkIjoiNjg0NTI3MzQxMDYxNDA3MzMyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmace67cf7b275fe41139d49e2804a3a83341ad917300bc370e467a9cee0da809c5d
hovercard-subject-tagissue:4288420105
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/148729/issue_layout
twitter:imagehttps://opengraph.githubassets.com/02368f776a40f731747b03c8b36edcbae16eeda6f416af8aafa6f578daddeb9a/python/cpython/issues/148729
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/02368f776a40f731747b03c8b36edcbae16eeda6f416af8aafa6f578daddeb9a/python/cpython/issues/148729
og:image:altFeature or enhancement Proposal: Python's regex prefix scanning loops advance one byte at a time to find the first character of a literal prefix. For SIZEOF_SRE_CHAR == 1, memchr is a drop-in repla...
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/148729#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F148729
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%2F148729
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/148729
Reloadhttps://github.com/python/cpython/issues/148729
Reloadhttps://github.com/python/cpython/issues/148729
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/148729
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/148729
Use memchr in SRE's prefix scannerhttps://github.com/python/cpython/issues/148729#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 18, 2026https://github.com/python/cpython/issues/148729#issue-4288420105
cpython/Modules/_sre/sre_lib.hhttps://github.com/python/cpython/blob/7ce737ea11919aebf7eef174f910759e74d0ea50/Modules/_sre/sre_lib.h#L1756-L1759
7ce737ehttps://github.com/python/cpython/commit/7ce737ea11919aebf7eef174f910759e74d0ea50
cpython/Modules/_sre/sre_lib.hhttps://github.com/python/cpython/blob/7ce737ea11919aebf7eef174f910759e74d0ea50/Modules/_sre/sre_lib.h#L1789-L1792
7ce737ehttps://github.com/python/cpython/commit/7ce737ea11919aebf7eef174f910759e74d0ea50
gh-145797https://github.com/python/cpython/issues/145797
gh-57343https://github.com/python/cpython/issues/57343
gh-57345https://github.com/python/cpython/issues/57345
gh-148729: use memchr in SRE prefix scan #148733https://github.com/python/cpython/pull/148733
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 18, 2026https://github.com/python/cpython/issues/148729#event-24639002038
https://github.com/haampie
haampiehttps://github.com/haampie
on Apr 18, 2026https://github.com/python/cpython/issues/148729#issuecomment-4274028947
haampiehttps://github.com/haampie
completedhttps://github.com/python/cpython/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Apr 18, 2026https://github.com/python/cpython/issues/148729#event-24639234305
haampiehttps://github.com/haampie
on Apr 18, 2026https://github.com/python/cpython/issues/148729#event-24639294188
eendebakpthttps://github.com/eendebakpt
performancePerformance or resource usagehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22performance%22
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
on Apr 18, 2026https://github.com/python/cpython/issues/148729#event-24642702305
haampiehttps://github.com/haampie
on Apr 18, 2026https://github.com/python/cpython/issues/148729#event-24643059763
pythongh-148729: use memchr in SRE prefix scanhttps://github.com/haampie/cpython/commit/e488932c42e7c1abf2053ab579871e8fe17cca72
pythongh-148729https://github.com/python/cpython/issues/148729
e488932https://github.com/haampie/cpython/commit/e488932c42e7c1abf2053ab579871e8fe17cca72
https://github.com/picnixz
picnixzhttps://github.com/picnixz
on Apr 20, 2026https://github.com/python/cpython/issues/148729#issuecomment-4282363061
https://github.com/haampie
haampiehttps://github.com/haampie
on Apr 20, 2026https://github.com/python/cpython/issues/148729#issuecomment-4282966010
cpython/Objects/stringlib/fastsearch.hhttps://github.com/python/cpython/blob/5a3f479601e52d694cc21415cb925037dffc1138/Objects/stringlib/fastsearch.h#L50
5a3f479https://github.com/python/cpython/commit/5a3f479601e52d694cc21415cb925037dffc1138
cpython/Objects/stringlib/fastsearch.hhttps://github.com/python/cpython/blob/5a3f479601e52d694cc21415cb925037dffc1138/Objects/stringlib/fastsearch.h#L773
5a3f479https://github.com/python/cpython/commit/5a3f479601e52d694cc21415cb925037dffc1138
cpython/Objects/stringlib/fastsearch.hhttps://github.com/python/cpython/blob/5a3f479601e52d694cc21415cb925037dffc1138/Objects/stringlib/fastsearch.h#L57-L62
5a3f479https://github.com/python/cpython/commit/5a3f479601e52d694cc21415cb925037dffc1138
cpython/Objects/stringlib/fastsearch.hhttps://github.com/python/cpython/blob/5a3f479601e52d694cc21415cb925037dffc1138/Objects/stringlib/fastsearch.h#L67-L70
5a3f479https://github.com/python/cpython/commit/5a3f479601e52d694cc21415cb925037dffc1138
https://github.com/haampie
haampiehttps://github.com/haampie
on Apr 21, 2026https://github.com/python/cpython/issues/148729#issuecomment-4290488251
https://github.com/haampie
haampiehttps://github.com/haampie
on Apr 22, 2026https://github.com/python/cpython/issues/148729#issuecomment-4296801933
haampie@6e1b1b9https://github.com/haampie/cpython/commit/6e1b1b9e285899f4422d07b1644a6f2c6df74a3e
https://portal.cfarm.net/machines/list/https://portal.cfarm.net/machines/list/
https://github.com/serhiy-storchaka
serhiy-storchakahttps://github.com/serhiy-storchaka
on Jul 6, 2026https://github.com/python/cpython/issues/148729#issuecomment-4889319092
https://github.com/haampie
haampiehttps://github.com/haampie
on Jul 6, 2026https://github.com/python/cpython/issues/148729#issuecomment-4895383195
https://github.com/maurycy
maurycyhttps://github.com/maurycy
on Oct 7, 2026https://github.com/python/cpython/issues/148729#issuecomment-6035339331
@haampiehttps://github.com/haampie
@serhiy-storchakahttps://github.com/serhiy-storchaka
kevinprimeraquecreo-stackhttps://github.com/kevinprimeraquecreo-stack
Project_VKhttps://github.com/users/kevinprimeraquecreo-stack/projects/1
on Oct 18, 2026https://github.com/python/cpython/issues/148729#event-33592115355
github-project-automationhttps://github.com/apps/github-project-automation
Project_VKhttps://github.com/users/kevinprimeraquecreo-stack/projects/1
on Oct 18, 2026https://github.com/python/cpython/issues/148729
kevinprimeraquecreo-stackhttps://github.com/kevinprimeraquecreo-stack
Project_VKhttps://github.com/users/kevinprimeraquecreo-stack/projects/1
on Oct 18, 2026https://github.com/python/cpython/issues/148729#event-33592171271
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/cpython/issues/148729
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/cpython/issues/148729
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.