[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [sc-users] top five sclang pet peeves




On 25 Dec 2017, at 20:29, <brianlheim@xxxxxxxxx> <brianlheim@xxxxxxxxx> wrote:

4. Calling add on an array returns a new array instead of adding to the
existing one. I understand it has its advantages for chaining operations
etc. This, admittedly, is mostly a matter of me having to get used to the
conventions again.

I think there is a good reason for this that I can't remember right now. In absence of that reason I can't say I agree or disagree, except that it has also confused me a lot.

It’s to do with memory management. An Array is allocated with a fixed max size, which you can specify. If your add exceeds that max size add returns a new Array. If not it does return/alter the receiver.

So the usual advice is to reassign if you’re not sure if this will happen, i.e. a = a.add(2);

Help for this: Set max size appropriately if you know it in advance, or use a List, which has an Array,  and handles the reallocation automatically if needed.

I can’t comment on how worthwhile this design is these days. Possibly it was more appropriate in the early days of SC. In general though it pays to think about max size anyway though, as object allocation is one of the more expensive things to do CPU wise. Some years back I did a survey of all calls to Array:add or ++ in Common and set appropriate max sizes, and used add instead of ++ where possible. The savings were impressive in some cases.

S.