Article View: comp.lang.c
Article #33Re: Taming The Switch-Statement
From: Waldek Hebisch
Date: Wed, 07 Oct 2026 15:10
Date: Wed, 07 Oct 2026 15:10
232 lines
10777 bytes
10777 bytes
bart <bc@freeuk.com> wrote:
> On 07/10/2026 05:19, Waldek Hebisch wrote:
>> bart <bc@freeuk.com> wrote:
>>> On 07/10/2026 00:14, Waldek Hebisch wrote:
>>>> bart <bc@freeuk.com> wrote:
>>>
>>>>> What program can gcc compile in 10ms, Hello World?
>>>>
>>>> On my machine the following non-canonical version of Hello World:
>>>>
>>>> #include <unistd.h>
>>>> int
>>>> main(void) {
>>>> write(1, "Hello\n", 6);
>>>> return 0;
>>>> }
>>>>
>>>> takes about 20-22 ms. More realistically, compilation of a lead
>>>> C program (66 lines) and linking of several .o files (many
>>>> produced by a non-C compiler) + libc needs 23 ms producing
>>>> 66 kB executable (of which 20 kB is code and the rest is mainly
>>>> debug info). The same, but linking statically to libc needs 56 ms
>>>> and produces 550 kB executable (of which 413 kB is code).
>>>> Note: the last two are for Risc-V. That is compiler runs on my PC,
>>>> but executables only run on Risc-V machine. And actually
>>>> transferring the executable takes more time than C compilation.
>>>
>>> So, on your machine, what's the throughput of gcc in lines per second?
>>>
>>> I mean something more accurate than the 300lps of your 6-line example
>>> which will be mostly due to overheads.
>>
>> Above you also have 66 line file. gcc compile time depends very
>> much on source file. One case is 647 lines (17 kB) file, strightforward
>> code without attempt to cram too much into each line. Plain 'gcc -Wall'
>> (so no optimization) needs 42 ms. This code is actually doing
>> possibly large work,
>
> What does that mean, work done at runtime? That shouldn't affect it.
Yes.
> The
> compiler will only see a bunch of tokens related to the size of the source.
You want useful result, that is why '-O'.
> so more realistic is 'gcc -O -Wall' which
>> needs 67 ms. 'gcc -O2 -Wall' takes 114 ms and 'gcc -O3 -Wall' takes
>> 119 ms (but are unlikely to produce significant speedup). Another
>> case is 847 lines file (24 kB) including 669 line (44 kB) header with
>> macros and 22 line header declaring functions. This is heavily macroized
>> code which macroexpandd to about 900 kB.
>
> Recent discussions about headers suggested even huge headers were a
> non-issue, according to some here.
Normal headers, yes. To give you an idea what is involved let me show
small part of header and the file. From 'arrpack.h':
#define PROC1N(N1, N2, TYPE, OP, ARGS...) /* 1 array normal */ \
int N1 ## A ## N2(ARGS TYPE *arr, int *spec) \
{ \
int err, off, d, nd; \
int *wk, *wp, *sps, *sp; \
TYPE *arrend; \
\
if (err = arrspec(spec, 1, 1, &off, &d, &nd, &sps, &wk)) \
return err; \
arr += off; \
\
while (1) { \
/* dimension 1 expanded */ \
for (arrend = arr+nd; arr < arrend; arr += d) \
{ OP } \
for (sp=sps, wp=wk; ; ) { /* loop over dims 2...N */ \
if (sp == wk) return 0; \
arr += *sp++; \
if (++(*wp) == *sp++) \
*wp++ = 0; \
else \
break; \
} \
} \
}
...
#define PROC1(N1, N2, TYPE, OP, ARGS...) \
PROC1N(N1, N2, TYPE, OP, ARGS) \
PROC1M(N1, N2, TYPE, OP, ARGS) \
PROC1I(N1, N2, TYPE, OP, ARGS)
From 'arrpack.c':
#include <math.h>
#include "arrpack.h"
/*
-- Single array -------------------------------------------------------
*/
#define N2 zero
#define OP *arr = 0;
PROC1(b, N2, unsigned char, OP, )
PROC1(i, N2, int, OP, )
PROC1(s, N2, float, OP, )
PROC1(d, N2, double, OP, )
PROC1(c, N2, __complex__ float, OP, )
PROC1(z, N2, __complex__ double, OP, )
#undef OP
#undef N2
As you can see this creates 18 not so small functions.
> I have an example of a 32KB file that expands to 4MB after
> preprocessing. Here is compile-speed:
>
> Time EXE size
> gcc -O0 16 secs 3.0 MB
> gcc -O2 42 secs 1.5 MB
> tcc 0.37 secs 3.6 MB
>
>
> I'm not arguing against never using gcc or never using optimisations.
>
> I'm saying that for my development patterns (compile 100s of times a
> day), it is just too slow and unresponsive to use routinely.
OK
>> Consider silly example,
>> but small enough to give it here in full (targeting Risc-V):
>>
>> long f6(void) {
>> return 6;
>> }
>>
>> 'tcc -c' gives the following code (after disassembly):
>>
>> 0000000000000000 <f6>:
>> 0: ff010113 add sp,sp,-16
>> 4: 00113423 sd ra,8(sp)
>> 8: 00813023 sd s0,0(sp)
>> c: 01010413 add s0,sp,16
>> 10: 00000013 nop
>> 14: 0060051b addw a0,zero,6
>> 18: 00813083 ld ra,8(sp)
>> 1c: 00013403 ld s0,0(sp)
>> 20: 01010113 add sp,sp,16
>> 24: 00008067 ret
>>
>> that is 10 instructions, 40 bytes. 'gcc -c' (so no optimization)
>> gives:
>>
>> 0000000000000000 <f6>:
>> 0: 1141 add sp,sp,-16
>> 2: e422 sd s0,8(sp)
>> 4: 0800 add s0,sp,16
>> 6: 4799 li a5,6
>> 8: 853e mv a0,a5
>> a: 6422 ld s0,8(sp)
>> c: 0141 add sp,sp,16
>> e: 8082 ret
>>
>> so 8 instructions, 16 bytes. 'gcc -O -c' gives:
>
> What does '-O' do? On my gcc it gives same results as -O0.
>
>> 0000000000000000 <f6>:
>> 0: 4519 li a0,6
>> 2: 8082 ret
>>
>> so 2 instructions, 4 bytes.
>
> My bcc product gives better code than tcc while nearly as fast
> compiling. For your example, bcc generates this for x64:
>
> f6:
> mov eax, 6
> ret
>
> These are EXE sizes for Lua, for x64/WinABI
>
> gcc -O0 465 KB
> gcc -O2 414 KB
> gcc -O3 466 KB
> gcc -Os 328 KB
>
> tcc 384 KB
> bcc 237 KB
>
>> One things is that apparently tcc does not
>> support Risc-V compressed instructions, which leads to considerably
>> bigger code. Another thing is that tcc inserts a 'nop' for no good
>> reason. And even without optimization gcc realizes that nothing
>> clobbers link register so avoids storing and restoring it. gcc
>> produces constant in "wrong" register, so has to move it, but in the
>> balance looks slightly better than tcc.
>
> My point above is that you can do minor 'optimisations' (generating
> sensible code, plus peep-hole stuff) with very little cost.
>
> (My bcc product has an extra step, which allocates some locals to
> registers. Plus a peep-hole optimiser stage which doesn't make it any
> faster, but makes the code a little smaller.
>
> This has zero-cost, so it is enabled by default. It is zero-cost because
> the extra time to do it (only 10% anyway) if offset by having less code
> to deal with in the later stages.
>
> If I disable it, then lua.exe ends up at 289KB - still smaller than gcc.)
>
>
>> 'gcc -O' clearly gives much better code, on low end machines it is
>> likely to be 5 times faster.
>
> If that means -O2 or -O3, then on my programs, it is more typically 2x
> as fast, for C code. But more like 1.5 x as fast for the C code
> transpiled from my language using my old transpiler, for language projects.
I meant '-O'. In gcc '-O' means simpler and faster optimizations,
which almost always lead to faster code. '-O2' means havier
optimizations, and '-O3' even more heavy. Also, optimizations
at '-O2' or '-O3' level may backfire: transformation that is
intended to increase speed may actually decrease it.
On high-end machines CPU frequently is quite powerful, but execution
speed is limited by memory access. In such case executing say twice
as many instructions as needed costs only a little. But on low end
machines executing useless instructions cases significant slowdown.
--
Waldek Hebisch
Technical Details
Message-ID:
<11a5nca$gg7c$1@paganini.bofh.team>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!newsfeed.bofh.team!paganini.bofh.team!not-for-mailReferences:
<119qf6l$2rmg3$1@dont-email.me> <119uqnv$d6j2$1@dont-email.me> <20261005143559.157@kylheku.com> <11a1arj$1e8ks$1@dont-email.me> <11a295r$1lqal$3@dont-email.me> <11a2e2m$ef4o$2@dont-email.me> <11a2j83$1qu9n$1@dont-email.me> <11a2maf$1soc7$1@dont-email.me> <11a2vrn$20jj3$1@dont-email.me> <EI7xS.906$aNSb.21@fx20.iad> <11a37gv$23ruj$1@dont-email.me> <11a3vcj$88e7$1@paganini.bofh.team> <11a43hl$2e5k2$1@dont-email.me> <11a4h8v$eeb8$1@paganini.bofh.team> <11a574n$2pano$1@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!newsfeed.bofh.team!paganini.bofh.team!not-for-mail
From: antispam@fricas.org (Waldek Hebisch)
Newsgroups: comp.lang.c
Subject: Re: Taming The Switch-Statement
Date: Wed, 7 Oct 2026 15:10:05 -0000 (UTC)
Organization: bofh.team Usenet service
Message-ID: <11a5nca$gg7c$1@paganini.bofh.team>
References: <119qf6l$2rmg3$1@dont-email.me> <119uqnv$d6j2$1@dont-email.me> <20261005143559.157@kylheku.com> <11a1arj$1e8ks$1@dont-email.me> <11a295r$1lqal$3@dont-email.me> <11a2e2m$ef4o$2@dont-email.me> <11a2j83$1qu9n$1@dont-email.me> <11a2maf$1soc7$1@dont-email.me> <11a2vrn$20jj3$1@dont-email.me> <EI7xS.906$aNSb.21@fx20.iad> <11a37gv$23ruj$1@dont-email.me> <11a3vcj$88e7$1@paganini.bofh.team> <11a43hl$2e5k2$1@dont-email.me> <11a4h8v$eeb8$1@paganini.bofh.team> <11a574n$2pano$1@dont-email.me>
X-Complaints-To: abuse@bofh.team
Injection-Date: Wed, 7 Oct 2026 15:10:05 -0000 (UTC)
Injection-Info: paganini.bofh.team; logging-data="540908"; mail-complaints-to="usenet@bofh.team"; posting-host="d028f9ad2fa2c51f22dbfed0d81d7daf"; posting-account="69e66f1681224ab92235b464ce7a90b6";
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (Linux/6.1.0-9-amd64 (x86_64))
X-Postfilter: Postfilter-NG 2026.09.1-rc3