• Advertisement
Sign in to follow this  

Unity Data Oriented Design Question

This topic is 2599 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was looking through an old thread on DOD and I came across this post by Hodgman: http://www.gamedev.net/community/forums/topic.asp?topic_id=575076&whichpage=2�

Looking at his code, I see this:


ParticleLifeData& life = lifeData[i];
ParticleLocation& pos = location[i];



and


ParticleLocation& pos = location[i];
ParticleMaterial& mat = material[i];



In both instances, each lookup into lifeData, location, or material will cause a catch miss, won't it? Assuming a 64 kb cache line, if the index into lifeData causes a cache miss, then lifeData[i], lifeData[i + 1], lifeData[i + 2], lifeData[i + 3], lifeData[i + 4], lifeData[i + 5], lifeData[i + 6], lifeData[i + 7] will be loaded into a cache line. But when locations is indexed, won't that cause another cache miss since the cache was either just filled or was all ready loaded with some block of lifeData? If this is the case, would it make more sense to pair either lifeData and location into one structure, or pair location and material into one structure so that the cache miss can be eliminated in one of the cases? Or, is there no cache miss because lifeData, location and material will each be loaded into separate cache lines? If this is the case, then how does the CPU know which cache line should be used when a cache miss is incurred (ie say material[i] causes a cache miss, how does the cpu know not to use the cache line that location[i] is loaded into; is it able to tell which cache line contains the "oldest" memory [ie, last touched]?)?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by bronxbomber92
is there no cache miss because lifeData, location and material will each be loaded into separate cache lines?
Yes (hopefully, most of the time - see associativity below).
Quote:
But when locations is indexed [after indexing lifeData], won't that cause another cache miss since the cache was either just filled or was all ready loaded with some block of lifeData?
No, the cache is made up of many lines. Hopefully each array will end up in different parts of the cache.
Quote:
In both instances, each lookup into lifeData, location, or material will cause a catch miss, won't it? Assuming a 64 kb cache line, if the index into lifeData causes a cache miss, then lifeData[i], lifeData[i + 1], lifeData[i + 2], lifeData[i + 3], lifeData[i + 4], lifeData[i + 5], lifeData[i + 6], lifeData[i + 7] will be loaded into a cache line.
If [i+0] to [i+7] are all loaded into a cache line, then you definitely won't get a cache miss when accessing those 8 elements. You might get a cache-miss when accessing [i+8], however, the CPU has a prediction unit which will hopefully realise that you're traversing an array, and it will also fetch the next cache line from RAM in advance!
If not, you can issue your own prefetch instructions to do this (grab cache lines that you'll need soon, to avoid cache misses) - although, it's hard to do this right without the help of an expensive profiler ;)
Quote:
If this is the case, would it make more sense to pair either lifeData and location into one structure, or pair location and material into one structure so that the cache miss can be eliminated in one of the cases?
The reason that the particle data was broken into 3 structures in that thread (Life, Location and Material) was because different algorithms used different sub-sets of the total data.

Update only needed Life and Location data, while Render only needed Location and Material data.

So, by breaking it up into 3 structures, we're preventing Update from loading Material data into the cache for no reason, and preventing Render from loading Life data into the cache for no reason.
Quote:
how does the CPU know which cache line should be used when a cache miss is incurred (ie say material[i] causes a cache miss, how does the cpu know not to use the cache line that location[i] is loaded into; is it able to tell which cache line contains the "oldest" memory [ie, last touched]?)?
Different CPUs use different algorithms. The way that a large amount of RAM is mapped into a small amount of cache is called cache associativity.

Share this post


