René's URL Explorer Experiment
Title: gh-162513: Use `memcmp()` for equality comparisons between contiguous byte `memoryview`s by maurycy · Pull Request #162514 · python/cpython · GitHub
Open Graph Title: gh-162513: Use `memcmp()` for equality comparisons between contiguous byte `memoryview`s by maurycy · Pull Request #162514 · python/cpython
X Title: gh-162513: Use `memcmp()` for equality comparisons between contiguous byte `memoryview`s by maurycy · Pull Request #162514 · python/cpython
Description: Compare a run of bytes in one call to memcmp() in cmp_base(). The fast path applies when both operands have the same native format (B, b or c), both strides are one byte, both suboffset arrays are null, and the run is not empty.
This covers a B view compared with bytes or bytearray, and contiguous rows of multidimensional views.
Comparisons between different formats still use struct unpacking.
Closes #162513
macOS PGO+LTO:
Comparison
main
Patch
main / patch
4 KiB, equal
8.43 µs
95.0 ns
88.73×
4 KiB, last byte differs
8.45 µs
95.9 ns
88.13×
4 KiB, first byte differs
15.2 ns
14.9 ns
1.02×
8 bytes, equal
22.2 ns
15.0 ns
1.48×
Every other byte, 2,048 elements
4.2 µs
4.17 µs
1.01×
Equal 32 × 64 views, contiguous rows, outer stride 128
4.21 µs
164.4 ns
25.58×
Open, map and compare two copies of the pip wheel
5.81 ms
314 µs
18.54×
Expression
Size
Equal, main
Equal, patch
First byte differs, main
First byte differs, patch
mv == value
0 B
14.8 ns
15.6 ns
—
—
mv == value
1 B
16.6 ns
17.2 ns
16.2 ns
17.3 ns
mv == value
4 B
19.9 ns
16.7 ns
16.6 ns
17.0 ns
mv == value
16 B
35.8 ns
16.6 ns
16.8 ns
17.0 ns
mv == value
100 B
207.9 ns
18.0 ns
17.0 ns
16.7 ns
packet[:4] == magic
4 B
42.8 ns
37.6 ns
38.9 ns
38.7 ns
packet[:16] == magic
16 B
57.6 ns
38.9 ns
38.8 ns
38.6 ns
Comparison
main
Patch
main / patch
Unsigned byte view vs. bytearray, 100 bytes, equal
205.7 ns
18.5 ns
11.15×
Signed byte view (b) vs. bytes, 4 zero bytes, equal
744.9 ns
758.2 ns
0.98×
Character view (c) vs. bytes, 4 zero bytes, unequal
609.5 ns
615.6 ns
0.99×
bench
import ensurepip
import mmap
import tempfile
from pathlib import Path
import pyperf
def compare_files(path1, path2):
with path1.open("rb") as f1, path2.open("rb") as f2:
with (
mmap.mmap(f1.fileno(), 0, access=mmap.ACCESS_READ) as m1,
mmap.mmap(f2.fileno(), 0, access=mmap.ACCESS_READ) as m2,
memoryview(m1) as v1,
memoryview(m2) as v2,
):
return v1 == v2
left = memoryview(bytearray(4096))
right = memoryview(bytearray(4096))
first_diff = memoryview(b"x" + bytes(4095))
last_diff = memoryview(bytes(4095) + b"x")
short_left, short_right = left[:8], right[:8]
strided_left, strided_right = left[::2], right[::2]
runner = pyperf.Runner()
for stmt in (
"left == right",
"left == last_diff",
"left == first_diff",
"short_left == short_right",
"strided_left == strided_right",
):
runner.timeit(stmt, stmt, globals=globals())
packet = memoryview(bytes(128))
for size in (0, 1, 4, 16, 100):
view = packet[:size]
equal = bytes(size)
runner.timeit(f"mv == bytes/{size}/equal", "view == equal", globals=globals())
if size:
miss = b"x" + bytes(size - 1)
runner.timeit(f"mv == bytes/{size}/first differs", "view == miss", globals=globals())
if size in (4, 16):
for outcome, magic in (("equal", equal), ("first differs", miss)):
stmt = f"packet[:{size}] == magic"
runner.timeit(f"slice == bytes/{size}/{outcome}", stmt, globals=globals())
mutable = bytearray(100)
view = packet[:100]
signed = packet[:4].cast("b")
chars = packet[:4].cast("c")
raw = bytes(4)
rows_left = left.cast("B", (64, 64))[::2]
rows_right = right.cast("B", (64, 64))[::2]
for stmt in ("view == mutable", "signed == raw", "chars == raw", "rows_left == rows_right"):
runner.timeit(stmt, stmt, globals=globals())
wheel = next(Path(ensurepip.__file__).with_name("_bundled").glob("pip-*.whl"))
with tempfile.TemporaryDirectory() as tmp:
copy = Path(tmp) / wheel.name
copy.write_bytes(wheel.read_bytes())
assert compare_files(wheel, copy)
runner.bench_func("open, mmap and compare two pip wheels", compare_files, wheel, copy)
Issue: gh-162513
Open Graph Description: Compare a run of bytes in one call to memcmp() in cmp_base(). The fast path applies when both operands have the same native format (B, b or c), both strides are one byte, both suboffset arrays are ...
X Description: Compare a run of bytes in one call to memcmp() in cmp_base(). The fast path applies when both operands have the same native format (B, b or c), both strides are one byte, both suboffset arrays are ...
Opengraph URL: https://github.com/python/cpython/pull/162514
X: @github
direct link
Domain: github.com
IPv4 address: 140.82.114.3
| 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 | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:39ae1ab7-f9d7-346a-36e4-8fbbb9228261 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 9FDC:39ABD:DFAB54:12FBC74:6B0652DB |
| html-safe-nonce | 0001b9d1bf46d65f29126d22a6240da7a028952db8b079283515c75517d6c9af |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RkRDOjM5QUJEOkRGQUI1NDoxMkZCQzc0OjZCMDY1MkRCIiwidmlzaXRvcl9pZCI6IjYzMTYwMDEwOTEzNTIxNTQ4NDMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 546aba0d4b51985976ec01e2a9e697c15bf2850016bc98b3bcae3b64fd26660d |
| hovercard-subject-tag | pull_request:5462987376 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | ///pull_requests/show/files |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/python/cpython/pull/162514/files |
| twitter:image | https://avatars.githubusercontent.com/u/5383?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/5383?s=400&v=4 |
| og:image:alt | Compare a run of bytes in one call to memcmp() in cmp_base(). The fast path applies when both operands have the same native format (B, b or c), both strides are one byte, both suboffset arrays are ... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 772b6997ca4daa0428dd49a25d76052e765555ae560fe9c88517eb828bbd861e |
| turbo-cache-control | no-preview |
| diff-view | unified |
| 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 full-width |
| disable-turbo | true |
| 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 |
Links:
| Skip to content | https://github.com/python/cpython/pull/162514/files#start-of-content |
| https://github.com/ |
| Sign in | https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F162514%2Ffiles |
| GitHubRepository hosting, code review, and collaboration | https://github.com/platform |
| CopilotAI coding agent for tasks, projects, and automated work | https://github.com/features/copilot |
| ActionsCI/CD and automation | https://github.com/features/actions |
| Code QualityQuality analysis, fixes, and enforcement | https://github.com/features/code-quality |
| CodespacesCloud development environments | https://github.com/features/codespaces |
| Code SecurityVulnerability detection and remediation | https://github.com/security/advanced-security/code-security |
| Secret ProtectionSecret scanning and leak prevention | https://github.com/security/advanced-security/secret-protection |
| All Features | https://github.com/features |
| Documentation | https://docs.github.com/en |
| Training | https://learn.github.com/skills |
| Certifications | https://learn.github.com/credentials |
| Build with Copilot | https://github.com/features/ai |
| Customize Copilot | https://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview |
| Integrations | https://github.com/marketplace |
| Trending repos | https://github.com/trending |
| GitHub Sponsors | https://github.com/open-source/sponsors |
| Developer articles | https://github.com/resources/articles |
| ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place. | https://github.blog/changelog/ |
| Enterprises | https://github.com/enterprise |
| Team | https://github.com/team |
| Startups | https://github.com/enterprise/startups |
| Nonprofits | https://github.com/solutions/industry/nonprofits |
| Healthcare | https://github.com/solutions/industry/healthcare |
| Financial services | https://github.com/solutions/industry/financial-services |
| Manufacturing | https://github.com/solutions/industry/manufacturing |
| Government | https://github.com/solutions/industry/government |
| See all industries | https://github.com/solutions/industry |
| Why GitHub | https://github.com/why-github |
| Customer Stories | https://github.com/customer-stories |
| Business insights | https://github.com/solutions/executive-insights |
| Trust Center | https://github.com/trust-center |
| GitHub Advanced SecurityEnterprise-grade code security | https://github.com/security/advanced-security |
| Copilot for businessEnterprise-grade AI features | https://github.com/features/copilot/copilot-business |
| Premium SupportEnterprise-grade 24/7 support | https://github.com/enterprise/premium-support |
| Documentation | https://docs.github.com/en |
| Resource center | https://github.com/resources |
| Blog | https://github.blog/ |
| Events & webinars | https://github.com/resources/events |
| Community Forum | https://github.com/orgs/community/discussions |
| Customer Stories | https://github.com/customer-stories |
| Integrations | https://github.com/marketplace |
| Customer support | https://help.github.com/ |
| Training | https://learn.github.com/skills |
| GitHub Status | https://www.githubstatus.com/ |
| About | https://github.com/about |
| Partners | https://github.com/partners |
| Changelog | https://github.blog/changelog/ |
| GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub. | https://githubuniverse.com/ |
| Pricing | https://github.com/pricing |
| Sign in | https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F162514%2Ffiles |
| Sign up | https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=python%2Fcpython |
| Reload | https://github.com/python/cpython/pull/162514/files |
| Reload | https://github.com/python/cpython/pull/162514/files |
| Reload | https://github.com/python/cpython/pull/162514/files |
| python | https://github.com/python |
| cpython | https://github.com/python/cpython |
| Code | https://github.com/python/cpython |
| Issues5k+ (5k+) | https://github.com/python/cpython/issues |
| Pull requests2.4k (2.4k) | https://github.com/python/cpython/pulls |
| Actions | https://github.com/python/cpython/actions |
| Projects | https://github.com/python/cpython/projects |
| Security and quality | https://github.com/python/cpython/security |
| Insights | https://github.com/python/cpython/insights |
| Sign up for GitHub
| https://github.com/signup?return_to=%2Fpython%2Fcpython%2Fissues%2Fnew%2Fchoose |
| terms of service | https://docs.github.com/terms |
| privacy statement | https://docs.github.com/privacy |
| Sign in | https://github.com/login?return_to=%2Fpython%2Fcpython%2Fissues%2Fnew%2Fchoose |
| maurycy | https://github.com/maurycy |
| python:main | https://github.com/python/cpython/tree/main |
| maurycy:memoryview-byte-equality | https://github.com/maurycy/cpython/tree/memoryview-byte-equality |
|
Conversation
3
| https://github.com/python/cpython/pull/162514 |
|
Commits
6
| https://github.com/python/cpython/pull/162514/commits |
|
Checks
53
| https://github.com/python/cpython/pull/162514/checks |
|
Files changed
| https://github.com/python/cpython/pull/162514/files |
| Please reload this page | https://github.com/python/cpython/pull/162514/files |
|
gh-162513: Use memcmp() for equality comparisons between contiguous byte memoryviews
| https://github.com/python/cpython/pull/162514/files#top |
|
Show all changes
6 commits
| https://github.com/python/cpython/pull/162514/files |
|
b42914c
memcmp
maurycy Nov 23, 2026
| https://github.com/python/cpython/pull/162514/commits/b42914cdad71a067e2572df430b8435d9ee2d38f |
|
da0b5a9
test
maurycy Nov 23, 2026
| https://github.com/python/cpython/pull/162514/commits/da0b5a9306cfa46c7621c284ebc4f717e666e52e |
|
bb5e152
news
maurycy Nov 23, 2026
| https://github.com/python/cpython/pull/162514/commits/bb5e152fd272dc109aa5e24d129206c7845e3ce1 |
|
1ac2545
claim credit
maurycy Nov 23, 2026
| https://github.com/python/cpython/pull/162514/commits/1ac254522dfcca989f17dfce005c9bc95e603a6f |
|
5b2b544
pep 7
maurycy Nov 23, 2026
| https://github.com/python/cpython/pull/162514/commits/5b2b544beb4e0c90ef1423310a243e2f28181d8e |
|
affb52d
worth testing
maurycy Nov 23, 2026
| https://github.com/python/cpython/pull/162514/commits/affb52d0380a6c04898c1889070a803385b7836f |
|
Clear filters
| https://github.com/python/cpython/pull/162514/files |
| Please reload this page | https://github.com/python/cpython/pull/162514/files |
| Please reload this page | https://github.com/python/cpython/pull/162514/files |
|
test_memoryview.py
| https://github.com/python/cpython/pull/162514/files#diff-d41c6bb40a1e03fea5a20d15c4077413e0ddde65651147922b625b03a66a2f16 |
|
2026-11-24-00-25-22.gh-issue-162513.6-uUKS.rst
| https://github.com/python/cpython/pull/162514/files#diff-c43a79acd692ba1d8214b76b231f7b130f1e8e2c26be3e35fc1eec4cc158a652 |
|
memoryobject.c
| https://github.com/python/cpython/pull/162514/files#diff-7d1cb11da20dc3738b65b0f7381bf02df53132562a9749ba8a22d67a0f05f106 |
| Lib/test/test_memoryview.py | https://github.com/python/cpython/pull/162514/files#diff-d41c6bb40a1e03fea5a20d15c4077413e0ddde65651147922b625b03a66a2f16 |
|
View file
| https://github.com/python/cpython/blob/affb52d0380a6c04898c1889070a803385b7836f/Lib/test/test_memoryview.py |
|
Open in desktop
| https://desktop.github.com |
| https://github.co/hiddenchars |
| https://github.com/python/cpython/pull/162514/{{ revealButtonHref }} |
|
| https://github.com/python/cpython/pull/162514/files#diff-d41c6bb40a1e03fea5a20d15c4077413e0ddde65651147922b625b03a66a2f16 |
|
| https://github.com/python/cpython/pull/162514/files#diff-d41c6bb40a1e03fea5a20d15c4077413e0ddde65651147922b625b03a66a2f16 |
| Misc/NEWS.d/next/Core_and_Builtins/2026-11-24-00-25-22.gh-issue-162513.6-uUKS.rst | https://github.com/python/cpython/pull/162514/files#diff-c43a79acd692ba1d8214b76b231f7b130f1e8e2c26be3e35fc1eec4cc158a652 |
|
View file
| https://github.com/python/cpython/blob/affb52d0380a6c04898c1889070a803385b7836f/Misc/NEWS.d/next/Core_and_Builtins/2026-11-24-00-25-22.gh-issue-162513.6-uUKS.rst |
|
Open in desktop
| https://desktop.github.com |
| https://github.co/hiddenchars |
| https://github.com/python/cpython/pull/162514/{{ revealButtonHref }} |
| Objects/memoryobject.c | https://github.com/python/cpython/pull/162514/files#diff-7d1cb11da20dc3738b65b0f7381bf02df53132562a9749ba8a22d67a0f05f106 |
|
View file
| https://github.com/python/cpython/blob/affb52d0380a6c04898c1889070a803385b7836f/Objects/memoryobject.c |
|
Open in desktop
| https://desktop.github.com |
| https://github.co/hiddenchars |
| https://github.com/python/cpython/pull/162514/{{ revealButtonHref }} |
|
| https://github.com/python/cpython/pull/162514/files#diff-7d1cb11da20dc3738b65b0f7381bf02df53132562a9749ba8a22d67a0f05f106 |
| eendebakpt | https://github.com/eendebakpt |
| Nov 24, 2026 | https://github.com/python/cpython/pull/162514/files#r4673695974 |
| Learn more | https://docs.github.com/articles/managing-disruptive-comments/#hiding-a-comment |
| main...eendebakpt:memoryview-cmp-typed-loops | https://github.com/python/cpython/compare/main...eendebakpt:memoryview-cmp-typed-loops |
| Please reload this page | https://github.com/python/cpython/pull/162514/files |
|
| https://github.com/python/cpython/pull/162514/files#diff-7d1cb11da20dc3738b65b0f7381bf02df53132562a9749ba8a22d67a0f05f106 |
| Please reload this page | https://github.com/python/cpython/pull/162514/files |
|
| https://github.com |
| Terms | https://docs.github.com/site-policy/github-terms/github-terms-of-service |
| Privacy | https://docs.github.com/site-policy/privacy-policies/github-privacy-statement |
| Security | https://github.com/security |
| Status | https://www.githubstatus.com/ |
| Community | https://github.community/ |
| Docs | https://docs.github.com/ |
| Contact | https://support.github.com?tags=dotcom-footer |
Response headers:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | no-cache |
Viewport: width=device-width
URLs of crawlers that visited me.