News for hackers. Stuff that matters.
Re:A fun optimization trick from rsync by matthewaveryusa on Sunday October 28, 2018 @10:16PM (#18324517)
That's not clever, that's language abuse and horrific -- You could do this in so many other ways there is absolutely no point in abusing #includes mid-source-code (without mentioning the code smell around statics.)

Now, as a working curiosity, this is pretty damn cool.

Re:A fun optimization trick from rsync by nwmcsween on Sunday October 28, 2018 @10:43PM (#18324640)
Not really its just a static var on a switch statement nothing really bad about it, in fact its a semi nice way to handle a simple state machine.
Re:A fun optimization trick from rsync by repsilat on Sunday October 28, 2018 @10:55PM (#18324705)
Not tooo uncommon either. It's a "reasonable" way to write generators in C (if you're masochistic Python programmer, I guess.)

Switch on the last return point at the top of the function, and make all variables that need to persist between calls `static`.

(I've written it before, and I swear I didn't invent it... Maybe it's just a short skip from Duff's Device? Still, using it to pick up before the last return instead of after is new trick for me!)

Re:A fun optimization trick from rsync by dimatura on Monday October 29, 2018 @07:05AM (#18326435)
Yes, it's basically the idea in Duff's device. Protothreads (http://dunkels.com/adam/pt/) uses a pretty similar trick.
Re:A fun optimization trick from rsync by repsilat on Monday October 29, 2018 @03:02PM (#18330246)
Ah, that's exactly the trick I was thinking of. I think I learned it from [1], but I'm pretty sure Protothreads predates that post. Thanks for the link!

1: https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html

Re:A fun optimization trick from rsync by fpoling on Monday October 29, 2018 @04:51AM (#18325912)
This was modeled on early Fortran where all local variables were static and it was trivial to implement persistent state machines.
Re:A fun optimization trick from rsync by geezerjay on Monday October 29, 2018 @04:14AM (#18325777)
To my eyes it also looked like a Duff's device which kept track of the successful state.
Re:A fun optimization trick from rsync by Piskvorrr on Monday October 29, 2018 @07:00AM (#18326416)
Yup. First thing that popped into my mind - with Duff's observation following shortly thereafter. Clever...too clever by half, IMNSHO.
Re:A fun optimization trick from rsync by imron on Monday October 29, 2018 @01:28AM (#18325270)
> without mentioning the code smell around statics

Or the code smell around fall through switch statements.

There are legitimate uses for fall through switch statements and/or mid-file includes, and or static function vars, but combining all of them together is not clever, it's a recipe for bugs.

Re:A fun optimization trick from rsync by ktm5j on Monday October 29, 2018 @12:06AM (#18324979)
Im with you. This is great and clever, but I would have to maintain code like this.