Xojo Compiler Optimization Question

Hello,

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.

I hope my question is not too confusing.

Thank you!

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 :wink:

@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).

I would bet, Xojo gets three strings from constants and than concats it.

What do you mean Christian?

my understanding of how constants work is basically this (and I can be corrected if I’m wrong :slight_smile: )

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

I just tried and it looks like text literals get added at compile time.

e.g.

dim s as string = “Hello” + " " + “World”

will be “Hello World” in the exe file.

[quote=423527:@Christian Schmitz]I just tried and it looks like text literals get added at compile time.

e.g.

dim s as string = “Hello” + " " + “World”

will be “Hello World” in the exe file.[/quote]

So it optimizes the use of Constants and Literals… which are just unnamed constants after a fashion

Might I ask how you observed this?

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.

you can just build an app and look into the app file.

are you saying something like this?

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.

but if your code follow the template in my post… you don’t have X “foos”… you have ONE

const xFoo="FOO"
for i=1 to 1000
  myArray.append xFoo+str(i)
next i

and

for i=1 to 1000
  myArray.append "Foo"+str(i)
next i

would compile to near identical code… I say “near” as various pointers might have different values, but otherwise the same

now if you had “FOO” in 100 distinct locations, the yes a constant would be preferred, but only for maintenance not to make the code smaller

Constants for the most part are a convience for the developer, not a optimizer for the compiler

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.