Repository navigation
[mypyc] Use memmove to shift items in vec pop and remove - #22464
Open
Jahongir-Qurbonov wants to merge 1 commit into
Open
Jahongir-Qurbonov wants to merge 1 commit into
Jahongir-Qurbonov wants to merge 1 commit into
Conversation
GCC already compiles the item shifting loops into a memmove call, but clang doesn't, so pop(0) on a large vec was ~35% slower in clang builds (such as macOS wheels). Calling memmove directly makes clang builds as fast as GCC builds.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
vecpopandremoveshift the remaining items with a loop likeitems[i] = items[i + 1]. GCC turns this loop into amemmovecall (as a tail call), but clang 21 doesn't and emits a slower element-by-element loop. This PR callsmemmovedirectly invec_template.c,vec_t.candvec_nested.c. The reference counting logic is unchanged.Benchmark:
pop(v, 0)repeated n/2 times on a copy of a vec with n items, librt built at-O3, CPython 3.13, Linux x86_64. Each number is the best of 15 runs, in ns per pop.pop(v)(no shift) takes ~75-90 ns in every build.vec[i64], n=16vec[i64], n=1000vec[i64], n=10000vec[str], n=1000vec[str], n=10000GCC builds don't change, and clang builds (such as macOS wheels) now match them.
Testing: the existing vec run tests cover
popwith an index andremovefor all vec kinds, including the shared buffer and decref ordering cases. They pass with both gcc andCC=clang.