The Xojo docs explicitly state that the Text data type is immutable. It doesn’t say one way or the other concerning its successor in API 2, String.
Coming from the .NET framework, I’m accustomed to strings being immutable (mainly for thread safety reasons) and to the performance tradeoffs involved. I have worked for many years on a code base that’s very heavy on text processing, and one learns that there’s considerable performance lift in avoiding string allocations like the plague in that environment.
For example, code like this in C#:
string string1 = “Foo”;
string string 2 = “Bar”;
string string1 = string2;
… represents another allocation in reassigning string1. It does not simply assign a reference to string2 to string1 or copy the existing characters into the existing instance if possible, it creates a whole new string, assigns a copy of the characters of string2 to it, and then discards the old reference to string1 for later garbage collection. This is basically syntax sugar that makes it look like no allocation is happening – yet, it is. String concatenation is also a no-no at least in principle. One uses a StringBuilder instance to construct a string from sequential bits and pieces, or one uses Spans over an array of characters and copies into that. Either way, you create a new string from the final result so that there is only one string allocation, and no extra or hidden ones.
Do these considerations apply in Xojo? I’m guessing that they do, that Memory is more or less a substitute for Span, and that one can write abstractions to construct complex strings with minimal allocations.
Yes, I understand that this is probably not a practical problem in most apps and would constitute premature optimization. I am just wanting to confirm the internal architecture so that I can keep it in mind when I have hot paths with lots of string manipulation.
Another factor is that .NET does optimize away concatenations of (I think currently) up to 4 string fragments, and perhaps Xojo does so as well. It basically does an under-the-hood assembly of a character array and does a fast in-memory copy into it and creates a new string with one allocation. Not having access to the source like I am used to (I would Google, e.g., “System.String source code” to see what it’s actually doing) I am hoping someone has written an article or blog post about this at some point.
I guess a related question (because of how much of an impact on performance object allocations are, and when) is whether Xojo uses garbage collection or reference counting internally. If anyone knows.
Thanks in advance for any info you might have!