-
-
Notifications
You must be signed in to change notification settings - Fork 35k
gh-153563 feat: add unicode_memeq() to compare either directly or SSE2 vector #153564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+59
−1
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Core_and_Builtins/2026-07-11-17-15-55.gh-issue-153198.dmBbIm.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Optimize unicode_eq string equality comparisons for short strings by using | ||
| inline integer casts and SSE2 vector instructions. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,61 @@ | ||
| /* Fast unicode equal function optimized for dictobject.c and setobject.c */ | ||
|
|
||
| /* Return 1 if two unicode objects are equal, 0 if not. | ||
| * unicode_eq() is called when the hash of two unicode objects is equal. | ||
| */ | ||
| #if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86) | ||
| # include <emmintrin.h> | ||
| # define USE_SSE2 1 | ||
| #endif | ||
|
|
||
| static inline int | ||
| unicode_memeq(const void *p1, const void *p2, size_t len) { | ||
| if (len == 0) return 1; | ||
|
|
||
| const uint8_t *u1 = (const uint8_t *)p1; | ||
| const uint8_t *u2 = (const uint8_t *)p2; | ||
|
|
||
| switch (len) { | ||
| case 1: | ||
| return *u1 == *u2; | ||
| case 2: | ||
| return *(const uint16_t *)u1 == *(const uint16_t *)u2; | ||
| case 3: | ||
| return (*(const uint16_t *)u1 == *(const uint16_t *)u2) && | ||
| (*(const uint16_t *)(u1 + 1) == *(const uint16_t *)(u2 + 1)); | ||
| case 4: | ||
| return *(const uint32_t *)u1 == *(const uint32_t *)u2; | ||
| case 5: | ||
| case 6: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding |
||
| case 7: | ||
| return (*(const uint32_t *)u1 == *(const uint32_t *)u2) && | ||
| (*(const uint32_t *)(u1 + len - 4) == *(const uint32_t *)(u2 + len - 4)); | ||
| case 8: | ||
| return *(const uint64_t *)u1 == *(const uint64_t *)u2; | ||
| default: | ||
| if (len <= 16) { | ||
| return (*(const uint64_t *)u1 == *(const uint64_t *)u2) && | ||
| (*(const uint64_t *)(u1 + len - 8) == *(const uint64_t *)(u2 + len - 8)); | ||
| } | ||
| #ifdef USE_SSE2 | ||
| if (len <= 32) { | ||
| __m128i v1_a = _mm_loadu_si128((const __m128i *)u1); | ||
| __m128i v2_a = _mm_loadu_si128((const __m128i *)u2); | ||
| __m128i cmp_a = _mm_cmpeq_epi8(v1_a, v2_a); | ||
| int mask_a = _mm_movemask_epi8(cmp_a); | ||
|
|
||
| __m128i v1_b = _mm_loadu_si128((const __m128i *)(u1 + len - 16)); | ||
| __m128i v2_b = _mm_loadu_si128((const __m128i *)(u2 + len - 16)); | ||
| __m128i cmp_b = _mm_cmpeq_epi8(v1_b, v2_b); | ||
| int mask_b = _mm_movemask_epi8(cmp_b); | ||
|
|
||
| return (mask_a == 0xFFFF) && (mask_b == 0xFFFF); | ||
| } | ||
| #endif | ||
| return memcmp(p1, p2, len) == 0; | ||
| } | ||
| } | ||
|
|
||
| /* Return 1 if two unicode objects are equal, 0 if not. | ||
| * unicode_eq() is called when the hash of two unicode objects is equal. | ||
| */ | ||
|
|
@@ -18,5 +74,5 @@ unicode_eq(PyObject *str1, PyObject *str2) | |
|
|
||
| const void *data1 = PyUnicode_DATA(str1); | ||
| const void *data2 = PyUnicode_DATA(str2); | ||
| return (memcmp(data1, data2, len * kind) == 0); | ||
| return unicode_memeq(data1, data2, len * kind); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can instead check the support in
configure?