diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c622b1..e286f32 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -95,6 +95,33 @@ if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang") add_compile_options(-ffunction-sections -fdata-sections) add_link_options(-Wl,--gc-sections -Wl,-Map=mapfile.map) + # AVR is the tightest target in the footprint tables, and the one place + # where a further -Os knob still pays: -mcall-prologues replaces each + # function's register-save prologue and epilogue with a call to the shared + # __prologue_saves__ / __epilogue_restores__ pair in libgcc. Measured on + # atmega8, full configuration (tools/footprint.sh, and a linked image to net + # out the helpers): + # + # libsofabuffers.a 8572 B -> 7550 B (-1022 B) + # linked image 8980 B -> 8068 B ( -912 B) + # + # The 110 B difference is that helper pair, which the image pays once; a + # firmware built with the same flag amortises it and keeps the full -1022 B. + # The cost is cycles -- every call now enters and leaves through a shared + # routine -- which is the trade this library makes deliberately: flash is the + # scarce resource on these parts, speed is subordinate. + # + # Guarded on the processor rather than the compiler because the flag is + # AVR-only; CMAKE_SYSTEM_PROCESSOR is set by utils/avr/toolchain-avr.cmake. + # `naked` functions (utils/avr/startup.c) emit no prologue and are + # unaffected. Other architectures measured no gain from their equivalents: + # RISC-V -msave-restore wins 308 B in the archive but only 56 B in a linked + # image, because the __riscv_save_*/__riscv_restore_* set costs 1284 B there, + # and on ARMv6-m every comparable knob came out at ±0 B or worse. + if(CMAKE_SYSTEM_PROCESSOR STREQUAL "avr") + add_compile_options(-mcall-prologues) + endif() + if(SOFAB_ENABLE_LTO) # -O3 rather than the -Os above: this leg is not measuring footprint, it # is trying to give the optimiser every reason to act on a type-based diff --git a/README.md b/README.md index 20c1751..bdbaf2f 100644 --- a/README.md +++ b/README.md @@ -713,6 +713,18 @@ is `.text` (flash). The tables are `size libsofabuffers.a` at `-Os`, regenerated configuration; `atmega8` is the exception, its workflow being on manual dispatch until the test image links again. +The AVR rows carry one flag beyond `-Os`: `-mcall-prologues`, which routes every +prologue and epilogue through libgcc's shared save/restore pair. It is worth +1022 B of the `atmega8` *Full* row and is applied by the top-level +`CMakeLists.txt` for that processor only, so a hand-rolled AVR build without it +measures about 1 KB more. In a linked image it nets 912 B — the shared +pair costs 110 B once, which a firmware built with the same flag amortises. +It buys flash with cycles, which is the trade this library makes on these parts. +The equivalents elsewhere do not pay and are deliberately not set: RISC-V +`-msave-restore` saves 308 B in the archive but only 56 B in an image +(its helper set costs 1284 B there), and on ARMv6-m every comparable knob +measured ±0 B or worse. + **Full configuration** | Architecture | .text | .data | .bss | @@ -720,7 +732,7 @@ until the test image links again. | ARMv6-m | ~3.9KB | 0.0KB | 0.0KB | | ARMv7-m+fp.dp | ~3.9KB | 0.0KB | 0.0KB | | RV32IMC | ~5.0KB | 0.0KB | 0.0KB | -| atmega8 | ~8.4KB | 0.0KB | 0.0KB | +| atmega8 | ~7.4KB | 0.0KB | 0.0KB | **Full configuration, strict UTF-8 on** — the only rows where the validator (`utf8.c`) is compiled in. The delta over *Full* above is its entire @@ -731,10 +743,10 @@ until the test image links again. | ARMv6-m | ~4.1KB | 0.0KB | 0.0KB | | ARMv7-m+fp.dp | ~4.1KB | 0.0KB | 0.0KB | | RV32IMC | ~5.2KB | 0.0KB | 0.0KB | -| atmega8 | ~8.8KB | 0.0KB | 0.0KB | +| atmega8 | ~7.8KB | 0.0KB | 0.0KB | The [hold-back framing](#sequence-framing-and-the-hold-back-window) is part of -those *Full* rows: 276 B on ARMv6-m, 512 B on atmega8. A pure-C consumer +those *Full* rows: 276 B on ARMv6-m, 398 B on atmega8. A pure-C consumer encoding only through `sofab_object_encode()` takes it back out with `SOFAB_DISABLE_LAZY_SEQ_SUPPORT` — ARMv6-m returns to 3684 B, `sofab_ostream_t` shrinks from 56 B to 20 B per stream, and a typical encode drops @@ -750,7 +762,7 @@ unaffected. | ARMv6-m | ~1.1KB | 0.0KB | 0.0KB | | ARMv7-m+fp.dp | ~1.1KB | 0.0KB | 0.0KB | | RV32IMC | ~1.4KB | 0.0KB | 0.0KB | -| atmega8 | ~2.8KB | 0.0KB | 0.0KB | +| atmega8 | ~2.4KB | 0.0KB | 0.0KB | Same minimal configuration, additionally without `object.c` (`SOFAB_DISABLE_OBJECT_API`): @@ -760,7 +772,7 @@ Same minimal configuration, additionally without `object.c` | ARMv6-m | ~0.7KB | 0.0KB | 0.0KB | | ARMv7-m+fp.dp | ~0.7KB | 0.0KB | 0.0KB | | RV32IMC | ~0.9KB | 0.0KB | 0.0KB | -| atmega8 | ~1.9KB | 0.0KB | 0.0KB | +| atmega8 | ~1.6KB | 0.0KB | 0.0KB | #### What each switch is worth