Title: Speed up integer searching in memoryview and array.array · Issue #159716 · python/cpython · GitHub
Open Graph Title: Speed up integer searching in memoryview and array.array · Issue #159716 · python/cpython
X Title: Speed up integer searching in memoryview and array.array · Issue #159716 · python/cpython
Description: Feature or enhancement Currently for memoryview and array.array operations like .index() and .count() convert elements from native byte format to a PyObject then call PyObject_RichCompareBool to compare them. This is significantly slower...
Open Graph Description: Feature or enhancement Currently for memoryview and array.array operations like .index() and .count() convert elements from native byte format to a PyObject then call PyObject_RichCompareBool to co...
X Description: Feature or enhancement Currently for memoryview and array.array operations like .index() and .count() convert elements from native byte format to a PyObject then call PyObject_RichCompareBool to co...
Opengraph URL: https://github.com/python/cpython/issues/159716
X: @github
Domain: github.com
IPv4 address: 140.82.112.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Speed up integer searching in memoryview and array.array","articleBody":"### Feature or enhancement\n\nCurrently for `memoryview` and `array.array` operations like `.index()` and `.count()` convert elements from native byte format to a `PyObject` then call `PyObject_RichCompareBool` to compare them. This is significantly slower than the equivalent `bytes` as reported on discord.\n\n```python\ndata = memoryview(bytearray(64 * 1024))\ndata[-1] = 10\ndata.index(10, 100) # ~860x slower than bytes(data).index(10, 100)\n```\n\nFor one dimensional `memoryview` and `array.array` when the items are native integer formats (struct formats `bBhHiIlLqQnN`) we can do the comparison as event bytes using optimized methods for 1-, 2-, 4- and 8-byte comparison.\n\nNote this does not apply for objects like float where exact-byte comparison does not work (`0.0 == -0.0`).\n\nDetect the cases we can make faster and generalize existing helpers for \"packing\" into native bytes and comparing buffer protocol objects with 1-, 2-, 4-, and 8-byte types which are exact-byte comparable.\n\n### Has this already been discussed elsewhere?\n\nI have already discussed this feature proposal on Discourse\n\n### Links to previous discussion of this feature:\n\n- https://discuss.python.org/t/memoryview-index-860x-slower-than-bytes-index/104973\n- gh-125420: addition of `memoryview.index()` and `count()`\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-160541\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/cmaloney","@type":"Person","name":"cmaloney"},"datePublished":"2026-10-19T16:58:18Z","url":"https://github.com/python/cpython/issues/159716"}
| 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:b8266716-6bad-6c16-1664-f78b5c161c84 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9B10:B0F20:CE1DD6:117EE72:6B064BFF |
| html-safe-nonce | d3ff02b06b6981b09152b41f697ae11713977bf186019ff6906da490e67aedeb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QjEwOkIwRjIwOkNFMURENjoxMTdFRTcyOjZCMDY0QkZGIiwidmlzaXRvcl9pZCI6Ijg2ODQ2NDg0MzEzNTc5ODI3MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 9e928b3ed9d679a78ac5cc6be96c2af135805bcaa86110ec025fd144e67f22ae |
| hovercard-subject-tag | issue:5928014377 |
| 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/159716/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6d57e98af61dadcbd14b30fc6ed4d38ff4d9f7de1878ff056fdaf9402f32163f/python/cpython/issues/159716 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6d57e98af61dadcbd14b30fc6ed4d38ff4d9f7de1878ff056fdaf9402f32163f/python/cpython/issues/159716 |
| og:image:alt | Feature or enhancement Currently for memoryview and array.array operations like .index() and .count() convert elements from native byte format to a PyObject then call PyObject_RichCompareBool to co... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | cmaloney |
| hostname | github.com |
| expected-hostname | github.com |
| None | 772b6997ca4daa0428dd49a25d76052e765555ae560fe9c88517eb828bbd861e |
| 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 | f1f9a08f7c769a37f1dfc18ad43d463c222a7eea |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width