Commit 34e359f0

34e359f0f4b9Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 2 files · ~2 modified

Description

perf: enable -ffunction-sections + --gc-sections for dead code elimination

Compile sp_bigint.o and regexp .o with -ffunction-sections -fdata-sections so the linker can strip unused functions when linking user programs with --gc-sections.

Results (text section size): puts "hello": 3431 → 2134 bytes (-38%) factorial(50): 39559 → 35407 bytes (-10%)

These flags are supported since GCC 2.7 (1996) / binutils 2.17 (2006), covering GCC 4.2.1 (FreeBSD) and GCC 4.5.2 onward.

Changes:

  • Makefile: SEC_FLAGS/GC_FLAGS variables, applied to .o builds and test/bench link steps
  • spinel wrapper: add flags to user-facing cc invocation

75/75 tests pass, bootstrap stable.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
34e359f0f4b9
Parent
ec200ad10f3a

MakefilemodifiedView file

@@ -10,6 +10,10 @@
1010
1111 CC ?= cc
1212 CFLAGS = -O2 -Wno-all
13+# Per-function sections allow the linker to strip unused bigint/regexp
14+# functions from the final binary (supported since GCC 2.7 / binutils 2.17).
15+SEC_FLAGS = -ffunction-sections -fdata-sections
16+GC_FLAGS = -Wl,--gc-sections
1317
1418 # Prism library (auto-detect from gem, or set PRISM_DIR manually)
1519 PRISM_DIR ?= $(shell ruby -rprism -e 'puts $$LOADED_FEATURES.grep(/prism/).first.sub(%r{/lib/.*}, "")' 2>/dev/null)
@@ -51,11 +55,11 @@
5155
5256 build/regexp/%.o: lib/regexp/%.c lib/regexp/re_internal.h
5357 @mkdir -p build/regexp
54- $(CC) -c -O2 -Ilib/regexp $< -o $@
58+ $(CC) -c -O2 $(SEC_FLAGS) -Ilib/regexp $< -o $@
5559
5660 build/sp_bigint.o: lib/sp_bigint.c lib/sp_bigint.h lib/mruby_shim.h
5761 @mkdir -p build
58- $(CC) -c -O2 -Wno-all -Ilib lib/sp_bigint.c -o build/sp_bigint.o
62+ $(CC) -c -O2 -Wno-all $(SEC_FLAGS) -Ilib lib/sp_bigint.c -o build/sp_bigint.o
5963
6064 SP_RT_LIB = lib/libspinel_rt.a
6165
@@ -91,7 +95,7 @@
9195 bn=$$(basename "$$f" .rb); \
9296 ./spinel_parse "$$f" /tmp/_sp_t.ast 2>/dev/null && \
9397 ./spinel_codegen /tmp/_sp_t.ast /tmp/_sp_t.c 2>/dev/null && \
94- $(CC) $(CFLAGS) -Ilib /tmp/_sp_t.c $(SP_RT_LIB) -lm -o /tmp/_sp_t_bin 2>/dev/null; \
98+ $(CC) $(CFLAGS) $(SEC_FLAGS) -Ilib /tmp/_sp_t.c $(SP_RT_LIB) -lm $(GC_FLAGS) -o /tmp/_sp_t_bin 2>/dev/null; \
9599 if [ $$? -eq 0 ]; then \
96100 expected=$$(timeout 10 ruby "$$f" 2>/dev/null); \
97101 actual=$$(timeout 10 /tmp/_sp_t_bin 2>/dev/null); \
@@ -114,7 +118,7 @@
114118 bn=$$(basename "$$f" .rb); \
115119 timeout 10 ./spinel_parse "$$f" /tmp/_sp_b.ast 2>/dev/null && \
116120 timeout 10 ./spinel_codegen /tmp/_sp_b.ast /tmp/_sp_b.c 2>/dev/null && \
117- $(CC) $(CFLAGS) -Ilib /tmp/_sp_b.c $(SP_RT_LIB) -lm -o /tmp/_sp_b_bin 2>/dev/null; \
121+ $(CC) $(CFLAGS) $(SEC_FLAGS) -Ilib /tmp/_sp_b.c $(SP_RT_LIB) -lm $(GC_FLAGS) -o /tmp/_sp_b_bin 2>/dev/null; \
118122 if [ $$? -eq 0 ]; then \
119123 expected=$$(timeout 60 ruby "$$f" 2>/dev/null); \
120124 ruby_rc=$$?; \