From: Willy Tarreau <willy@w.ods.org>
To: Rob Wilkens <robw@optonline.net>
Cc: Linus Torvalds <torvalds@transmeta.com>,
Christoph Hellwig <hch@infradead.org>, Greg KH <greg@kroah.com>,
Alan Cox <alan@lxorguk.ukuu.org.uk>,
William Lee Irwin III <wli@holomorphy.com>,
Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: any chance of 2.6.0-test*? -> goto example
Date: Sun, 12 Jan 2003 23:48:29 +0100 [thread overview]
Message-ID: <20030112224829.GA29534@alpha.home.local> (raw)
In-Reply-To: <1042404503.1208.95.camel@RobsPC.RobertWilkens.com>
Hi !
On Sun, Jan 12, 2003 at 03:48:24PM -0500, Rob Wilkens wrote:
[...]
> > For example, it is quite common to have conditionals THAT DO NOT NEST.
>
> Could someone (Linus, I know your busy, so I won't ask you -- in fact,
> I'm amazed you'd bother to write to _me_ at all) please point me to
> sample code that illustrates this. I'm trying to envision "conditionals
> that do not nest that need goto". As near as I can tell, there must be
> some condition that the goto nests all the conditions in otherwise the
> goto wouldn't be needed. In some cases, Maybe what's between the goto
> and whats gone to should be broken up into a new function (modularized)
> and called independently of function that the goto was in.
Look at any tree walking function or macro. Try to write an efficient one
yourself, with lots of time the same tests and code to walk left, right, up or
down. Always the same thing : just move pointers around and go on to the loop.
Of course you can write it in your "clear structured" code, but I bet many
people won't understand it because of the multiple nested tests. But a simple
goto in each if statement to go to a common place is far cleaner. It's about
the same as the final instruction in the for loop.
> As someone else pointed out, it's provable that goto isn't needed, and
> given that C is a minimalist language, I'm not sure why it was included.
Of course it's provable. Anyway, any algorithm can also be written with a
very complex S-K program ! But if you'd know how a processor works, you'd
understand the evidence : about 1/5 of instructions is a branch (goto). The
basics of computing are test, set and branch. Your while, for, if ... all
do a mix of these. Why do you want to put the branch as a special case ? it's
as much needed as others. And the only instruction which does only branch
without test nor set is goto.
What you do with your methods, is replacing a branch with a set, test then
branch. More efficient ? I don't think so ! More readable ? I don't think so.
Linus is right, you've been brainwashed :-)
In fact, at school, thet tell you not to use goto to force you to learn how to
use useful things such as the while loop. If you had used BASIC in the past,
you'd remember how awful these programs were, not because of all the gotos, but
because since we were never told the beauty of other methods, we simply used
crappy gotos everywhere within left-justified code.
> > The Pascal language is a prime example of the latter problem. Because it
> > doesn't have a "break" statement, loops in (traditional) Pascal end up
> > often looking like total shit, because you have to add totally arbitrary
> > logic to say "I'm done now".
>
> But at least the code is "readable" when you do that. Sure it's a
> little more work on the part of the programmer.
and the CPU ! Fortunately, Borland added the break and continue statements so
that their compiler could be used to produce good programs.
> But anyone reading the
> code can say "oh, so the loop exits when condition x is met", rather
> than digging through the code looking for any place their might be a
> goto.
I prefer a "goto end_of_loop" than a "end_of_loop=1" followed by nested
"if (!end_of_loop)" for each following blocks ! This way, you focus on
what the code really does and not how the programmer could trick the
compiler to tell the CPU what he wanted to really do.
> -Rob
Now, you asked for an example. Here is one. Please recode it without the
gotos, try to keep it at least as readable, and then bench it !
I'll send you public apologies if you can make it either faster and as
much readable, or as fast and much more readable. (yes, all the code
sits within a for condition, just to allow 'break' within the loop !).
As you see, I don't really mind writing awful primitives if they help
upper levels being much cleaner and efficient.
Cheers,
Willy
#define tree_foreach_destructive(__type, __root, __data, __stack, __slen) \
for (__slen = 0, __stack[0] = __root, __data = NULL; ({ \
__label__ __left, __right, __again, __end; \
typeof(__root) __ptr = __stack[__slen]; \
__again: \
__data = __ptr->data; \
if (__data != NULL) { \
__ptr->data = NULL; \
goto __end; \
} \
else if (__ptr->left != NULL) { \
__stack[++__slen] = __ptr = __ptr->left; \
goto __again; \
} \
else \
__left: \
if (__ptr->right != NULL) { \
__stack[++__slen] = __ptr = __ptr->right; \
goto __again; \
} \
else \
__right: \
if (!__slen--) \
goto __end; /* nothing left, don't delete the root node */ \
else { \
typeof (__root) __old; \
pool_free(__type, __ptr); \
__old = __ptr; \
__ptr = __stack[__slen]; \
if (__ptr->left == __old) { \
/* unlink this node from its parent */ \
__ptr->left = NULL; \
goto __left; \
} \
else { \
/* no need to unlink, the parent will also die */ \
goto __right; \
} \
} \
__end: \
(__slen >= 0); /* nothing after loop */}); )
next prev parent reply other threads:[~2003-01-12 22:40 UTC|newest]
Thread overview: 173+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-01-10 16:10 any chance of 2.6.0-test*? William Lee Irwin III
2003-01-10 16:28 ` Dave Jones
2003-01-10 16:41 ` Zwane Mwaikambo
2003-01-10 17:08 ` Dave Jones
2003-01-10 17:19 ` Alan Cox
2003-01-10 16:40 ` Jeff Garzik
2003-01-10 17:06 ` Dave Jones
2003-01-10 17:25 ` Jeff Garzik
2003-01-10 17:29 ` Linus Torvalds
2003-01-10 18:47 ` J.A. Magallon
2003-01-10 19:37 ` Matthew D. Pitts
2003-01-10 19:51 ` Jeff Garzik
2003-01-14 1:39 ` James H. Cloos Jr.
2003-01-15 21:46 ` [PATCH][RESEND w/ reasonable Subject] alsa before oss in Kconfig James H. Cloos Jr.
2003-01-10 18:16 ` any chance of 2.6.0-test*? Alan Cox
2003-01-10 17:37 ` Jochen Friedrich
2003-01-10 17:38 ` Linus Torvalds
2003-01-12 9:27 ` Greg KH
2003-01-12 16:55 ` Alan Cox
2003-01-12 17:05 ` Linus Torvalds
2003-01-12 17:17 ` Christoph Hellwig
2003-01-12 19:15 ` Linus Torvalds
2003-01-12 19:34 ` Rob Wilkens
2003-01-12 19:37 ` Rob Wilkens
2003-01-12 19:53 ` Tomas Szepe
2003-01-12 20:03 ` Rob Wilkens
2003-01-13 15:43 ` Terje Eggestad
2003-01-13 15:49 ` Jens Axboe
2003-01-13 16:25 ` Terje Eggestad
2003-01-13 16:26 ` Jens Axboe
2003-01-13 16:41 ` Terje Eggestad
2003-01-13 16:43 ` Jens Axboe
2003-01-13 17:00 ` Zwane Mwaikambo
2003-01-13 18:48 ` Jens Axboe
2003-01-13 22:14 ` Keith Owens
2003-01-13 22:42 ` John Levon
2003-01-13 22:49 ` Zwane Mwaikambo
2003-01-13 23:32 ` Valdis.Kletnieks
2003-01-13 23:43 ` Zwane Mwaikambo
2003-01-12 20:44 ` Alan Cox
2003-01-12 20:45 ` William Lee Irwin III
2003-01-13 13:14 ` Bernd Schmidt
2003-01-12 21:22 ` David Woodhouse
2003-01-18 13:04 ` Folkert van Heusden
2003-01-12 19:38 ` Linus Torvalds
2003-01-12 19:59 ` Rob Wilkens
2003-01-12 20:18 ` Valdis.Kletnieks
2003-01-12 20:23 ` yodaiken
2003-01-12 20:26 ` Tomas Szepe
2003-01-12 20:32 ` Sean Neakums
2003-01-12 20:51 ` Valdis.Kletnieks
2003-01-13 0:54 ` Randy Dunlap
2003-01-12 20:46 ` Kevin Puetz
2003-01-13 0:48 ` Randy Dunlap
2003-01-18 13:07 ` Folkert van Heusden
2003-01-12 20:20 ` David Ford
2003-01-12 20:22 ` Linus Torvalds
2003-01-12 20:33 ` Robert Love
2003-01-12 20:33 ` Linus Torvalds
2003-01-13 10:54 ` Horst von Brand
2003-01-13 11:09 ` Eric W. Biederman
2003-01-13 14:34 ` Terje Eggestad
2003-01-13 23:23 ` Bob Taylor
2003-01-12 20:48 ` Rob Wilkens
2003-01-12 20:59 ` Dimitrie O. Paun
2003-01-12 22:48 ` Willy Tarreau [this message]
2003-01-13 0:53 ` any chance of 2.6.0-test*? -> goto example Rob Wilkens
2003-01-13 1:31 ` Willy Tarreau
2003-01-13 16:10 ` Alexander Kellett
2003-01-14 10:36 ` Helge Hafting
2003-01-12 23:11 ` Coding style - (Was Re: any chance of 2.6.0-test*?) John Bradford
2003-01-13 11:07 ` any chance of 2.6.0-test*? Helge Hafting
2003-01-13 3:30 ` yodaiken
2003-01-12 21:29 ` Rik van Riel
2003-01-13 0:03 ` Scott Robert Ladd
2003-01-13 0:38 ` Rob Wilkens
2003-01-13 3:06 ` [OT] " J Sloan
2003-01-13 16:51 ` Emiliano Gabrielli
2003-01-13 0:38 ` Randy Dunlap
2003-01-12 19:41 ` Christoph Hellwig
2003-01-12 19:41 ` Rob Wilkens
2003-01-12 19:58 ` David Ford
2003-01-12 20:07 ` Rob Wilkens
2003-01-12 20:31 ` Oliver Neukum
2003-01-12 20:34 ` David Ford
2003-01-12 20:31 ` Robert Love
2003-01-12 21:02 ` Rob Wilkens
2003-01-12 21:15 ` Matti Aarnio
2003-01-12 21:27 ` Rob Wilkens
2003-01-12 21:40 ` Rik van Riel
2003-01-12 21:44 ` Rob Wilkens
2003-01-12 21:49 ` Aaron Lehmann
2003-01-12 22:07 ` Rob Wilkens
2003-01-12 22:18 ` Aaron Lehmann
2003-01-12 22:34 ` Rob Wilkens
2003-01-12 22:52 ` Aaron Lehmann
2003-01-12 23:11 ` Rob Wilkens
2003-01-12 23:31 ` Oliver Neukum
2003-01-12 23:39 ` Emiliano Gabrielli
2003-01-12 23:46 ` Rob Wilkens
2003-01-12 23:57 ` Emiliano Gabrielli
2003-01-13 0:08 ` Rob Wilkens
2003-01-13 16:02 ` Terje Eggestad
2003-01-12 22:53 ` Sean Neakums
2003-01-12 23:06 ` Oliver Neukum
2003-01-12 23:48 ` Rik van Riel
2003-01-12 21:58 ` Emiliano Gabrielli
2003-01-12 22:12 ` Rob Wilkens
2003-01-12 22:29 ` Olivier Galibert
2003-01-12 23:21 ` Alan Cox
2003-01-12 22:12 ` Oliver Neukum
2003-01-12 22:18 ` Aaron Lehmann
2003-01-12 22:35 ` Rob Wilkens
2003-01-13 8:15 ` Jens Axboe
2003-01-12 21:49 ` David Ford
2003-01-13 19:22 ` [OffTopic] [apology] " Rob Wilkens
2003-01-12 21:59 ` Adam Kropelin
2003-01-12 22:21 ` Rob Wilkens
2003-01-12 22:33 ` Tomas Szepe
2003-01-12 22:36 ` Emiliano Gabrielli
2003-01-12 22:38 ` Oliver Neukum
2003-01-12 22:42 ` romieu
2003-01-13 1:06 ` Randy Dunlap
2003-01-13 1:21 ` Rob Wilkens
2003-01-13 1:51 ` Nuno Monteiro
2003-01-13 2:19 ` Rob Wilkens
2003-01-13 2:51 ` Marcelo Pacheco
2003-01-13 3:37 ` Rob Wilkens
2003-01-13 4:53 ` Billy Rose
2003-01-13 3:06 ` Jochen Striepe
2003-01-13 3:40 ` Rob Wilkens
2003-01-13 9:45 ` Matti Aarnio
2003-01-13 3:48 ` Ryan Anderson
2003-01-13 4:37 ` Billy Rose
2003-01-12 22:06 ` Oliver Neukum
2003-01-12 22:22 ` Rob Wilkens
2003-01-12 22:43 ` Oliver Neukum
2003-01-12 22:51 ` Rob Wilkens
2003-01-12 23:00 ` Robert Love
2003-01-12 23:16 ` Rob Wilkens
2003-01-12 23:16 ` Nicolas Pitre
2003-01-12 23:05 ` Emiliano Gabrielli
2003-01-12 23:23 ` Oliver Neukum
2003-01-12 23:45 ` Emiliano Gabrielli
2003-01-12 23:45 ` Emiliano Gabrielli
2003-01-13 20:01 ` Horst von Brand
2003-01-12 23:16 ` Oliver Neukum
2003-01-13 2:00 ` Ryan Anderson
2003-01-13 3:09 ` Rob Wilkens
2003-01-14 2:13 ` Rik van Riel
2003-01-12 22:58 ` Robert Love
2003-01-12 22:28 ` Matti Aarnio
2003-01-13 1:26 ` David Lang
2003-01-13 2:00 ` William Lee Irwin III
2003-01-13 11:08 ` How to avoid the woord 'goto' (was Re: any chance of 2.6.0-test*?) Bernd Petrovitsch
2003-01-13 14:56 ` Rob Wilkens
2003-01-13 22:02 ` any chance of 2.6.0-test*? Val Henson
2003-01-13 10:32 ` gotos in kernel [Was: Re: any chance of 2.6.0-test*?] Horst von Brand
2003-01-13 14:53 ` Rob Wilkens
2003-01-13 13:08 ` any chance of 2.6.0-test*? Dave Jones
2003-01-13 13:40 ` Andrew Walrond
2003-01-13 13:49 ` Oliver Neukum
2003-01-14 6:52 ` Bruce Harada
2003-01-14 7:12 ` Brian Tinsley
2003-01-13 15:23 ` Richard B. Johnson
2003-01-13 6:23 ` Greg KH
2003-01-13 16:35 ` Linus Torvalds
2003-01-13 18:33 ` Alan Cox
2003-01-13 18:01 ` Linus Torvalds
2003-01-13 19:17 ` Andrew McGregor
2003-01-12 17:37 ` Russell King
2003-01-13 1:08 ` Michael Kingsbury
2003-01-13 1:52 ` William Lee Irwin III
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=20030112224829.GA29534@alpha.home.local \
--to=willy@w.ods.org \
--cc=alan@lxorguk.ukuu.org.uk \
--cc=greg@kroah.com \
--cc=hch@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=robw@optonline.net \
--cc=torvalds@transmeta.com \
--cc=wli@holomorphy.com \
/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
all inboxes | Powered by JetHome®