Skip to content

mm, sched, libc: save 576 bytes of code on DEFAULT_SMALL builds - #20628

Merged
xiaoxiang781216 merged 6 commits into
apache:masterfrom
jingfei195887:size-reduction-tier-a
Oct 15, 2026
Merged

xiaoxiang781216 merged 6 commits into
apache:masterfrom
jingfei195887:size-reduction-tier-a

Conversation

@jingfei195887

@jingfei195887 jingfei195887 commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Related to the footprint discussion in #20414. Three small, independent
changes that let size-constrained configurations drop code that currently
cannot be trimmed. Default (non-DEFAULT_SMALL) builds keep today's behavior.

  1. mm/mm_heap: optional inline for mm_addfreechunk()
    mm_addfreechunk() walks the free list and is inlined into every heap path
    (malloc, free, realloc, shrinkchunk, initialize), so each caller carries its
    own copy of the loop. New CONFIG_MM_INLINE_ADDFREECHUNK
    (default !DEFAULT_SMALL). When disabled, mm_addfreechunk.c emits one
    shared copy. The function body stays in one place (mm.h); the .c only
    instantiates it, so no code is duplicated.

  2. sched: optional inline for nxsched_add_prioritized()
    Inlined into all 18 call sites across the scheduler, semaphores, message
    queues and events. New CONFIG_SCHED_INLINE_ADDPRIORITIZED
    (default !DEFAULT_SMALL), same single-body pattern via
    sched_addprioritized.c.

  3. libc: build lib_cxx_initialize() only for C++ init
    Without CONFIG_HAVE_CXXINITIALIZE the function body compiles away, but the
    empty function and its call from nxtask_startup() remain. The file is now
    built only when C++ static initialization is supported; otherwise the call
    is a no-op macro.

Items 1 and 2 were made inline for performance in #14144 and #14059. That
stays the default; only DEFAULT_SMALL builds switch to the shared copy.

Impact

  • Default builds: no functional or size change from items 1 and 2. Both new
    options default to y unless CONFIG_DEFAULT_SMALL=y.
  • DEFAULT_SMALL builds: one extra call/return on heap free-list insertion and
    on priority-list insertion, in exchange for code size.
  • No API or ABI change. lib_cxx_initialize() is libc-internal
    (libs/libc/libc.h).
  • Make and CMake are both updated.

Testing

Host: Ubuntu x86_64, arm-none-eabi-gcc 10.3.1 20210621. Rebased on master
734b0c5757. All sizes below come from clean CMake builds.

Size, stm32f103-minimum:nsh (DEFAULT_SMALL=y):

text data bss
master (734b0c5) 51975 468 2952
this PR 51399 468 2952
delta -576 0 0

Per commit, applied in order: mm_addfreechunk -360, nxsched_add_prioritized
-200, lib_cxx_initialize -16.

Both states of each option build, and the out-of-line symbol is present
only when the option is off:

configuration (at this PR) text out-of-line symbols
defaults (DEFAULT_SMALL=y, both inline options off) 51399 mm_addfreechunk, nxsched_add_prioritized
MM_INLINE_ADDFREECHUNK=y, SCHED_INLINE_ADDPRIORITIZED=y 51967 none
HAVE_CXXINITIALIZE=y 51455 lib_cxx_initialize linked

Other checks:

  • sim:smp with both inline options off: builds; one shared copy of each function.
  • ./tools/checkpatch.sh -g master..HEAD: all checks pass. cmake-format --check: clean.

Runtime, sim:ostest: run with both inline options off and, as a control,
with both on (same tree). Both runs reached waitpid_test inside a 540 s
timeout with no ERROR, assertion or panic, and went through the same tests.
Neither configuration finished ostest in that window, so this shows no
difference between the two configurations rather than a full ostest pass.

mm_addfreechunk() walks the free list and is inlined into every heap
path (malloc, free, realloc, shrinkchunk, initialize), so each caller
carries its own copy of the loop.

Add MM_INLINE_ADDFREECHUNK, default !DEFAULT_SMALL. Small builds now
emit one shared copy; default builds are unchanged.

stm32f103-minimum:nsh (DEFAULT_SMALL=y): text -360 bytes.

Signed-off-by: jingfei <[email protected]>
nxsched_add_prioritized() is inlined into all 18 call sites across
the scheduler, semaphores, message queues and events.

