Rendering concept

Started by
10 comments, last by demonkoryu 11 years, 7 months ago

Also, as someone else mentioned, do not store entire objects in memory within arrays, lists, collections, etc... Store only the pointer to it. Otherwise, you're duplicating (potentially) a LOT of data; plus, modifying one instance in one set of memory will not reflect the changes in another... and any type of sorting/moving operation requires a LOT of extra work for the CPU. Remember that the CPU moves data fastest when it comes in chunks that match the native size of the registers (e.g., 32-bit chunks for x86, 64-bit chunks for x64). So storing the pointers to objects in your array/list/collection can be a marked optimization, as pointers will naturally match the optimal native data size.


However you have to becareful with this kind of thinking.

Pointers are basically cache miss factories and missing the cache is one of the worst things you can do as the CPU has to stall while it goes off to fetch data from memory. Amusingly as CPUs have gotten faster that stall has gotten worse as it can take hundreds of cycles before your data has been fetched which is all time the CPU is left twiddling its thumbs.

For certain operations it is BETTER to pack memory into contiguous chunks and access them in one direction from start to end; the pre-fetcher in the CPU will hide some to all of the latency of memory fetches and you'll be basically pulling data from cache as you go. Of course you also have to pay attention to data layout but it can be worth it.

So, if I was writing a command list system for feeding a graphics API would I embed the mesh into the list? No.
But would I embed the translation matrix? Yes, I would as I'm going to want to get that and going via a pointer is just accessing memory for the sake of accessing memory.

Data layout and cache friendlyness are two of the most important concepts going; saving some bytes by using a pointer instead of a real object might seem clever but often it can result is slowing things down rather than speeding it up.

Learn how the system works and avoid "typical C++ bullshit" (google that, it's a good read) if you want to do things quickly.
Advertisement
And read Pitfalls of Object Oriented Programming, while you're at it.

This topic is closed to new replies.

Advertisement