This is the mail archive of the
mailing list for the GCC project.
Re: switch statement performance
- From: Geoff Keating <geoffk at geoffk dot org>
- To: Paul Vixie <paul at vix dot com>
- Cc: gcc at gcc dot gnu dot org
- Date: 20 Jan 2004 16:07:45 -0800
- Subject: Re: switch statement performance
- References: <email@example.com> <20040120222630.0DC0214BBE@sa.vix.com>
Paul Vixie <firstname.lastname@example.org> writes:
> > You didn't explain why you wanted to do this, or what problem you're
> > trying to solve; I feel like this is the last message of a long
> > discussion. Are you concerned about the object code quality, or the
> > quality of the C code that GCC is compiling?
> > --
> > Geoffrey Keating <email@example.com>
> i had a program that was going real slow. turned out there was an expectation
> of jump table in a module, where a switch statement with ~50 cases had values
> specified as #define'd constants. the numeric values of these were all big
> 32-bit numbers where each set of 8 bits meant something special. the result
> was generated code equivilent to an if-else chain 50 expressions long, and
> when this was replaced with uglier code the function containing this inner-loop
> switch statement sped up by a factor of 1000.
Could you explain a bit more?
You see, the generated code shouldn't be equivalent to an if-else
chain. It's supposed to be a binary tree, which would be only at most
6 or 7 branches deep (well, I think we actually do a binary tree for
the outside conditions, and then an if-else chain for the inner ones,
since not-taken branches are cheaper), and therefore shouldn't have
particularly bad performance. Was the 'uglier code' particularly
Ideally, we'd like to have GCC generate efficient code automatically;
maybe not quite as clever as the programmer would have, but there
shouldn't be a factor of 1000 unless the programmer's *really* clever :-).
- Geoffrey Keating <firstname.lastname@example.org>