-
Notifications
You must be signed in to change notification settings - Fork 356
[generator.c] optimize copy_remaining_bytes #924
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
Open
samyron
wants to merge
3
commits into
ruby:master
Choose a base branch
from
samyron:sm/optimize-copy-remaining-bytes2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+50
−4
Open
Changes from all commits
Commits
Show all changes
3 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
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 |
|---|---|---|
|
|
@@ -297,13 +297,43 @@ ALWAYS_INLINE(static) char *copy_remaining_bytes(search_state *search, unsigned | |
| char *s = (buf->ptr + buf->len); | ||
|
|
||
| // Pad the buffer with dummy characters that won't need escaping. | ||
| // This seem wateful at first sight, but memset of vector length is very fast. | ||
| memset(s, 'X', vec_len); | ||
| // This seem wasteful at first sight, but memset of vector length is very fast. | ||
| // This is a space as it can be directly represented as an immediate on AArch64. | ||
| memset(s, ' ', vec_len); | ||
|
|
||
| // Optimistically copy the remaining 'len' characters to the output FBuffer. If there are no characters | ||
| // to escape, then everything ends up in the correct spot. Otherwise it was convenient temporary storage. | ||
| MEMCPY(s, search->ptr, char, len); | ||
| #if defined(__has_builtin) && __has_builtin(__builtin_memcpy) | ||
|
|
||
| #ifdef RBIMPL_ASSERT_OR_ASSUME | ||
| RBIMPL_ASSERT_OR_ASSUME(len < 16); | ||
| #endif | ||
|
|
||
| if (vec_len == 16 && len >= 4) { | ||
| // If __builtin_memcpy is available, use it to copy between SIMD_MINIMUM_THRESHOLD and vec_len-1 bytes. | ||
| // These copies overlap. The first copy will copy the first 8 (or 4) bytes. The second copy will copy | ||
| // the last 8 (or 4) bytes but overlap with the first copy. The overlapping bytes will be in the correct | ||
| // position in both copies. | ||
|
|
||
| // Please do not attempt to replace __builtin_memcpy with memcpy without profiling and/or looking at the | ||
| // generated assembly. On clang-specifically (tested on Apple clang version 17.0.0 (clang-1700.0.13.3)), | ||
| // when using memcpy, the compiler will notice the only difference is a 4 or 8 and generate a conditional | ||
| // select instruction instead of direct loads and stores with a branch. This ends up slower than the branch | ||
| // plus two loads and stores generated when using __builtin_memcpy. | ||
| if (len >= 8) { | ||
| __builtin_memcpy(s, search->ptr, 8); | ||
| __builtin_memcpy(s + len - 8, search->ptr + len - 8, 8); | ||
| } else { | ||
| __builtin_memcpy(s, search->ptr, 4); | ||
| __builtin_memcpy(s + len - 4, search->ptr + len - 4, 4); | ||
| } | ||
|
Comment on lines
+323
to
+329
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. I think this makes sense, but I'd extract it to some sort of Could even be in |
||
| } else { | ||
| MEMCPY(s, search->ptr, char, len); | ||
| } | ||
| #else | ||
| MEMCPY(s, search->ptr, char, len); | ||
| #endif | ||
|
|
||
| return s; | ||
| } | ||
|
|
||
|
|
||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.