[Repo Assist] perf: word-at-a-time backward copy in l_memmove#122
Draft
github-actions[bot] wants to merge 1 commit intomainfrom
Draft
[Repo Assist] perf: word-at-a-time backward copy in l_memmove#122github-actions[bot] wants to merge 1 commit intomainfrom
github-actions[bot] wants to merge 1 commit intomainfrom
Conversation
The backward branch (dst > src, overlapping regions) used a plain byte-at-a-time loop. This commit applies the same word-at-a-time technique already used in the forward branch: 1. Byte-copy trailing bytes until d is word-aligned. 2. If s is also word-aligned, copy sizeof(uintptr_t) bytes per iteration backward using may_alias word pointers. 3. Byte-copy the remaining head bytes. The read-before-write ordering is preserved: in the backward pass d > s throughout, so each word write lands ahead of (higher address than) the corresponding word read, making the copy safe for all overlapping configurations. Tests added to test_memmove(): - large backward overlap (64 bytes, dst = src+1) — exercises the new word-at-a-time path - large forward overlap (64 bytes, dst = src-1) — confirms the existing forward path also handles large moves correctly - large non-overlapping copy (64 bytes) — baseline sanity check CI: Linux gcc + clang, 1585/1583 assertions PASS Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
38 tasks
26 tasks
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.
🤖 This PR was created by Repo Assist, an automated AI assistant.
Summary
The backward-copy branch of
l_memmove(triggered whendst > srcand the ranges overlap) previously used a plain byte-at-a-time loop. This PR applies the same word-at-a-time technique already present in the forward branch.Root cause
The forward path was already optimised (PR #107 era), but the backward path was left as:
For large overlapping copies (e.g.
memmove(p+1, p, 64KB)) this is 8× slower than necessary on 64-bit targets.Fix
Three-phase backward copy (mirrors the forward path):
dis word-aligned.sis also word-aligned, copysizeof(uintptr_t)bytes per step backward usingmay_aliasword pointers.Read-before-write safety is preserved: in the backward pass
d > sthroughout, so each word write always lands at a higher address than the corresponding word read. No source data is overwritten before it is consumed.Tests added (
test_memmove)dst = src+1)dst = src-1)Test Status