I have a general question about compiler optimization when using constants. I have several classes that perform string additions. At hundreds of places {} brackets are added. So far I always use ... + "{" or ... + "}". Does the Xojo compiler automatically detect by some kind of pattern when these direct string variables occur and convert them internally into constants, or is it better to directly create a constant for each bracket and then call it in the code?
My understanding is that the program will show a new variable in RAM every time it encounters a direct string variable in the code, so I ask. On the other hand, my logic tells me that initializing hundreds of string constants at program start could take up a lot of RAM.
Independently of your question: strings are immutable. Don’t add lots of strings with + which is very inefficient. Do a join with an array or a regex or a template.
Dear Beatrix, I know about the use of arrays and joins and I use them too. I did not mention this, because my question is aimed at something else. Thank you anyway for the hint
@Beatrix Willius While what you said is true, using “+” 10000 times is not even twice as slow as populating a 10000-item array and using Join (tested with Xojo 2018r3).
my understanding of how constants work is basically this (and I can be corrected if I’m wrong )
If you define a simple constant, be it a number or a string, the COMPILER makes a pass thru the code and replaces all instances of the constant reference with the constant value. (or at least that is how it working back in the day)
const C1="AB"
const C2="CD"
const C3="EF"
dim s as string=C1+c2+c3
the compiler would see
dim s as string="AB"+"CD"+"EF"
But if it were a complex constant, it would be fully resolved first
const C1="AB"
const C2="CD"
const C3="EF"
const C4=c1+c2+c3
dim s as string=C4
the compiler would see
dim s as string="ABCDEF"
This would not affect RUNTIME at all, since all the resolution was done at compile time
Let me give you the scenario in more detail: Assuming I have the static word “Foo”, to which various other strings generated at runtime (e.g. “Bar”) are appended in various method calls (loop passes), then “Foo” is reinitialized each time, or does the compiler recognize the static Foo and make it a constant? Otherwise I would create a constant kFoo = “Foo”.
My aim is to speed up the methods and also to save RAM.
for i=1 to 1000
myArray.append "Foo"+str(i)
next i
That is a whole 'nother kettle of fish… those would not be “constants” since they are unknown until runtime
a constant has a static value at COMPILE time
Yes Dave, it’s the right direction. My thought also went in the following direction, that it would be more comfortable not to have x-fold “Foo” in the source code, but to manage the word “Foo” centrally as a constant. From this came the color, whether the app will benefit from it, or it alone makes my work as a developer easier.
It’s all glowing to me Dave. Thank you for your remarks. I am curious whether a member of Xojo’s staff will comment on the thread here in order to provide clarity.
Just confirming what Dave posted: if I build a test project with “Foo” in several functions and compile, “Foo” only appears once in the binary. The Xojo compiler does appear to identify matching literals and only store the string once.