Article View: comp.lang.c
Article #58Re: Taming The Switch-Statement
From: bart
Date: Thu, 08 Oct 2026 11:24
Date: Thu, 08 Oct 2026 11:24
149 lines
8093 bytes
8093 bytes
On 08/10/2026 05:49, Waldek Hebisch wrote:
> bart <bc@freeuk.com> wrote:
>> This generates about 5KB of code when expanded. You say the full version
>> is nearly 1MB? So what is the problem: generating that 1MB, or having to
>> process it?
>
> Macro expansion is pretty fast. It is production of machine code that
> takes time.
>
>> It's roughly 30Kloc so if it is processing all that code, then it would
>> be the same problem as dealing with those big headers. Less in fact
>> since presumably it is only processed once if it contain function defs.
>
> Sorry, you do not get it. Normal headers consist of declarations.
> gcc is reasonable fast at parsing and handling declarations. But
> above you have actual executable code and translating it to
> machine code takes time.
If I understand your point, it is that LPS throughput for C can be
problematic because source code can be a mix of executable code,
unexpanded macro invocations and declarations?
When producing benchmark figures, you try to avoid extreme cases of
excessive macro use and disproportionately large headers even containing
only declarations.
Such figures can still be useful when building fairly sane code-bases
with normal-sized headers and moderate use of macros.
Your example that expands to nearly 1MB, generates long expansions with
few line-breaks, so line-count is tricky anyway.
Now here I assumed the 42ms -O0 figure was for this program. But at an
average density of 20 or so characters/line, that would equate to around
a million lines per second throughput!
So I had to go back a few posts to read it more carefully. This program
takes 1.45s at -O0, so roughly 30Klps at that code density.
This is not bad for gcc.
>> ('bcc' is poor here partly because of how this project is written: full
>> of deeply nested macros and function calls. This seems to be typical of
>> interpreters written in C.
>
> The question frequently is "who is doing optimization?". Original
> C design was to have programmer do most optimization and use
> relatively dumb compiler. But is is easier to write the code
> assuming aggressive compiler optimization. Basically, you write
> rather general code, which always works, but is specific cases
> when translated anively performs useless operations. In the past
> programmer would specialize such code by hand, leaving only what is
> needed in each case. But now compiler optimizations can do most
> of this work.
My problem is that gcc/clang are slow even at -O0! So the question is
why, and could something have been done about that.
People here may say that even -O0 is doing static analysis etc. But
suppose you don't want that. My guess is the infrastructure needed to
enable optimisations gets in the way even for -O0.
>> My own interpreter, written in plain code and running the same
>> benchmark, takes 1.2 seconds.)
>
> Lua interpreter is recognized as rather slow.
Not any more. Lua 5.5 is quite nifty, and can match or beat my
interpreter on some tests, although not for Fibonacci!
> But you frequently
> compare apples with oranges.
I compare my pure interpreter for a dynamically typed language with some
one else's interpreter for their dynamically typed language, on a
program running the same algorithm.
Mine usually has the edge, even though it does everything wrong. Lua has
a small number of built-in types only; it uses a compact, tagged 64-bit
value for all objects; it uses an efficient register based VM.
I have a lot more types plus user-defined ones. I use a complex 128-bit
descriptor. I use a stack-based VM. Plus I use an interpreter that is
unoptimised code!
So, why am I still fast? Maybe this is a clue; in lua.c there is this
line (may be part of a switch, but you can't tell):
vmcase(OP_ADDK) {
op_arithK(L, l_addi, luai_numadd);
vmbreak;
}
That op_arithK() is actually a macro invocation that expands to this:
{TValue*v1=(&((base+(((void)0),((((int)((((i)>>((((0+7)+8)+1)))&(
(~((~(Instruction)0)<<(8)))<<(0))))))))))->val);TValue*v2=(k+(((void)
0),((((int)((((i)>>(((((0+7)+8)+1)+8)))&((~((~(Instruction)0)<<(8)))<<
(0)))))))));((void)0);{StkId ra=(base+(((int)((((i)>>((0+7)))&((~((~
(Instruction)0)<<(8)))<<(0)))))));if(((((v1))->tt_)==(((3)|((0)<<4))))
&&((((v2))->tt_)==(((3)|((0)<<4))))){lua_Integer i1=(((void)0),(((v1)
->value_).i));lua_Integer i2=(((void)0),(((v2)->value_).i));pc++;{
TValue*io=((&(ra)->val));((io)->value_).i=(((lua_Integer)(((
lua_Unsigned)(i1))+((lua_Unsigned)(i2)))));((io)->tt_=(((3)|((0)<<4)))
);};}else{lua_Number n1;lua_Number n2;if((((((v1))->tt_)==(((3)|((1)
<<4))))?((n1)=(((void)0),(((v1)->value_).n)),1):(((((v1))->tt_)==(((3)
|((0)<<4))))?((n1)=((lua_Number)(((((void)0),(((v1)->value_).i))))),1)
:0))&&(((((v2))->tt_)==(((3)|((1)<<4))))?((n2)=(((void)0),(((v2)->
value_).n)),1):(((((v2))->tt_)==(((3)|((0)<<4))))?((n2)=((lua_Number)
(((((void)0),(((v2)->value_).i))))),1):0))){pc++;{TValue*io=((&(ra)->
val));((io)->value_).n=(((n1)+(n2)));((io)->tt_=(((3)|((1)<<4))));};
}};};};
(I'm familiar with this as I had to debug my compiler on it.)
Here, my guess is it's relying on the compiler to clean up the mess.
While my equivalent is plain code.
(Note that in Lua 5.5, 'ADD' I believe is only defined for INT and FLOAT
types; it used to be just FLOAT. I have 'ADD' defined for 5 types.)
> People use interpreted languages
> either for safety, that is runtime checks or for fancy language features
> or both.
People use such languages because they are easier to use, they can be
more productive, and there is no annoying build-step.
(No static analysis either, and no warnings; how is that possible?)
Some may use them for prototyping or development before porting to a
compiled language.
> So really, comparing interpreters for
> different languages have limited use.
If you say so. But it sounds to me like making excuses for a slow product.
Endless efforts have been made over the last two decades in making
scripting languages run fast. Apart from approaches using type inference
or tracing-JIT, this also applies to pure interpreters.
Part of that involves benchmarking.
So can certainly compare language implementations.
Technical Details
Message-ID:
<11a7uhp$3p2ve$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<119qf6l$2rmg3$1@dont-email.me> <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> <11a5nca$gg7c$1@paganini.bofh.team> <11a63og$34v73$1@dont-email.me> <11a77cs$odht$1@paganini.bofh.team>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: bart <bc@freeuk.com> Newsgroups: comp.lang.c Subject: Re: Taming The Switch-Statement Date: Thu, 8 Oct 2026 12:24:40 +0100 Organization: A noiseless patient Spider Lines: 149 Message-ID: <11a7uhp$3p2ve$1@dont-email.me> References: <119qf6l$2rmg3$1@dont-email.me> <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> <11a5nca$gg7c$1@paganini.bofh.team> <11a63og$34v73$1@dont-email.me> <11a77cs$odht$1@paganini.bofh.team> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 08 Oct 2026 11:24:42 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3967982"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+rIaEkHXaEPLv1w+mNnyUz"; posting-host="929295152ff81c9165127c75309b2bee" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:OCXRzkyNSBVQ69Q/8GIzf70VAUI= sha256:ZiT0kxvQfH3Yzuwydww1ovXozdssRuB8QmtMGT/SXEM= sha1:4wxk+WYz9it6CCD/GcovXs/50tU= sha256:MdLsFSfYz/o/u+rkjqjCmIRMNJxBUG3tdOyESl8rTdg= Content-Language: en-GB In-Reply-To: <11a77cs$odht$1@paganini.bofh.team>