GenExpr Question regarding Behavior of History Objects in Functions

Sym's icon

Hello, in this example patch here - would the behaviour of both Codeboxes using History (within and outisde function) be the same?

Max Patch
Copy patch and select New From Clipboard in Max.

Both work correctly in the Debug Patch but I noticed that if I encapsulate larger patches in a function and call it, calling the function uses up more CPU than just calling the code within it directly in Codebox. I wonder if it has something to do with definitions inside the function body?

Graham Wakefield's icon

The definitions inside the function aren't really the issue; but the *size* of a function body might be.

To explain why: the code generator turns GenExpr functions into *inline* functions in C++. In theory, inline functions are effectively inserted within the same body of instructions from where the function was called (a bit like a macro but in machine code, so to speak). However, if a function is too large (too many instructions), it can become less efficient. The C++ compiler (Clang) makes the decision on whether a function should be inlined or not. If it is not inlined, then the function is exported as a C++ function and called in the same way it looks in the GenExpr.

It might be that your function is just large enough to trigger Clang's heuristics, to prevent inlining, but perhaps this inadvertently makes the code less efficient overall?

Sym's icon

Thank you very much Graham, I'm quite new to GenExpr after using Gen via Patching for a while so this is very helpful to understand.