Monday, September 4, 2017

performance - Why is chunk size often a power of two?


There are many Minecraft clones out there and I am working on my own implementation. A principle of terrain rendering is tiling the whole world in fixed size chunks to reduce the effort of localized changes.


In Minecraft the chunk size is 16 x 16 x 256 as far as I now. And in clones I also always saw chunk sizes of a power of the number 2.



Is there any reason for that, maybe performance or memory related? I know that powers of 2 play a special role in binary computers but what has that to do with the chunk size?



Answer



This would depend on the game and the indexing structure used for the chunks. Though, at such a high level, it's not too likely it has much to do with memory or a specific performance enhancement. More than likely it's an arbitrary decision for sizing chunks in a predictable way. It allows for some counting and indexing tricks using bit shifting that wouldn't be possible with numbers that aren't a power of two.


For example, counting in powers of two is as easy as shifting a bit in binary:


Dec =  Bin
1 = 000001
2 = 000010
4 = 000100
8 = 001000
16 = 010000

32 = 100000

Where these shortcuts will be used will depend on the developer and what problem they're trying to solve.


If you're making a decision for what size to make the chunks, and it doesn't matter in any other aspects, you may as well use something that is familiar and has benefits you're used to using.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...