[Devlog] – Learning optimization – 30/06/2018

Before continuing the Voxel generation, i had to know where to go and how i can rewrite it from scratch using optimized solutions.

I made a list of little things i’ve found that makes the generation faster and better. It’s was not easy at first but i manage to make it work and it became perfectly clear.

Here are the little things i did :

  • Avoid Monobehaviours and classes for values. They are object-oriented and useless for this. If you need a script only for a value/variables, use a struct. It is value-oriented.
  • You know you will store small values in your ints. Maybe not higher than 5. Then use a byte! It goes from 0 to 255. It is better than a basic int which goes to 2 billion. And it’s signed!
    Here is a list of them :

    //Signed int = negative + positive / Unsigned (uint) = positive only
    //Can store Decimal, Hex, Binary values

    – byte // »0″ to « 255 » (8bits int)
    – sbyte // »-128″ to « 127 » (8bits int)
    – int // »-2,147,483,648″ to « 2,147,483,647 » (32bits)
    – uint // »0″ to « 4 294 967 295″ (2*int, positive only / 32bits int)
    – ulong // »0 » to « 18 446 744 073 709 551 615″ (64bits int)
    – ushort // »0 » to « 65 535″ (16bits int)
    – long // »-9 223 372 036 854 775 808″ to « 9 223 372 036 854 775 807 » (64bits int)

  • Threads are awesome! You can do something out of the game. Imagine you are moving the player, moving mobs, changing the weather, testing ticks in chunks, having a redstone contraption,… How will you generate chunks over it? Use threads. Chunks generation will be done outside.
  • My new favorite type of variable is called « Vector3Int ». It is just a Vector3 storing ints instead of floats. In a voxel terrain, it helps a lot! It avoids converting everything.
  • Use little (but logic) lines when you can. If you know that your actual chunk or block is not visible before having to generate it, just stop there and go to the next! It avoids more loops and more testing for nothing.
  • Just like bytes, try to calculate the finale size of your arrays before initializing them. It will make array iteration faster.
  • Still in your arrays, use 1 Dimensional arrays (x size * y size * z size). It is REALLY faster to iterate in nested ‘for’ loops. The only problem is getting a value inside. Just keep in mind that in a triple loop (x contains y contains z), The last loop will move first and go up (Z, Y, X).
    – Here, Z values are just from ‘0′ to ‘size.z‘.
    – Y values are from ‘size.z‘ to ‘size.z * size.y‘ (Your actual value is « index – size.z »).
    – And X values are from ‘size.z * size.y‘ to ‘size.z * size.y * size.x‘ (Your value is « index + size.y * size.z »).
    Also, remember that when you are looping in a 1D array, you must restart your index count at 0 (before looping) and apply « index++ » at each loop to go to the next array index. The nested loop doesn’t give X,Y,Z position anymore but just the number of iterations.

I love learning new things and new optimizations. Just remember that each of these, even it they are small, does a lot in your code! They really improve everything.

Laisser un commentaire

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur la façon dont les données de vos commentaires sont traitées.