From f2d228828369d092bf17ba164039caef35fa7530 Mon Sep 17 00:00:00 2001 From: ikpil Date: Sat, 1 Jul 2023 12:24:23 +0900 Subject: [PATCH] added DtTileCacheBuildResult --- .../AbstractTileLayersBuilder.cs | 29 +++++-------------- .../DtTileCacheBuildResult.cs | 19 ++++++++++++ 2 files changed, 26 insertions(+), 22 deletions(-) create mode 100644 src/DotRecast.Detour.TileCache/DtTileCacheBuildResult.cs diff --git a/src/DotRecast.Detour.TileCache/AbstractTileLayersBuilder.cs b/src/DotRecast.Detour.TileCache/AbstractTileLayersBuilder.cs index 697e85d..112a592 100644 --- a/src/DotRecast.Detour.TileCache/AbstractTileLayersBuilder.cs +++ b/src/DotRecast.Detour.TileCache/AbstractTileLayersBuilder.cs @@ -53,39 +53,24 @@ namespace DotRecast.Detour.TileCache return layers; } + private List BuildMultiThread(RcByteOrder order, bool cCompatibility, int tw, int th, int threads) { - var tasks = new ConcurrentQueue>>>(); + var results = new List(); for (int y = 0; y < th; ++y) { for (int x = 0; x < tw; ++x) { int tx = x; int ty = y; - var task = Task.Run(() => - { - var partial = Build(tx, ty, order, cCompatibility); - return Tuple.Create(tx, ty, partial); - }); - tasks.Enqueue(task); + var task = Task.Run(() => Build(tx, ty, order, cCompatibility)); + results.Add(new DtTileCacheBuildResult(tx, ty, task)); } } - var partialResults = tasks - .Select(x => x.Result) - .ToDictionary(x => Tuple.Create(x.Item1, x.Item2), x => x.Item3); - - List layers = new List(); - for (int y = 0; y < th; ++y) - { - for (int x = 0; x < tw; ++x) - { - var key = Tuple.Create(x, y); - layers.AddRange(partialResults[key]); - } - } - - return layers; + return results + .SelectMany(x => x.task.Result) + .ToList(); } protected abstract List Build(int tx, int ty, RcByteOrder order, bool cCompatibility); diff --git a/src/DotRecast.Detour.TileCache/DtTileCacheBuildResult.cs b/src/DotRecast.Detour.TileCache/DtTileCacheBuildResult.cs new file mode 100644 index 0000000..eb90d3a --- /dev/null +++ b/src/DotRecast.Detour.TileCache/DtTileCacheBuildResult.cs @@ -0,0 +1,19 @@ +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace DotRecast.Detour.TileCache +{ + public class DtTileCacheBuildResult + { + public readonly int tx; + public readonly int ty; + public readonly Task> task; + + public DtTileCacheBuildResult(int tx, int ty, Task> task) + { + this.tx = tx; + this.ty = ty; + this.task = task; + } + } +} \ No newline at end of file