This is the mail archive of the gcc@gcc.gnu.org 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]

switch statement performance


today i wanted to go on a performance-related witch hunt, so i added:

--------

--- stmt.c.orig Mon Jan 19 19:39:41 2004
+++ stmt.c      Mon Jan 19 19:57:25 2004
@@ -5393,6 +5393,12 @@
               || (TREE_CODE (index_expr) == COMPOUND_EXPR
                   && TREE_CODE (TREE_OPERAND (index_expr, 1)) == INTEGER_CST))
        {
+         /* If the sequence would be absurdly long and slow, send warning.  */
+         if (extra_warnings
+             && count >= case_values_threshold ()
+             && compare_tree_int (range, 10 * count) > 0)
+           warning ("large range, slow code");
+
          index = expand_expr (index_expr, NULL_RTX, VOIDmode, 0);
 
          /* If the index is a short or char that we do not have

--------

and then ran -W against bind8, bind9, the freebsd kernel, and other smaller
things.  the results weren't amazing or stupendous, but they were quite
useful.  one of the things i learned is that a lot of sparse switch stmts
should be left as they are for cleanliness reasons, since they are not
inside loops or otherwise called often enough to warrant rewriting them to
avoid the if-else code generation behaviour signalled by the above warning().

therefore if this were to become part of standard gcc, the following things
are desirable:

	1. it should be turned on with "-Wswitch-ifelse", not "-W".

	2. there should be a pragma or attribute to turn it off on
	   any given switch statement, if a code review finds that
	   it's not performance related.

sadly, i've already reached beyond the top end of my gcc hacking ability,
so if those things were to be done, i would need help learning how.  and
of course, i'd need "permission" in the form of some indication from you
folks that this change, if done properly, would be accepted back into gcc.


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