Structs and Garbage Collection

About Monkey 2 Forums Monkey 2 Programming Help Structs and Garbage Collection

This topic contains 10 replies, has 4 voices, and was last updated by  codifies 2 years, 3 months ago.

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #6047

    Raudius
    Participant

    As I understand Structs (which is not very well btw), they are in essence a collection of primitives and pointers.

    So in (my) theory creating (a lot of) structs should not constitute a problem in terms of garbage collection.

    My concern is that I have entities that will require hit-testing with the ground each “Update()”. And the way I have currently implemented the hittesting it uses Rectf structs. Something like this:

    My concern is that “ToRectf” is creating a new “Rect” struct each update never to be used again, so it somehow needs to be removed (if Rects were objects then it would be an issue).

    If the struct values get interpreted as part of the call stack then that’s fine. But if it gets Garbage Collected (ie. during runtime the application has to check if there are instances of it elsewhere in the program) then that may cause unnecessary load and I’d be better off just passing the actual primitive float values to the HitTest function.

    I’m not sure if I explained the problem well. tl;dr I am mostly wondering how the Monkey-Compiler interprets Structs and whether Structs need to be garbage collected or not.

    #6064

    nerobot
    Participant

    Using search in main forums page I found the same topic with lot info.

    http://monkey2.monkey-x.com/forums/topic/structs-and-the-gc/

    #6067

    codifies
    Participant

    a wiki would be great for little nuggets like this…

    #6071

    Raudius
    Participant

    Thanks! I suppose it always pays to check the docs and forums.

    So creating a struct and passing it around instead of primitives is basically the same as both the primitives and structs are “pass by value”.

    #6072

    abakobo
    Participant

    So creating a struct and passing it around instead of primitives is basically the same as both the primitives and structs are “pass by value”.

    Yep. And using Structs is generaly a bit faster than classes as it is put on the stack and not on the heap.

    #6075

    codifies
    Participant

    err careful there, passing a reference to a class could be a lot faster than copying (passing by value) a struct, especially if you’re structs got more than a few members…

    Of course you can always pass a pointer to a struct….

    #6085

    Raudius
    Participant

    I think for my specific case I’d rather be creating structs than objects since they are a one time use thing and creating objects would drastically increase the load on the GC, right?

    I am just using “Rect” structs as a more convenient way to pass primitives (i.e. coordinates and dimensions for hit testing) and just wanted to make sure I am not making my game terribly inefficient in the process.

    #6086

    abakobo
    Participant

    In my experience the order of fastness is:
    1-primitive
    2-struct field
    3-struct property

    so avoid properties if it is not necessary

    I played along with optimisation in this example: https://github.com/abakobo/learn_monkey2/tree/master/julia
    The most optimised version I could get is not using structs because primitives where faster..
    In the struct_non_optim example you can try 3 different manners to use structs with 3 very different speeds (by uncommenting/commenting). Field being the fastest one.

    And here a little bit of code to illustrate the speed difference between struc and class with a simple object case:

    [/crayon]
    #6089

    Raudius
    Participant

    Damn you’re right parameters are much faster than structs! A lot faster!

    I even made my own test to check the speed difference for a similar implementation  to the one in my game, and primitives values are significantly faster. As I understand it this has to do with the time it takes to copy structs from one method stack to the next.

    I also found that using Getters to get the primitives also significantly affects the time it takes. I assume this is also to do with copying the (“pass-by-value”) value from the one method stack to the next as opposed to just reading it directly from the class’ memory in the heap.

    I may have to consider changing my game to just passing the primitives separately (instead of combining them inside a struct) since the performance impact is so much more than I anticipated.

    Thanks for the heads up!

    <!–more–>

    #6092

    nerobot
    Participant

    One note about optimization: if your game works at desired fps you may don’t care about optimization if that brings difficult code styling.

    #6093

    codifies
    Participant

    …and benchmarking, in the time it takes to test a few thousand calls, your PC could be doing something else, to stop doing it just in time for the next test, benchmarking is fraught potential pit falls…

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

You must be logged in to reply to this topic.