Structs, Classes and Cache Efficiency

About Monkey 2 Forums Monkey 2 Projects Structs, Classes and Cache Efficiency

This topic contains 2 replies, has 1 voice, and was last updated by  arpie 2 years, 4 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #5449

    arpie
    Participant

    I recently bought this book : http://gameprogrammingpatterns.com/ and was fascinated by this chapter in particular: http://gameprogrammingpatterns.com/data-locality.html

    TLDR: improving data locality can improve cache efficiency and can have enormous effects on speed of execution.

    I wondered if I could find a significant difference between using arrays of Structs vs using arrays of Class objects in Monkey2 as Class object arrays are (afaik) arrays of pointers, wheras arrays of Structs ar stored in contiguous memory.  Once I got it right, the results were surprising.

    It seems to be that Structs are a lot faster (up to 20x faster!) than Class objects for large numbers (thousands) of entities, and coding to optimise data locality is potentially worth considering… but this is a dark art and we all know not to waste time prematurely optimising code.

    I would be delighted if we ever get a reasonably safe way to use pointers into arrays of Structs but as far as I understand this is difficult to achieve in Monkey2 (using the undocumented (?) varptr and ptr).

    Your comments, results on other hardware, critiques of my code, etc. will be very welcome.

    This is the code I ended up with (NOTE: You must run it in release mode to get accurate results).  Hopefully the comments in the code will make it clear what is happening :

     

    And the results on my i5-powered laptop are :

    #5450

    arpie
    Participant

    And on my Sony Xperia D5803 running Android 6.0.1 the results aren’t too shabby either :

    #5453

    arpie
    Participant

    Finally, here are some results on a 2-year-old lenovo yoga B8000 android tablet. Not sure why the last line is missing.

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

You must be logged in to reply to this topic.