Question About Xojo Structures

I’m trying to decide whether to use a class or a structure to represent runtime values in my bytecode interpreter I’m writing in Xojo. I’ve read the language reference about structures (link). Are there any performance or memory improvements in using a structure over a class?

None that I know of
I could sure find out though as I still mail Aaron and Joe from time to time

If you don’t mind - I’m just curious if there’s a use case outside of C API calls.

Not really
That really is their primary intended usage case
Same was true for PString CString & WString

But I’ll ask
At best there might be a tiny speed improvement since getting a member value is an offset calc & read instead of possibly a method call (in the case of a computed property)

But you’d could test that

IIRC yes at least in terms of memory usage… A class has more RAM overhead


Instances will have a vtable for virtual dispatch of methods

You’d really have to have a LOT of objects for that to be a deciding point to use a structure instead

In Ruby or Dart, any values are Objects. This opens some nasty constructs like

10.times {|| puts “rick”}


“rick”.each_char {|c| puts c}


Not sure how this fits in this thread ?

Adds more content for Garry think about it?

I guess I dont see how but …

I have worked with languages that do the things you suggest
Sometimes its a bit weird where you can alter how addition of integers works because you can override the addition operator for integers (yes … its very weird)

Being able to use literals in the fashion you suggest would be handy in a fair number of places
I’ve submitted a few features requests to make it possible to use a literal as if it was a String but so far no luck

Not suggesting, showing real Ruby code. “Literals” are translated to their classes of objects and gain a lot of very useful methods. Functions also are objects, so you can pass “functions” (blocks of referenced code) around and such code knows its sender, parameters, etc. Many interesting constructs.

But thats not what Garry was asking about at all ?
Thats a language design issue
But not what he asked
He literally just wanted to knw if Xojo Structures presented any performance or memory issues when compared to Xojo classes

Hence why I dont get how the ruby code has any bearing on that ?

I thought he was trying to decide which way he would implement in his (i don’t know) compiler/interpreter, in doubt of whether moving “values” (structures) or objects (references of instantiated classes) around. Sometimes there’s a trade, in favor of one or another.

I’m trying to decide whether to use a class or a structure to represent runtime values in my bytecode interpreter I’m writing in Xojo.

not so much how to design the language etc

not that language design isnt an interesting subject - just not the thing Garry asked

Fast reading ignoring important parts, but I think he MAYBE will mix those things, not because speed, but necessity. Some objects, when encoding, probably will need to be translated to some structures and bytes from it streamed to the output, and the reverse during the runtime.

What got me thinking about structures in the first place was trying to minimise internally what I use to represent objects in my own language (which is being implemented in Xojo). As @npalardy suggested to @Karen, using a structure might use less memory (as you’d save a few internally members on not using a class in the C++ framework).

What I’m really looking for is a way to internally represent a “value” object in my language. Since it’s dynamically typed, an object could be nothing, a number, a boolean, a string or an object. The most naive implementation would be:

Class Value
  Property Type As RuntimeType
  Private Property NumberValue As Double // use if it's a number
  Private Property StringValue As String // use if it's a String
  Private Property BoolValue As Boolean // use if it's a Boolean
  Private Property Object As RuntimeClass // if it's a class
End Class

// `i` is a runtime integer. To get it's value (e.g. for an addition):
Var d As Double
If i.Type = RuntimeTypes.Number Then d = i.NumberValue

C has the concept of a tagged union. This allows a single value of one of a number of different types to be represented in a memory efficient manner. In combination with NaN-tagging I could in theory save having four properties in my Value class. After all, a value is a number or a string, it can’t be both at the same time so why waste memory storing an empty string property when all I need to store is a Double:

typedef struct {  
  ValueType type; 
  union {         
    bool boolean; 
    double number;
    Obj* obj;
  } as; 
} Value; 

Internally, this is what C does:

I have considered doing something like this is Xojo:

Class Value
  Property Type As RuntimeType
  Private Property mData As MemoryBlock

  Function AsNumber() As Double
    Return mData(0).DoubleValue
  End Function

  Function AsBoolean() As Boolean
    Return mData(0).BooleanValue
  End Function

  Function AsObject() As RuntimeClass
    Return mData(0).PtrValue // A pointer to a RuntimeClass
  End Function
End Class

Since @npalardy was once a Xojo engineer, I figured he might have some insight into how Xojo’s framework implements certain low level things like structures. It’s mostly out of curiosity but knowing the internal implementation can help when making design decisions when a project requires speed and memory usage be optimised.

@Rick.A The language I’m working on does function like Ruby in that you can do things like:

System.print("Hello".reverse()) # olleH

You are trying to put all the values inside of a large “container” object, that’s not the correct approach, you should create a very clean object type able to describe a basic structure (like name, type, etc) and extend it to other specialized types (class Number extends Object) and add a “value” As Double to it and have a type “Number” so you can know what type of object is this at runtime, those things.

So, YOUR String Object will have a “reverse” method, but your Number object, will not.

You can fake C “unions” in Xojo.
I know some ex-engineer that wrote a blog post about this :slight_smile:
That _might _ be useful

Xojo’s internals for structures I have to admit I never looked into so I really cant say that much