From: Paul Koning <paulkoning@comcast.net>
To: GCC Development <gcc@gcc.gnu.org>
Subject: Switch statement optimization
Date: Mon, 18 Apr 2022 10:41:03 -0400 [thread overview]
Message-ID: <321638F8-8BF6-4AFC-8434-471B05F6BBFA@comcast.net> (raw)
In switch statements with dense case values, the typical result is a jump table, which is fast. If the values are sparse, a tree of compares is generated instead.
What if nearly all cases are dense but there are a few outliers? An example appears in the NFS protocol parser, where you get a switch statement with cases for each of the opcode values. All but one are small integers assigned in sequence, but one is 10044. So the "sparse" case kicks in and a compare tree is generated for everything.
I can avoid this by putting in special case code for the 10044 case, then all the rest ends up being a jump table. That brings up the question if GCC should recognize such scenarios and break up the switch statement into "dense parts" handled by a jump table, leaving the sorting between those as a compare tree.
paul
next reply other threads:[~2022-04-18 14:41 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-18 14:41 Paul Koning [this message]
2022-04-19 14:33 ` Martin Liška
-- strict thread matches above, loose matches on Subject: below --
2006-07-10 14:48 Joern RENNECKE
2006-07-11 10:03 ` Christian Hildner
2006-07-10 9:39 Christian Hildner
2006-07-10 11:06 ` Ben Elliston
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=321638F8-8BF6-4AFC-8434-471B05F6BBFA@comcast.net \
--to=paulkoning@comcast.net \
--cc=gcc@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).