mkafrin / PolyZone

PolyZone is a FiveM mod to define zones of different shapes and test whether a point is inside or outside of the zone
MIT License
202 stars 193 forks source link

Lazy PolyZone grid creation #35

Closed mkafrin closed 3 years ago

mkafrin commented 3 years ago

Would it be possible to lazily compute the PolyZone grid optimization? In other words, instead of building the entire grid at zone creation, the grid would stay empty until a point in the zone was tested, and then the grid cell that point correlates with would be computed and cached. That way you save massively on memory if the zone is never interacted with or only interacted with at specific points, and brings the startup performance cost to nearly zero. Also this might make it worthwhile to increase gridDivisions across the board since the grid creation cost would be amortized over a much larger period of time.

After a brief look over the code, I don't readily see any barriers to doing this, though care would need to be taken for situations like when drawGrid is turned on, since when that is the case, you'd have to build the whole grid at zone creation.

Alternative: One possible alternative would be to lazily compute the entire grid upon first point in poly test, rather than lazily computing each grid cell. This would still give some of the memory and startup performance benefits, but could cause lag spikes during gameplay depending on the complexity of the zone and lose some other benefits of the prior option, like the ability to increase gridDivisions.

mkafrin commented 3 years ago

Implemented in 29f4849ae81681d94bb19816a7612b1fc4fae2ac