ursina_tutorials icon indicating copy to clipboard operation
ursina_tutorials copied to clipboard

meshcraft - minor perf improvement for directional checks, code simplification

Open lessw2020 opened this issue 2 years ago • 1 comments

Hi again - thanks for continuing to expand the meshcraft world and series!
I wanted to show a minor perf improvement that also simplifies the code a bit. It's not necessary to update, but wanted to at least highlight it as an option for the future.

For checking in all six directions, we don't need to create 6 instances of the Vec3 class every time we invoke functions that need to check all surrounding spaces (genWalls, gapShell, etc).

Rather - we can make a global constant of tuples for the required directions, and then simply use that and avoid all the memory/processing of the current loops which are instantiating a lot of Vec3 objects and then destroying them over and over.

Let me show with some examples: global constant declaration for single file use six_axis_header (*technically people like to make global constants ALL_CAPS but I find it annoying to read...)

with the global, then refactor the genWalls from current: genWalls_curr

to this: genWalls2

Now we are no longer instantiating 6 Vec3 objects over and over every time we check (I'm thinking in part of the blister mining where we'll be doing a lot of unnecessary overhead with Vec3 creation as one blazes through the sub-terrain). In some basic testing, the loop is now 20-50% faster though the absolute benefit is in microseconds...but that's measured on a fast server and regular laptops may see more absolute improvement.

The same refactor can be done for other functions ala gapShell:

gapShell_curr

can become: gapShell_faster

For a full refactor, the optimal method would be to make a new file called config.py or cfg.py and insert the sixAxis there. Then each file needing it imports it ala 'from config import sixAxis' This way there's a single source if any changes needed to sixAxis in the future and makes it simpler to access as new functions are added that need to check in all directions.
The same can be done with fourAxis options for other direction checks. This config approach provides single source change point, some code simplification, and some perf improvement as all these extra instantiations can add up as the meshcraft world expands.

Continuing to enjoy the meshcraft series and thanks again for such a fun project!

lessw2020 avatar Jan 20 '22 19:01 lessw2020

Brilliant. Thanks so much, Less.

Really good of you to provide such detail too.

RedHenDev avatar Jan 24 '22 10:01 RedHenDev