Link to post
Share on other sites
Thank you Hodgman! For some reason I was under the impression that there was only one cache line. The design of the program definitely makes much more sense knowing each array will most likely end up in different cache lines.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Now

  • Advertisement
  • Similar Content

    • By Atwo Studios
       
      Hey guys,

      Anthony here from Atwo Studios bringing you some new updates for the new year!
      In this video I go over our game ROY, the new games and some general updates to the company!

      If you have not checked out ROY feel free to give it a try! Many people have said they enjoyed the game thus far!
      ROY: https://goo.gl/o6JJ5P
       
    • By Affgoo
      https://play.google.com/store/apps/details?id=com.NE.Alien
      still a lot of work to do, but its pretty stable  please let me know what you think <3
      Atlas Sentry is a game of destroy everything. Using your turret, simply swivel and shoot your way to victory, upgrading your weapons to unleash destruction on the variety of spaceships. The bigger your combo’s the more score you get! Earn silver as you play and then purchase new weapons and abilities to better deal with your enemy. Different enemies use different tactics and weapons, work out your own priorities in their destruction order. 

      Features: 
      **2 different game modes 
      **A level select mode with 20 difficult levels including a final boss, can you defeat it? **Arcade mode of endless destruction, how long will you last? 
      **High scores to compete against others, see who can take the top spot. 
       
    • By Chamferbox
      Chamferbox, a mini game asset store has just opened with some nice game assets, 
      Here you can find a free greek statue asset 

      Also check their dragon, zombie dragon and scorpion monster out:



      They're running the Grand Opening Sale, it's 30% off for all items, but for gamedev member, you can use this coupon code:
      GRANDOPEN
      to get 50% off prices What are you waiting for, go to
      http://chamferbox.com
      and get those models now!

      View full story
    • By Dafu
      FES Retro Game Framework is now available on the Unity Asset Store for your kind consideration!
      FES was born when I set out to start a retro pixel game project. I was looking around for an engine to try next. I tried a number of things, from GameMaker, to Fantasy Consoles, to MonoGame and Godot and then ended up back at Unity. Unity is just unbeatable in it's cross-platform support, and ease of deployment, but it sure as heck gets in the way of proper retro pixel games!
      So I poured over the Unity pipeline and found the lowest levels I could tie into and bring up a new retro game engine inside of Unity, but with a completely different source-code-only, classic game-loop retro blitting and bleeping API. Months of polishing and tweaking later I ended up with FES.
      Some FES features:
      Pixel perfect rendering RGB and Indexed color mode, with palette swapping support Primitive shape rendering, lines, rectangles, ellipses, pixels Multi-layered tilemaps with TMX file support Offscreen rendering Text rendering, with text alignment, overflow settings, and custom pixel font support Clipping Sound and Music APIs Simplified Input handling Wide pixel support (think Atari 2600) Post processing and transition effects, such as scanlines, screen wipes, screen shake, fade, pixelate and more Deploy to all Unity supported platforms I've put in lots of hours into a very detail documentation, you can flip through it here to get an better glimpse at the features and general overview: http://www.pixeltrollgames.com/fes/docs/index.html
      FES is carefully designed and well optimized (see live stress test demo below). Internally it uses batching, it chunks tilemaps, is careful about memory allocations, and tries to be smart about any heavy operations.
      Please have a quick look at the screenshots and live demos below and let me know what you think! I'd love to hear some opinions, feedback and questions!
      I hope I've tickled your retro feels!



      More images at: https://imgur.com/a/LFMAc
      Live demo feature reel: https://simmer.io/@Dafu/fes
      Live blitting stress test: https://simmer.io/@Dafu/fes-drawstress
      Unity Asset Store: https://www.assetstore.unity3d.com/#!/content/102064

      View full story
    • By DevdogUnity

      Ho ho ho
      Tis the season of Christmas surprises, and we have a awesome one for you! 🎅  
      Sponsored by all your favorite Unity Asset Store developers, Nordic Game Jam, Pocket Gamer Connects, and co-hosted by Game Analytics, we (Joris and I – Devdog) are launching the second edition of our yearly Christmas Giveaway Calendar for all Unity game developers!
      You can already now sign up right here.
       
      So what’s this all about?
      For the past weeks, we’ve been collecting sponsored gifts related to Unity (asset vouchers, product keys, conference tickets etc.), and throughout each day of December leading up to Christmas Day on the 25th, we will be sending out these sponsored gifts as early gamedev Christmas presents via e-mail to hundreds of lucky winners.
      The total prize pool is at $35,000, with over 1200 presents donated by the awesome sponsors!
       
      Merry Christmas from Devdog, Game Analytics, and every single one of the sponsors.

      View full story
  • Advertisement