linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* doubt about "switch" - default case in af_inet.c
@ 2004-12-09 13:57 Phani Kandula
  2004-12-09 14:09 ` linux-os
  2004-12-16 18:43 ` Bill Davidsen
  0 siblings, 2 replies; 3+ messages in thread
From: Phani Kandula @ 2004-12-09 13:57 UTC (permalink / raw)
  To: linux-kernel

Hi all,

I'm a newbie to the Linux. I'm using 2.6.8 kernel. 
In /usr/src/linux/net/ipv4/af_inet.c I came across this...
 <code>
     switch (sock->state) {
     default:
     //do something..
         goto out;
     case SS_CONNECTED:
     //do something..
         goto out;
     case SS_CONNECTING:
     //do something..
         break;
     case SS_UNCONNECTED:
     //do something..
         break;
     }
 </code>

Is there any advantage in having 'default' as the first case? 
My understanding is that it will be useful only when 'default' is the
most likely case (in general).

Even then, my doubt: How will compiler (say gcc) implement 'default'
as the first value? Program is supposed to see all the cases and then
decide 'default'. Is this correct?

So, is this the best way to do it? please clarify..

Thanks,
Phani

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: doubt about "switch" - default case in af_inet.c
  2004-12-09 13:57 doubt about "switch" - default case in af_inet.c Phani Kandula
@ 2004-12-09 14:09 ` linux-os
  2004-12-16 18:43 ` Bill Davidsen
  1 sibling, 0 replies; 3+ messages in thread
From: linux-os @ 2004-12-09 14:09 UTC (permalink / raw)
  To: Phani Kandula; +Cc: linux-kernel

On Thu, 9 Dec 2004, Phani Kandula wrote:

> Hi all,
>
> I'm a newbie to the Linux. I'm using 2.6.8 kernel.
> In /usr/src/linux/net/ipv4/af_inet.c I came across this...
> <code>
>     switch (sock->state) {
>     default:
>     //do something..
>         goto out;
>     case SS_CONNECTED:
>     //do something..
>         goto out;
>     case SS_CONNECTING:
>     //do something..
>         break;
>     case SS_UNCONNECTED:
>     //do something..
>         break;
>     }
> </code>
>
> Is there any advantage in having 'default' as the first case?
> My understanding is that it will be useful only when 'default' is the
> most likely case (in general).
>
> Even then, my doubt: How will compiler (say gcc) implement 'default'
> as the first value? Program is supposed to see all the cases and then
> decide 'default'. Is this correct?
>
> So, is this the best way to do it? please clarify..
>
> Thanks,
> Phani

Generally speaking when you see "strange" code mixed with gotos,
somebody has adjusted the code, often by looking at the assembly
output, to maximize the performance.

Of course, when the compiler changes, the code may no longer be
optimum.

In principle, if the labels in a switch represent incrementing
numbers, the compiler can generate fast code by using that fact.
However, often little care is taken in the placement of these
labels or the numerical differences between them so the code output
degenerates to a bunch of "compares". In this case, the most common
value should be the one checked first.

Cheers,
Dick Johnson
Penguin : Linux version 2.6.9 on an i686 machine (5537.79 BogoMips).
  Notice : All mail here is now cached for review by John Ashcroft.
                  98.36% of all statistics are fiction.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: doubt about "switch" - default case in af_inet.c
  2004-12-09 13:57 doubt about "switch" - default case in af_inet.c Phani Kandula
  2004-12-09 14:09 ` linux-os
@ 2004-12-16 18:43 ` Bill Davidsen
  1 sibling, 0 replies; 3+ messages in thread
From: Bill Davidsen @ 2004-12-16 18:43 UTC (permalink / raw)
  To: Phani Kandula; +Cc: linux-kernel

Phani Kandula wrote:
> Hi all,
> 
> I'm a newbie to the Linux. I'm using 2.6.8 kernel. 
> In /usr/src/linux/net/ipv4/af_inet.c I came across this...
>  <code>
>      switch (sock->state) {
>      default:
>      //do something..
>          goto out;
>      case SS_CONNECTED:
>      //do something..
>          goto out;
>      case SS_CONNECTING:
>      //do something..
>          break;
>      case SS_UNCONNECTED:
>      //do something..
>          break;
>      }
>  </code>
> 
> Is there any advantage in having 'default' as the first case? 
> My understanding is that it will be useful only when 'default' is the
> most likely case (in general).
> 
> Even then, my doubt: How will compiler (say gcc) implement 'default'
> as the first value? Program is supposed to see all the cases and then
> decide 'default'. Is this correct?
> 
> So, is this the best way to do it? please clarify..

Just so. The compiler will check some or all of the cases first, and 
then take the default case. If you look at the code with and without 
optimization (use -S) you will see that it behaves the same way 
regardless of the placement of the default case. There is one exception, 
that is the one where control falls through from one case to another, 
and the default case is not last and lacks a break, such that the 
default winds up executing the code from the case(s) following.

Do I have to say that the code doing stuff like that is hard to read?

-- 
    -bill davidsen (davidsen@tmr.com)
"The secret to procrastination is to put things off until the
  last possible moment - but no longer"  -me

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2004-12-16 18:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-09 13:57 doubt about "switch" - default case in af_inet.c Phani Kandula
2004-12-09 14:09 ` linux-os
2004-12-16 18:43 ` Bill Davidsen

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 NNTP newsgroup(s).