symbol vs string?

VincentC's icon

Hello,

I'm at the point of refactoring an extensive patch because of a new controller device, but with the new data types, I'd like to understand the difference in using symbols or strings better. I work a lot with dictionaries, and I'm often renaming dict objects for which I create the proper name with sprintf or combines (see example two most left) to get the proper name for the dict I need. But now I'm wondering if memory usage would improve significantly if I would replace them with strings? There are about 400 places where this is used all over the patch, often switching the name of the dict objects while running it.

Thanks!

Max Patch
Copy patch and select New From Clipboard in Max.
Jeremy's icon

Strings have a few advantages compared to symbols: their memory can be reclaimed when they are no longer in use; using strings from dictionaries read from disk doesn't involve creating a symbolic copy (dicts use strings internally when read in), and they don't have an arbitrary length limit.

The memory overhead a symbol is not very significant -- it's the size of the string pointer + a single void* "thing". A string uses more memory than that for memory and thread-safety state maintenance.

If you're building an installation that needs to run unattended for 12 years, and part of that installation involves generating text, strings might be a good way to avoid slowly growing Max's heap usage until the system runs out of memory and begins to choke at some point.

As for this specific usage: the dict object's @name is a symbol, so even if you were to change your name-generation code to use strings, it'll be converted to a symbol at the end and you've gained nothing.

Over time, we'll update more objects for string-awareness (objects which display text, for instance), although I would imagine that stuff like names will stay in the symbol domain (because symbols also have some advantages compared to strings...).

Anyway, I think you can safely avoid the hassle and leave your name-generation code as it is.

Shakeeb Alireza's icon

Hi @JEREMY

Is there any plan to provide Max c-api supports to strings (and arrays)?

Shakeeb Alireza's icon

Please ignore my question. I have now seen your response here .

Thanks for the clarification!

Jeremy's icon

Check out https://cycling74.com/forums/accessing-and-processing-arrays, there is some preliminary information on that thread.

VincentC's icon

@Jeremy: thanks a lot, that was the kind of answer I needed!