Duplicating source code

DSP, Plugin and Host development discussion.
RELATED
PRODUCTS

Post

What are some alternatives to writing duplicate code in the source? My specific example is plugin state initialization/reset, where I find the same block of code in 3 different member functions of my plugin.
There's always the venerable C macro
Maybe an IDE plugin that pushes changes in one block of text to different areas?
C++ templates maybe?
Scripts?
Last edited by camsr on Tue May 23, 2017 10:15 pm, edited 1 time in total.

Post

The same block three times? So this means that you probably need this state to do something similar, so probably a class that initializes the content in the constructor and 3 different methods that may share similar code paths?

Post

Hi, why don't you just put the duplicate code into a function and call it? I think this could be a default constructor, too.

Post

Guaranteed inlining...
A source level solution is what's best.

Post

Squidsneeze wrote:Hi, why don't you just put the duplicate code into a function and call it? I think this could be a default constructor, too.
It generates unnecessary instructions/indirection.

Post

Is it certain that this kind of call will inline?

this->st_init();

Post

You could use the "__forceinline" keyword, then it should (don't know if it's a MSVS only keword). Else a bit unconventional, but you could put the code into a file which gets included at the needed positions.

Post

Yeah, that's the guaranteed way of doing it! And it's very inflexible, imagine renaming a hundred constants...

Post

Squidsneeze wrote:You could use the "__forceinline" keyword, then it should (don't know if it's a MSVS only keword). Else a bit unconventional, but you could put the code into a file which gets included at the needed positions.
Yes, __forceinline is MSVC only but GCC has the equivalent inline __attribute__((always_inline)) so it's a matter of setting up a #define. Actually I'm not sure why they haven't harmonized those between MSVC and GCC/LLVM, like they did with other stuff like #pragma pack.

Post

Actually I'm not sure why they haven't harmonized those between MSVC and GCC/LLVM, like they did with other stuff like #pragma pack.
Not the worst gcc/msvc mismatch. How about this one : http://stackoverflow.com/questions/1537 ... ute-packed Was that really harmonized and I have missed it?
~stratum~

Post

I guess what I am really looking for is a way to modify source code by some pre-defined pattern. Like adding new parameters for instance, instead of moping through the source copying/pasting and editing, just be able to define everything required in one area and have it automatically propagated via some scripted pattern. It's 2017, people are even using voice to code :)

Post

We are still in the stone age where considerable time is spent by deleting "const std::string&" phrases form code that you have copied from another file :lol:
~stratum~

Post

A function is the proper solution. In combination with template meta-programming.

A template functor can be used to ensure inlining along with some compiler-specific defines. Generally these won't be needed though.

The biggest issue is that you're using constructors where indirection or function-call overhead could actually make up any significant issue! This is a very, very, very, very, very, very bad sign. I can't type very enough times to express how very bad it is.

If you're writing optimized code that executes in tight loops you shouldn't be anywhere near using the heap or calling constructors.

In every other case however function-call overhead is entirely insignificant.
Free plug-ins for Windows, MacOS and Linux. Xhip Synthesizer v8.0 and Xhip Effects Bundle v6.7.
The coder's credo: We believe our work is neither clever nor difficult; it is done because we thought it would be easy.
Work less; get more done.

Post

aciddose, I think I agree with you. It probably is insignificant, the only tradeoff is having the same set of data in 3 separate areas.

Post

Forced inline through a keyword is a bad idea. The compiler has heuritics for inlining, and you should trust them.

If your initialization is more than a few variables, even if your function is not inlined, it won't matter much. A few registers on the stack, one additional unconditional predictable jump? That's nothing compared to even one branch test.

Post Reply

Return to “DSP and Plugin Development”