I think the dream has long been that a compiler could optimize away any inefficiencies that can be mechanically described. As such, if there is a benefit to seeing the same structure in many places, using a struct would be ideal.
I don't think we are near said dream, yet.
Doesn't seem to be a problem, at least for Rust: https://godbolt.org/z/fToxz3d7a.
Functions with plain arguments and a struct both produce identical assembly output.
Also worth noting clippy will warn about writing functions with too many arguments for the same reason listed in TFA
https://rust-lang.github.io/rust-clippy/master/index.html#to...
Re:N-Params vs. Single Param
by carlos-menezes on Thursday April 17, 2025 @03:54PM
(#43721415)
C++ for reference: https://godbolt.org/z/7G6qxK6T8
Re:N-Params vs. Single Param
by hwpythonner on Thursday April 17, 2025 @04:20PM
(#43721777)
Try compiling with optimizations. I think by default this site doesn't add optimization flags.
Here what happens with optimizations: https://godbolt.org/z/G18zd7chP
Look at the registers usages vs stack
Re:N-Params vs. Single Param
by carlos-menezes on Thursday April 17, 2025 @04:25PM
(#43721818)
Got it! Thanks for the link.
Re:N-Params vs. Single Param
by physicsguy on Thursday April 17, 2025 @03:57PM
(#43721470)
If you add a third argument you get different assembly
I’ve seen some evidence it hurts performance in JS, too. If you’re doing something “a lot”, like media qstreaming, or realtime updates, ordered params are likely faster.
I agree with others that the destructuring is a nice syntax to read and write. Yet, I have started to feel it’s an overused pattern, deployed by habit rather than consideration (in my code anyway). I can’t put my finger on why. It’s possibly a gut-feel that my functions would be better taking feeer arguments.
Re:N-Params vs. Single Param
by oneoverten on Thursday April 17, 2025 @07:33PM
(#43723382)
Yes. This is true for JS too, object-wrapping parameters just to make them named will add runtime overhead.
But in native languages like C/C++/etc, this pattern usually hurts performance. Separate arguments go into registers (faster but depends on number of arguments), while structs often involve indirection, and sometimes alignment/padding issues. Also, passing a const struct doesn’t guarantee the fields inside are truly const.
That said, it's context dependent. If you're passing stuff through multiple layers or want a more stable function signature, using a struct can be cleaner. Just not something I’d generalize to any language or any use case.