Changed to use Span<byte> and stackalloc for improved performance and memory management in `RcLayers.BuildHeightfieldLayers()`

This commit is contained in:
ikpil 2024-06-27 14:04:30 +09:00
parent 27751522f9
commit ba6815769a
2 changed files with 13 additions and 14 deletions

View File

@ -18,7 +18,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Changed `reg`, `area` arrays to byte arrays for uniformity and efficiency in `DtTileCacheContour` - Changed `reg`, `area` arrays to byte arrays for uniformity and efficiency in `DtTileCacheContour`
- Changed `RcChunkyTriMesh` to separate the function and variable. - Changed `RcChunkyTriMesh` to separate the function and variable.
- Changed to consolidate vector-related functions into one place. - Changed to consolidate vector-related functions into one place.
- Changed stack handling from List to a fixed-size array with manual index management for optimization in `RcLayers.BuildHeightfieldLayers` - Changed stack handling from List to a fixed-size array with manual index management for optimization in `RcLayers.BuildHeightfieldLayers()`
- Changed to use Span<byte> and stackalloc for improved performance and memory management in `RcLayers.BuildHeightfieldLayers()`
### Removed ### Removed
- Removed RcMeshDetails.VdistSq2(float[], float[]) - Removed RcMeshDetails.VdistSq2(float[], float[])

View File

@ -25,7 +25,6 @@ using DotRecast.Core.Numerics;
namespace DotRecast.Recast namespace DotRecast.Recast
{ {
using static RcRecast; using static RcRecast;
public static class RcLayers public static class RcLayers
@ -61,7 +60,6 @@ namespace DotRecast.Recast
/// @name Layer, Contour, Polymesh, and Detail Mesh Functions /// @name Layer, Contour, Polymesh, and Detail Mesh Functions
/// @see rcHeightfieldLayer, rcContourSet, rcPolyMesh, rcPolyMeshDetail /// @see rcHeightfieldLayer, rcContourSet, rcPolyMesh, rcPolyMeshDetail
/// @{ /// @{
/// Builds a layer set from the specified compact heightfield. /// Builds a layer set from the specified compact heightfield.
/// @ingroup recast /// @ingroup recast
/// @param[in,out] ctx The build context to use during the operation. /// @param[in,out] ctx The build context to use during the operation.
@ -80,8 +78,8 @@ namespace DotRecast.Recast
int w = chf.width; int w = chf.width;
int h = chf.height; int h = chf.height;
byte[] srcReg = new byte[chf.spanCount]; Span<byte> srcReg = stackalloc byte[chf.spanCount];
Array.Fill(srcReg, (byte)0xFF); srcReg.Fill(0xFF);
int nsweeps = chf.width; int nsweeps = chf.width;
RcLayerSweepSpan[] sweeps = new RcLayerSweepSpan[nsweeps]; RcLayerSweepSpan[] sweeps = new RcLayerSweepSpan[nsweeps];
@ -91,14 +89,14 @@ namespace DotRecast.Recast
} }
// Partition walkable area into monotone regions. // Partition walkable area into monotone regions.
int[] prevCount = new int[256]; Span<int> prevCount = stackalloc int[256];
byte regId = 0; byte regId = 0;
// Sweep one line at a time. // Sweep one line at a time.
for (int y = borderSize; y < h - borderSize; ++y) for (int y = borderSize; y < h - borderSize; ++y)
{ {
// Collect spans from this row. // Collect spans from this row.
Array.Fill(prevCount, 0); prevCount.Fill(0);
byte sweepId = 0; byte sweepId = 0;
for (int x = borderSize; x < w - borderSize; ++x) for (int x = borderSize; x < w - borderSize; ++x)