Title: Improve performances of `fnmatch.translate` · Issue #122288 · python/cpython · GitHub
Open Graph Title: Improve performances of `fnmatch.translate` · Issue #122288 · python/cpython
X Title: Improve performances of `fnmatch.translate` · Issue #122288 · python/cpython
Description: Feature or enhancement Proposal: I implemented fnmatch.translate and fnmatch.filter in C in #121446 but the performances for fnmatch.filter are less pronounced than those in fnmatch.translate. While I believe that the factor 2x improveme...
Open Graph Description: Feature or enhancement Proposal: I implemented fnmatch.translate and fnmatch.filter in C in #121446 but the performances for fnmatch.filter are less pronounced than those in fnmatch.translate. Whil...
X Description: Feature or enhancement Proposal: I implemented fnmatch.translate and fnmatch.filter in C in #121446 but the performances for fnmatch.filter are less pronounced than those in fnmatch.translate. Whil...
Opengraph URL: https://github.com/python/cpython/issues/122288
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Improve performances of `fnmatch.translate`","articleBody":"# Feature or enhancement\r\n\r\n### Proposal:\r\n\r\nI implemented `fnmatch.translate` and `fnmatch.filter` in C in #121446 but the performances for `fnmatch.filter` are less pronounced than those in `fnmatch.translate`. While I believe that the factor 2x improvement brought by the C implementation is worthwhile for `fnmatch.translate`, this comes at a high maintenance cost. Instead, I translated (no pun intended) the implementation in Python and obtained the following timings (PGO build):\r\n\r\n```\r\n+------------------------------------------+-----------------------+-----------------------+\r\n| Benchmark | fnmatch-translate-ref | fnmatch-translate-py |\r\n+==========================================+=======================+=======================+\r\n| abc/[!]b-ac-z9-1]/def/\\*?/*/**c/?*[][!] | 6.09 us | 3.99 us: 1.53x faster |\r\n+------------------------------------------+-----------------------+-----------------------+\r\n| !abc/[!]b-ac-z9-1]/def/\\*?/*/**c/?*[][!] | 6.39 us | 4.07 us: 1.57x faster |\r\n+------------------------------------------+-----------------------+-----------------------+\r\n| a**?**cd**?**??k*** | 2.24 us | 1.51 us: 1.49x faster |\r\n+------------------------------------------+-----------------------+-----------------------+\r\n| a/**/b/**/c | 1.97 us | 1.12 us: 1.76x faster |\r\n+------------------------------------------+-----------------------+-----------------------+\r\n| man/man1/bash.1 | 3.00 us | 1.21 us: 2.48x faster |\r\n+------------------------------------------+-----------------------+-----------------------+\r\n| a*b*c*d*e*f*g*h*i*j*k*l*m*n*o** | 5.40 us | 3.33 us: 1.62x faster |\r\n+------------------------------------------+-----------------------+-----------------------+\r\n| Geometric mean | (ref) | 1.71x faster |\r\n+------------------------------------------+-----------------------+-----------------------+\r\n``` \r\n\r\nIt's not as good as the benchmarks reported in https://github.com/python/cpython/issues/121445, but I think this could be considered. Note that the improvements are brought by a new (but equivalent) algorithm during the translation phase. Currently, the translation algorithm does:\r\n\r\n- Do not compress consecutive '*' into 1 within the same loop iteration.\r\n- Use a sentinel to indicate a wildcard instead.\r\n- Add to a list `re.escape(c)` for every non-special character (i.e, not for groups beginning by `?*[`).\r\n- In a second phase, merge the groups delimited by the sentinels in order to produce the final regex.\r\n\r\nOur (Barney and me) implementation instead does the following:\r\n\r\n- Cache `re.escape` so that the lookup is faster.\r\n- Remember the positions of '*' in the above split instead of using a sentinel object.\r\n That way, I can easily merge parts during the second phase (technically, it'd be possible to do it in one phase but two passes makes it clearer).\r\n\r\n### Has this already been discussed elsewhere?\r\n\r\nNo response given\r\n\r\n### Links to previous discussion of this feature:\r\n\r\n_No response_\r\n\r\n\u003c!-- gh-linked-prs -->\r\n### Linked PRs\r\n* gh-122289\r\n\u003c!-- /gh-linked-prs -->\r\n","author":{"url":"https://github.com/picnixz","@type":"Person","name":"picnixz"},"datePublished":"2024-07-25T16:05:21Z","url":"https://github.com/python/cpython/issues/122288"}
| 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:b7c7afc6-e918-fd08-5fb3-c8bc02f8b3b6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8474:136CDA:179688D:1F384F3:6B020AE1 |
| html-safe-nonce | f8ccedeea4ac9f97807a228ffed310307a9dde889357d9a56aecba40f793bc7e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NDc0OjEzNkNEQToxNzk2ODhEOjFGMzg0RjM6NkIwMjBBRTEiLCJ2aXNpdG9yX2lkIjoiMTU1NzE1OTE1OTY3MjIxMjE5MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | baab11038ee5ead7d49b63e1246b50f10bb463d5db4c4ab6bbcc703a5fa94d2c |
| hovercard-subject-tag | issue:2430427162 |
| 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/122288/issue_layout |
| twitter:image | https://opengraph.githubassets.com/198561ec12e0206b3a84be26e13dd52d6eaf7c3df9bac8ef43a91efe6aecca20/python/cpython/issues/122288 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/198561ec12e0206b3a84be26e13dd52d6eaf7c3df9bac8ef43a91efe6aecca20/python/cpython/issues/122288 |
| og:image:alt | Feature or enhancement Proposal: I implemented fnmatch.translate and fnmatch.filter in C in #121446 but the performances for fnmatch.filter are less pronounced than those in fnmatch.translate. Whil... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | picnixz |
| 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 | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width