Title: `re`: no ASCII fast path for `\w` in Unicode mode, unlike `\s` · Issue #159251 · python/cpython · GitHub
Open Graph Title: `re`: no ASCII fast path for `\w` in Unicode mode, unlike `\s` · Issue #159251 · python/cpython
X Title: `re`: no ASCII fast path for `\w` in Unicode mode, unlike `\s` · Issue #159251 · python/cpython
Description: Feature or enhancement Use the existing ASCII table to speed up word matching for ASCII characters, while keeping Unicode by default. For example: >>> import re >>> re.findall(r"\w+", "hello café 123") ['hello', 'café', '123'] Most chara...
Open Graph Description: Feature or enhancement Use the existing ASCII table to speed up word matching for ASCII characters, while keeping Unicode by default. For example: >>> import re >>> re.findall(r"\w+", "hello café 1...
X Description: Feature or enhancement Use the existing ASCII table to speed up word matching for ASCII characters, while keeping Unicode by default. For example: >>> import re >>> re.findall(r&q...
Opengraph URL: https://github.com/python/cpython/issues/159251
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`re`: no ASCII fast path for `\\w` in Unicode mode, unlike `\\s`","articleBody":"### Feature or enhancement\n\nUse the existing ASCII table to speed up word matching for ASCII characters, while keeping Unicode by default.\n\nFor example:\n\n```python\n>>> import re\n>>> re.findall(r\"\\w+\", \"hello café 123\")\n['hello', 'café', '123']\n```\n\nMost characters in this example are ASCII, but the `re` still uses Unicode classification for each character that `\\w` examines.\n\n## Idea\n\nThe idea is for chars less than 128, use `SRE_IS_WORD()`. For the rest, keep the existing Unicode predicate:\n\n```c\n#define SRE_UNI_IS_WORD(ch)\\\n ((ch) \u003c 128 ? SRE_IS_WORD(ch) : (SRE_UNI_IS_ALNUM(ch) || (ch) == '_'))\n```\n\nThe same predicate is used by `\\W`, `\\b` and `\\B` so it'd speed them up, too.\n\nWe're doing it for `\\s`:\n\nhttps://github.com/python/cpython/blob/9028df36bb9e2942b19164f6708f9cf8a55c5ce9/Include/cpython/unicodeobject.h#L753-L758\n\n## Microbenchmark\n\n| Benchmark | main | patched |\n|---|---:|---:|\n| `\\w+` findall, ASCII prose | 295 us | 235 us (1.25x faster) |\n| `\\w+\\|[^\\w\\s]+` (NLTK WordPunct), ASCII prose | 417 us | 327 us (1.28x faster) |\n| `\\W+` findall, ASCII prose | 321 us | 276 us (1.16x faster) |\n| `\\b\\w` findall, ASCII prose | 347 us | 223 us (1.55x faster) |\n| `\\w+` findall, ASCII punctuation only | 132 us | 81.8 us (1.61x faster) |\n| `\\w+` findall, Latin-1 prose | 326 us | 270 us (1.21x faster) |\n| `\\w+` findall, CJK text | 71.8 us | 77.4 us (1.08x slower) |\n| `\\w+` findall, mixed scripts | 206 us | 172 us (1.20x faster) |\n| `\\w+` findall with `re.ASCII` (control) | 229 us | not significant |\n| `\\d+` findall, ASCII prose (control) | 154 us | 165 us (1.07x slower) |\n\nLinux (AMD EPYC 4484PX, Ubuntu 24.04, gcc 13), PGO+LTO builds of `main` at 9028df36bb9e\n\n`perf stat -r 5` on the same machine:\n\n| Case | main instructions | patched instructions | main cycles | patched cycles |\n|---|---:|---:|---:|---:|\n| `\\w+` ASCII prose | 14.85G | 12.14G (-18%) | 3.20G | 2.61G (-18%) |\n| NLTK WordPunct, ASCII prose | 15.64G | 12.84G (-18%) | 3.32G | 2.68G (-19%) |\n| `\\w+` CJK text | 13.63G | 13.50G (-1%) | 2.97G | 3.27G (+10%) |\n| `\\w+` with `re.ASCII` (control) | 12.08G | 12.09G (0%) | 2.57G | 2.55G (-1%) |\n| `\\d+` ASCII prose (control) | 14.85G | 14.85G (0%) | 3.03G | 3.09G (+2%) |\n\nSo the `\\d+` is likely just noise between PGO+LTO builds, but 100% CJK seems to be real cost. Are we ok with it? \n\n\u003cdetails>\n\u003csummary>pyperf microbenchmark\u003c/summary>\n\n```python\nimport re\n\nimport pyperf\n\nascii_text = (\"The quick brown fox jumps over the lazy dog, said Alice; \"\n \"it was 42 degrees outside and the kettle_was on. \") * 200\nlatin_text = (\"Zażółć gęślą jaźń, señor; la niña está aquí: café, \"\n \"naïve, crème brûlée. \") * 300\ncjk_text = \"漢字仮名交じり文で書かれた文章です。これはテストの文章です。\" * 300\nmixed_text = (ascii_text[:2000] + latin_text[:1000] + cjk_text[:500]) * 4\npunct_text = \"!!! ??? ... --- *** ((( ))) ;;; ::: ,,, \" * 300\n\nword_run = re.compile(r\"\\w+\")\nnltk_wordpunct = re.compile(r\"\\w+|[^\\w\\s]+\")\nnonword_split = re.compile(r\"\\W+\")\nboundary = re.compile(r\"\\b\\w\")\nascii_flag = re.compile(r\"\\w+\", re.ASCII)\ndigit_run = re.compile(r\"\\d+\")\n\n\ndef findall(pattern, text):\n return pattern.findall(text)\n\n\nrunner = pyperf.Runner()\nrunner.bench_func(r\"\\w+ findall, ASCII prose\", findall, word_run, ascii_text)\nrunner.bench_func(r\"\\w+|[^\\w\\s]+ (NLTK WordPunct), ASCII prose\", findall, nltk_wordpunct, ascii_text)\nrunner.bench_func(r\"\\W+ findall, ASCII prose\", findall, nonword_split, ascii_text)\nrunner.bench_func(r\"\\b\\w findall, ASCII prose\", findall, boundary, ascii_text)\nrunner.bench_func(r\"\\w+ findall, ASCII punctuation only\", findall, word_run, punct_text)\nrunner.bench_func(r\"\\w+ findall, Latin-1 prose\", findall, word_run, latin_text)\nrunner.bench_func(r\"\\w+ findall, CJK text\", findall, word_run, cjk_text)\nrunner.bench_func(r\"\\w+ findall, mixed scripts\", findall, word_run, mixed_text)\nrunner.bench_func(r\"\\w+ findall with re.ASCII (control)\", findall, ascii_flag, ascii_text)\nrunner.bench_func(r\"\\d+ findall, ASCII prose (control)\", findall, digit_run, ascii_text)\n```\n\n\u003c/details>\n\n### Has this already been discussed elsewhere?\n\nNo response given\n\n### Links to previous discussion of this feature:\n\n_No response_\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-159252\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/maurycy","@type":"Person","name":"maurycy"},"datePublished":"2026-10-12T19:19:54Z","url":"https://github.com/python/cpython/issues/159251"}
| 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:84078a7b-bd51-e15e-c4db-2abc0b80d062 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8E36:26ECEB:3D8C64D:4E37239:6B01FA1E |
| html-safe-nonce | 4b448028e0cc830c9c7d70e77249269679ed6d45b499045510287115c9db0e34 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RTM2OjI2RUNFQjozRDhDNjREOjRFMzcyMzk6NkIwMUZBMUUiLCJ2aXNpdG9yX2lkIjoiNjkwNjQ0OTAzMTcxOTk0MjY4NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e521238872f52042eccfa065ee798a139f3e4c7a55ec9182ce211d5c6560e9d8 |
| hovercard-subject-tag | issue:5819205320 |
| 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/159251/issue_layout |
| twitter:image | https://opengraph.githubassets.com/cc35bbd9a58bc3d10851c6c9261084646565db994d71601c08ba4ba09b68929b/python/cpython/issues/159251 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/cc35bbd9a58bc3d10851c6c9261084646565db994d71601c08ba4ba09b68929b/python/cpython/issues/159251 |
| og:image:alt | Feature or enhancement Use the existing ASCII table to speed up word matching for ASCII characters, while keeping Unicode by default. For example: >>> import re >>> re.findall(r"\w+", "hello café 1... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | maurycy |
| 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