electro-smith / DaisySP

A Powerful DSP Library in C++
https://www.electro-smith.com/daisy
Other
882 stars 139 forks source link

Style guide suggests slow code passing by const & #151

Closed claudiocabral closed 3 years ago

claudiocabral commented 3 years ago

Passing arguments that fit on a register by reference instead of by value produces slower code, as it requires an extra unnecessary load. Here's a godbolt exemplifying this: https://godbolt.org/z/TbYxvxbea (you can see the extra ldr instruction on line 9) References should only be prefered for data types that don't fit in registers or have expensive constructors associated with them.

stephenhensley commented 3 years ago

Sorry for the slow response on this. This is a great point, and I totally agree. There are a few other annoying side-effects of this as well (i.e. the argument requiring an address means it you can't foo.Set(1.5f);.

I'm going to make an update that simply removes that section from the style guide. I think it'll be worth revisiting the entire style guide soon anyway as it's been over a year since it was written during the conversion from the original C library to C++.

Thanks for the info, and the evidence. I hadn't seen godbolt before, which looks like an awesome resource.