Every object ends up as a C++ Struct?

About Monkey 2 Forums Monkey 2 Development Every object ends up as a C++ Struct?

This topic contains 8 replies, has 5 voices, and was last updated by  ImmutableOctet 2 years, 10 months ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #1018

    therevills
    Participant

    I was just looking around at the generated C++ code and noticed that even if we define an object as a class the generated object is a struct.

    [/crayon] [/crayon]

    Is this going to have performance issues since everything is on the stack and not on the heap?

    #1019

    Danilo
    Participant

    MX2 classes are always used as/with pointers in C++,
    so no struct on stack. And the pointer is added to the Garbage Collector.
    Beside that, AFAIK the only difference between struct/class in C++
    is the default access specifier (struct is all public by default).

    #1020

    therevills
    Participant

    My C++ is very rusty… I thought the one of the main differences between a struct and a class was the struct went on the stack and the class went on the heap, along with that you can compare struct by value and classes by reference.

    #1021

    ImmutableOctet
    Participant

    @therevills:

    That’s only the case in Monkey 2, which takes a similar approach to D.

    Basically, the mindset is that ‘Structs’ should be used to represent POD (Plain-old data), with some nice syntactic sugar. Although in Monkey 2’s case, it’s not pulling any punches about what you can do with a ‘Struct’. In Monkey’s case, it cares about where you use them.

    In C++, structures and classes are inherently the same, with the implication being that most (But not necessarily all) ‘structs’ will have a simplistic memory layout. Thanks to heavy usage in this manor, and long-time C compatibility, this is how they stand without added complexity, like virtual functions/methods and inheritance.

    With that said, however, the difference between classes and structures in C++ is superficial, usually an indication of intended design patterns, rather than a core difference.

    To put it simply, structs in C++ are just public by default, with no other differences. The extra meanings come from design philosophies popularized by C.

    In contrast, Monkey 2, that is, in actual MX2 code, not translator output, structures are dedicated types used for copy-based manipulation. Basically, this means they live on the stack, or more specifically, their context (Scope).

    They’re meant to be treated the same way integers, floats, and strings were in Monkey 1. Sure there’s side effects to using them; shallow copies, for example, have potential safety concerns, but they’re not all bad.

    Perhaps the most interesting part about Monkey 2’s ‘Structs’ is that they’re allocated where they stand. This means an array of structures is allocated all in one area, which is not only faster, but it also lets you map out an area of memory however you like.

    It’s not just arrays that benefit from this; you could also use ‘Structs’ inside of classes. For example, you could allocate a vector without needing to use the garbage collector. This is great because it means that an object can have its position held locally, where it can’t be easily changed by something untrusted.

    This also means that providing interfaces for APIs is easier, and there’s no potential overhead from constantly looking around in memory.

    Not to mention that operations on the structure become a lot more simplistic, allowing you to make as many objects as you want, without ever needing to worry about your memory footprint.

     

    There’s more to be said about structures and how they’re used with regards to things like ABI compatibility and expected layouts, but that’s a topic for another day.

    #1022

    dawlane
    Participant

    Both structs and class can be allocated to the heap. There’s very little difference between a struct and a class. My C/C++ is rusty, but if I remember the other difference apart from public/private, is to do with template<>. You could do template<class T>, but not template<struct T>.

    #1025

    therevills
    Participant

    @immutableoctet

    Thanks for the detailed explanation.

    Coming from C#, I’m more use this this explanation of when to use Struts vs Classes:

    https://msdn.microsoft.com/en-us/library/ms229017(v=vs.110).aspx

    @dawlane

    On this page http://monkey2.monkey-x.com/language-reference/

    Mark states:

    Structs are very cheap to create as they are allocated ‘on the stack’ which is effectively a free operation and has no impact on garbage collection.

     

     

    #1026

    dawlane
    Participant

    @therevills: What Mark has said relates to Monkey2 Structs, not C/C++ structs. The mx2cc compiler from what I can see, outputs Monkey2 Classes as a C/C++ struct. But when you initialise that Monkey2 Class for use, it gets allocated to the memory heap via GC. Monkey2 Structs gets put onto the memory stack.

    Basically both Monkey2 Classes and Structs become C/C++ structs. What gets allocated to the heap depends on whether is a Monkey2 Class or not. Which I think was the answer you was looking for with your topic title.

    In any computer language you should never go mad and put everything on the memory stack. The stack has a size limit and exceeding this limit will crash the application or if your unlucky the Operating System as well.

    #1187

    Gerry Quinn
    Participant

    As several people have noticed, there’s no difference in C++ apart from the default access specifier, and apparently a template thing I didn’t know about.  When I coded in C++ I never used the struct keyword at all, on the basis that it was redundant – a hangover in the interests of C compatibility.  (This was long before C# made structs fashionable, in an attempt to pull back from the restrictions of Java.)

    However, I think that in the interests of nice target code, they should probably use whichever was originally set.  And if one is to be used always, I think it should be class, even if that means a bunch of redundant ‘public’ keywords too.

    #1188

    ImmutableOctet
    Participant

    @Gerry Quinn and dawlane: The template issue isn’t actually a thing. For regular usage of C++’s templates, ‘typename’ and ‘class’ mean the same thing.

    I don’t believe ‘struct’ is even a thing with templates, you just write ‘typename’ or ‘class’ and it works with anything. Trying to use ‘struct’ with C++ templates in GCC results in compilation error while ‘class’ works fine.

    As for Monkey 2’s code generation, it literally doesn’t matter. Well, unless you absolutely want consistent or good looking compiler output. At that point nobody would be happy anyway.

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.