This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: switch statement performance

Paul Vixie <> 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 <>
> 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 <>

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]