Reg - how many?!
-
Is there a limit to how many Reg entries you can have?
-
@DanH 32 + 32 / namespace IIRC
-
@Matt_SF thanks!
-
@Matt_SF ok gonna run out
What should I use as an alternative? const?
-
@DanH Nope, you need need
var
instead since a constant can't be modified later. Unless you have somereg
that are meant to be constants which would be a bad implementation.
If you run out ofreg
in different namespaces, it's probably that your design isn't optimised. So the solution would be to either better optimise your code to usereg
only when it is strictly necessary, or divide your namespaces in smaller ones, or both... -
@DanH said in Reg - how many?!:
What should I use as an alternative? const?
Restructure your code. Break it up into smaller manageable pieces with their own namespaces.
Edit: Just realised I had the same answer as Greg :)
-
-
@DanH Sounds like you're doing something crazy. You should probably be declaring
vars
in a callback rather thanreg
I'm not surereg
offers any advantage there (perhaps scope). But 32 in one timer definitely sounds like you could do some optimisation.And no reason you can't break the contents of a timer up into multiple namespaces.
-
@d-healey I'm not declaring the regs in a callback, but I am using them in the timer callback... vars might well work actually. What's the advantage of reg over var?
The reason for the amount is grabbing values from global cables out of scriptnode, no way around it as far as I can tell.
-
@DanH Vars should always be a last resort.
Read this - https://forum.hise.audio/topic/79/scripting-best-practices
And this - https://docs.hise.audio/scripting/scripting-in-hise/hise-script-coding-standards.html#variables (scroll down to the "Types" section).Why don't you use a single array to store your global cable values? Is this something you could use a broadcaster for instead of a timer?
-
@d-healey haven't used broadcasters before, could be a possibility I guess, I've been following @Christoph-Hart 's guide on how to use global cables thus far.
https://docs.hise.audio/scripting/scripting-api/globalroutingmanager/index.html
https://docs.hise.audio/scripting/scripting-api/globalcable/index.html -
@DanH yeah store them in an array.
-
@d-healey @Christoph-Hart Argh arrays!
I might make a small snippet if you fancy helping me out!
-
An array is just like lots of normal variables living in one house. So you only need to go to one address and ask for the person you want, instead of going to multiple addresses :p.
I hope that analogy works...
Let's imagine you are using
reg
variables like thisreg myGlobalCableData0; reg myGlobalCableData1; reg myGlobalCableData2; reg myGlobalCableData3; reg myGlobalCableData4; function myTimerCallback() { myGlobalCableData0 = someValue; myGlobalCableData1 = someValue; myGlobalCableData2 = someValue; myGlobalCableData3 = someValue; myGlobalCableData4 = someValue; }
You can do it with an array like this
const myGlobalCableData = []; // Arrays should pretty much always be const function myTimerCallback() { myGlobalCableData[0] = someValue; myGlobalCableData[1] = someValue; myGlobalCableData[2] = someValue; myGlobalCableData[3] = someValue; myGlobalCableData[4] = someValue; }
You might find this useful too:
https://youtu.be/Y8sraa5ig-M -
-
@iamlamprey I don't think there is any benefit to them being var or reg.
So unless they're in an inline function, in which case they should be local like everything else, const seems to be the best option, unless you can think of a reason I've missed?
Oh and of course inside a regular function they should be var because that's all we have there.
-
@d-healey If there's no performance overhead then I guess it's just good coding practice to make them const since the actual array isn't changing, just what's in it
I guess var / reg would only be used if you're pointing it to another array or something
-
@iamlamprey The engine can resolve a dot operator for a const object at compile time, so it improves the performance of function calls.
var slow = [1, 2, 3, 4, 5]; slow.indexOf(2); // this requires two table lookups to get the function to call const var fast = [1, 2, 3, 4, 5]; fast.indexOf(2); // this function call can be resolved at compile time.
-
@DanH
Aconst
array will always be an array whereas areg
could be something else later :reg myArray = [] ; reg myArray = 42; // this will work. const myArray = [] ; const myArray = 42; // this won't
If I'm not mistaken, it has something to do with memory allocation. That's why it's always a good practice to
reserve
the arrays's slots number if you know it.const myArray = [] ; myArray.reserve(4); // push 4 variables into the array later
Edit : ah, didn't see @Christoph-Hart 's answer