Skip to content

compiler-builtins: Add size-optimized memcpy/memmove/memset - #163944

Closed
kleinesfilmroellchen wants to merge 2 commits into
rust-lang:mainfrom
kleinesfilmroellchen:optimize-memset-size
Closed

kleinesfilmroellchen wants to merge 2 commits into
rust-lang:mainfrom
kleinesfilmroellchen:optimize-memset-size

Conversation

@kleinesfilmroellchen

@kleinesfilmroellchen kleinesfilmroellchen commented Oct 7, 2026 •

Copy link
Copy Markdown

Tracking issue: #125612

On thumbv8m.main-none-eabi with -Os, this reduces memcpy from 324 bytes to 30 bytes. The assembly is so short and cute UwU, I can include it here:

0000152a <__aeabi_memcpy>:
    152a:           1883        adds    r3, r0, r2
    152c:           4298        cmp     r0, r3
    152e:           bf28        it      cs
    1530:           4770        bxcs    lr
    1532:           b580        push    {r7, lr}
    1534:           466f        mov     r7, sp
    1536:       ,-> f811 3b01   ldrb.w  r3, [r1], #1
    153a:       |   3a01        subs    r2, #1
    153c:       |   f800 3b01   strb.w  r3, [r0], #1
    1540:       '-- d1f9        bne.n   1536 <__aeabi_memcpy+0xc>
    1542:           e8bd 4080   ldmia.w sp!, {r7, lr}
    1546:           4770        bx      lr

memset is reduced from 100 to 26 bytes at least in simple tests.

Introducing this change requires introducing the optimize_for_size feature to compiler-builtins, and passing it from alloc (and possibly other places I have missed). This is why I have made the PR here and not in the compiler-builtins repo.

I’ve done some testing on multiple ARM embedded targets, but overall this code should be trivially correct. The previous if <size-dependent condition> are simply turned into if false with the flag, always using the bytewise copy/write branch.

r? @diondokter
(as per our discussion about this on wg-embedded zulip)

… core+alloc

This (probably?) requires rebuilding compiler_intrinsics via build_std.
Note that this code is trivially correct, as it simply turns maybe-ifs into never-ifs.

On thumbv8m.main-none-eabi with -Os, this reduces memcpy from 324 bytes to 30 bytes. The assembly is so short, I can include it in the commit message:

```arm-assembly
0000152a <__aeabi_memcpy>:
    152a:           1883        adds    r3, r0, r2
    152c:           4298        cmp     r0, r3
    152e:           bf28        it      cs
    1530:           4770        bxcs    lr
    1532:           b580        push    {r7, lr}
    1534:           466f        mov     r7, sp
    1536:       ,-> f811 3b01   ldrb.w  r3, [r1], rust-lang#1
    153a:       |   3a01        subs    r2, rust-lang#1
    153c:       |   f800 3b01   strb.w  r3, [r0], rust-lang#1
    1540:       '-- d1f9        bne.n   1536 <__aeabi_memcpy+0xc>
    1542:           e8bd 4080   ldmia.w sp!, {r7, lr}
    1546:           4770        bx      lr
```

memset is reduced from 100 to 26 bytes at least in simple tests.
@rustbot

rustbot commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

compiler-builtins is developed in its own repository. If possible, consider making this change to rust-lang/compiler-builtins instead.

cc @tgross35

@rustbot rustbot added A-compiler-builtins Area: compiler-builtins (https://github.com/rust-lang/compiler-builtins) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 7, 2026
@rustbot

rustbot commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

⚠️ Warning ⚠️

  • There are issue links (such as #123) in the commit messages of the following commits.
    Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtrees.

@rustbot

rustbot commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

Error: Failed to set assignee to diondokter: invalid assignee

Note: Only org members with at least the repository "read" role, users with write permissions, or people who have commented on the PR may be assigned.

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #triagebot on Zulip.

@rustbot

rustbot commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

Error: Failed to set assignee to reroll: invalid assignee

Note: Only org members with at least the repository "read" role, users with write permissions, or people who have commented on the PR may be assigned.

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #triagebot on Zulip.

@kleinesfilmroellchen

Copy link
Copy Markdown
Author

r? libs

@clarfonthey

Copy link
Copy Markdown
Contributor

Considering how this can be done via a separate PR to compiler-builtins, I would prefer that over this. We can still enable the feature on this repo without making changes to subtrees that will have to be reconciled later.

@kleinesfilmroellchen

Copy link
Copy Markdown
Author

Considering how this can be done via a separate PR to compiler-builtins, I would prefer that over this. We can still enable the feature on this repo without making changes to subtrees that will have to be reconciled later.

So that will require a separate PR here to pass the flag through alloc, right?

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 7, 2026
@kleinesfilmroellchen

Copy link
Copy Markdown
Author

@clarfonthey

Copy link
Copy Markdown
Contributor

So that will require a separate PR here to pass the flag through alloc, right?

Correct; I believe that this should be fine even if the subtree isn't merged yet, since cargo features just do nothing if they're not defined for the other crate.

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

Labels

A-compiler-builtins Area: compiler-builtins (https://github.com/rust-lang/compiler-builtins) T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants