Repository navigation
Optimize memset/memcpy size - #1333
Conversation
… 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.
|
|
Error: Failed to set assignee to
Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #triagebot on Zulip. |
|
r? libs |
|
(note: |
|
r? |
|
Error: Parsing assign command in comment failed: specify user to assign to (if you want to reroll without a specific reviewer in mind, use Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #triagebot on Zulip. |
|
@rustbot reroll (excuse me while i misunderstand the dev guide) |
|
Error: Cannot determine a new reviewer. Use Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #triagebot on Zulip. |
|
Ah okay, so that doesn’t work here. Welp. |
|
No need to request reviews on this repo really, usually it's just me. This is a really aggressive size optimization. |
|
Sometimes you really do care, yes. But this is indeed really aggressive. I wonder if it could be made in build-std that this is separately selectable instead of always on when core is built with optimize-for-size. |
|
On targets I am looking at, like the CH32V003 (16K Flash) and Pebble Original with STM32F205RE (24K combined memory), this is absolutely worth it. We’ve been looking at shaving off just a kilobyte here and there, and this makes a significant difference, especially since calls to these primitives are very common. |
|
It feels like perhaps we should have levels? I wonder why this is a feature at all #t-libs > optimize_for_size.
That shouldn't matter, they should be getting instantiated once. |
This hides the much slower versions of memcpy/memset. The regular flag is still kept in case it gets used in the future by other code.
2d5a3ce to
783713e
Compare
|
Added a more aggressive additional feature, in case that’s the direction we want to go. |
|
Thank you! |
Tracking issue: rust-lang/rust#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:
memset is reduced from 100 to 26 bytes at least in simple tests.
Actually using this requires optimize_for_size in alloc, like the original version of this PR. I’ll make a separate r-l/r PR once this is merged to do that.
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 intoif falsewith the flag, always using the bytewise copy/write branch.@diondokter
r? @clarfonthey