News for hackers. Stuff that matters.
Re:Don't DRY Your Code Prematurely by semi-extrinsic on Thursday May 30, 2024 @03:03PM (#40527441)
> Dennis Ritchie encouraged modularity by telling all and sundry that function calls were really, really cheap in C.

The old salty professor who taught numerical physics at my uni insisted that function calls were slow and that it was better to write everything in main. He gave all his examples in Fortran 77. This was in the 2010s...

Re:Don't DRY Your Code Prematurely by coliveira on Thursday May 30, 2024 @05:34PM (#40528965)
In fact he is right. The advantage of writing modular code, however, is that we can test the locations where performance is needed and optimize later. With a big main it becomes very hard to do anything complex.
Re:Don't DRY Your Code Prematurely by Archelaos on Thursday May 30, 2024 @06:07PM (#40529253)
This is why I liked it when the language I was coding in supported inline expansion: I could keep my code modular but nevertheless avoid the penality of function calls in performance critical functions in the compiled code.
Re:Don't DRY Your Code Prematurely by chipdart on Thursday May 30, 2024 @11:32PM (#40531212)
> In fact he is right.

Was he, though? I mean, yeah having to push and pop a call stack does indeed require more work than not having to do that. However, compilers can and do inline and optimize out function calls.

And what's the real performance impact of calling functions a constant number of times outside of the hot path? Is an untestable spaghetti salad of things better than a few hypothetical push and pops?

There's wisdom behind Knuth's remarks on premature optimization.