Skip to content

gh-153563 feat: add unicode_memeq() to compare either directly or SSE2 vector#153564

Open
rajat315315 wants to merge 2 commits into
python:mainfrom
rajat315315:unicode_eq_opt
Open

gh-153563 feat: add unicode_memeq() to compare either directly or SSE2 vector#153564
rajat315315 wants to merge 2 commits into
python:mainfrom
rajat315315:unicode_eq_opt

Conversation

@rajat315315

@rajat315315 rajat315315 commented Jul 11, 2026

Copy link
Copy Markdown

Linked Issue

Fixes #153563

Proposed Changes

  • Modified Objects/stringlib/eq.h to implement a static inline memory comparison unicode_memeq that optimizes checks for string lengths up to 32 bytes.
  • Bypassed the standard memcmp function call inside unicode_eq for these short string sizes.
  • Wrapped SSE2 instructions in target architecture macros (__x86_64__, etc.) to guarantee portability on ARM, WebAssembly, and older x86 machines.

Verification & Testing

Rebuilt CPython on Linux x86_64 with GCC 15 and ran the standard regression test suite. All tests passed successfully:

./python -m test test_str test_dict test_capi
# == Tests result: SUCCESS ==
# All 3 tests OK.

Benchmarks (Mean Latency of String Equality Comparison)

Measured Python-level comparison times using different pointers (recreated string objects) with identical hashes to isolate the unicode_eq path:

String Length (Bytes) Baseline Latency Optimized Latency Speedup
2 74.425 ns 54.978 ns 1.35x (+35% faster)
4 51.202 ns 68.622 ns 0.75x
8 50.330 ns 40.765 ns 1.23x (+23% faster)
12 40.722 ns 34.995 ns 1.16x (+16% faster)

For keys of length 2, 8, and 12 characters (which represent the vast majority of Python identifiers), this optimization provides a 16% to 35% speedup at the Python runtime level, directly accelerating attribute lookups and dictionary throughput.

@bedevere-app

bedevere-app Bot commented Jul 11, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimize unicode_eq for short string comparisons by bypassing memcmp overhead

1 participant