Add SCHED_INLINE_ADDPRIORITIZED, default !DEFAULT_SMALL. Small builds
now use one shared copy; default builds are unchanged.

stm32f103-minimum:nsh (DEFAULT_SMALL=y): text -200 bytes.

Signed-off-by: jingfei <[email protected]>
Without CONFIG_HAVE_CXXINITIALIZE the body of lib_cxx_initialize()
compiles away, but the empty function and its call from
nxtask_startup() remain. Build the file only when C++ static
initialization is supported and make the call a no-op otherwise.

stm32f103-minimum:nsh: text -16 bytes.

Signed-off-by: jingfei <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 13, 2026 10:41

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

2 open findings
What changed in this PR

Reduces code size for DEFAULT_SMALL builds by making two hot-path helpers optionally out-of-line and by removing an always-built (but often empty) C++ init stub from non-C++ configurations.

Changes:

  • Add CONFIG_MM_INLINE_ADDFREECHUNK / CONFIG_SCHED_INLINE_ADDPRIORITIZED to switch between header-inlined vs single out-of-line instantiation.
  • Add small .c “instantiation” units and wire them into both Make and CMake builds when inlining is disabled.
  • Build lib_cxx_initialize.c only when CONFIG_HAVE_CXXINITIALIZE=y, and provide a no-op replacement otherwise.
File Description
sched/​sched/​sched_addprioritized.c New instantiation unit to emit one out-of-line nxsched_add_prioritized()
sched/​sched/​sched.h Conditional inline vs external definition for nxsched_add_prioritized()
sched/​sched/​Make.defs Conditionally build sched_addprioritized.c when not inlining
sched/​sched/​CMakeLists.txt Same conditional inclusion for CMake builds
sched/​Kconfig Add SCHED_INLINE_ADDPRIORITIZED option defaulting off for DEFAULT_SMALL
mm/​mm_heap/​mm_addfreechunk.c New instantiation unit to emit one out-of-line mm_addfreechunk()
mm/​mm_heap/​mm.h Conditional inline vs external definition/prototype for mm_addfreechunk()
mm/​mm_heap/​Make.defs Conditionally build mm_addfreechunk.c when not inlining
mm/​mm_heap/​CMakeLists.txt Same conditional inclusion for CMake builds
mm/​Kconfig Add MM_INLINE_ADDFREECHUNK option defaulting off for DEFAULT_SMALL
libs/​libc/​misc/​lib_cxx_initialize.c Remove internal #ifdef since file is now conditionally built
libs/​libc/​misc/​Make.defs Only build lib_cxx_initialize.c when C++ init is supported
libs/​libc/​misc/​CMakeLists.txt Same conditional inclusion for CMake builds
libs/​libc/​libc.h No-op fallback for lib_cxx_initialize() when C++ init is unavailable

🧠 Review effort: Lite


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread mm/mm_heap/mm_addfreechunk.c Resolved
Comment thread libs/libc/libc.h Resolved
@jingfei195887
jingfei195887 marked this pull request as draft October 13, 2026 13:09
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report

arduino-mega2560

  • flash: .text -978 B (-1.5%, 64,958 B / 262,144 B, total: 25% used)

hifive1-revb

  • flash: .text -312 B (-0.4%, 83,940 B / 4,194,304 B, total: 2% used)

mirtoo

  • kseg0_progmem: .text -832 B (-1.2%, 67,104 B / 131,072 B, total: 51% used)

qemu-intel64

  • Code: .text -32 B (-0.0%, 8,682,480 B)

rx65n-rsk2mb

  • ROM: .text -16 B (-0.0%, 87,060 B / 2,097,152 B, total: 4% used)

stm32-nucleo-f103rb

@acassis
acassis marked this pull request as ready for review October 15, 2026 11:16
@xiaoxiang781216
xiaoxiang781216 merged commit 6b69a58 into apache:master Oct 15, 2026
52 of 53 checks passed
@raiden00pl

raiden00pl commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

"fixup!" commits should not be merged! That’s completely messed up CI!

@xiaoxiang781216

Copy link
Copy Markdown
Contributor

"fixup!" commits should not be merged! That’s completely messed up CI!

could you explain more? if it's a hard rule, let's add to the style check.

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

Labels

Area: Memory Management Memory Management issues Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants