From ffd2e8ea9d83c4295d30a423484338d6a69de6d5 Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Mon, 7 Jul 2014 02:00:15 +0100 Subject: [PATCH] Sped up ActorMap.ActorsInBox. - By ensuring both the add and remove actor lists are sets, we ensure the partitioning bins will contain only distinct actors. We can remove the HashSet and Distinct in ActorsInBox and ActorsInWorld which provides a nice speedup for queries. ActorsInBox sees nearly a 3x speedup in the RA shellmap. --- OpenRA.Game/Traits/World/ActorMap.cs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/OpenRA.Game/Traits/World/ActorMap.cs b/OpenRA.Game/Traits/World/ActorMap.cs index fd28f527c2..b31f331077 100644 --- a/OpenRA.Game/Traits/World/ActorMap.cs +++ b/OpenRA.Game/Traits/World/ActorMap.cs @@ -1,6 +1,6 @@ #region Copyright & License Information /* - * Copyright 2007-2013 The OpenRA Developers (see AUTHORS) + * Copyright 2007-2014 The OpenRA Developers (see AUTHORS) * This file is part of OpenRA, which is free software. It is made * available to you under the terms of the GNU General Public License * as published by the Free Software Foundation. For more information, @@ -43,12 +43,12 @@ namespace OpenRA.Traits readonly Map map; readonly CellLayer influence; - List[] actors; - int rows, cols; + readonly List[] actors; + readonly int rows, cols; // Position updates are done in one pass // to ensure consistency during a tick - readonly List addActorPosition = new List(); + readonly HashSet addActorPosition = new HashSet(); readonly HashSet removeActorPosition = new HashSet(); readonly Predicate actorShouldBeRemoved; @@ -169,7 +169,7 @@ namespace OpenRA.Traits public void AddPosition(Actor a, IOccupySpace ios) { - addActorPosition.Add(a); + UpdatePosition(a, ios); } public void RemovePosition(Actor a, IOccupySpace ios) @@ -180,7 +180,7 @@ namespace OpenRA.Traits public void UpdatePosition(Actor a, IOccupySpace ios) { RemovePosition(a, ios); - AddPosition(a, ios); + addActorPosition.Add(a); } public IEnumerable ActorsInBox(WPos a, WPos b) @@ -194,14 +194,13 @@ namespace OpenRA.Traits var j1 = (top / info.BinSize).Clamp(0, rows - 1); var j2 = (bottom / info.BinSize).Clamp(0, rows - 1); - var actorsChecked = new HashSet(); for (var j = j1; j <= j2; j++) { for (var i = i1; i <= i2; i++) { foreach (var actor in actors[j * cols + i]) { - if (actor.IsInWorld && actorsChecked.Add(actor)) + if (actor.IsInWorld) { var c = actor.CenterPosition; if (left <= c.X && c.X <= right && top <= c.Y && c.Y <= bottom) @@ -214,8 +213,7 @@ namespace OpenRA.Traits public IEnumerable ActorsInWorld() { - return actors.SelectMany(a => a.Where(b => b.IsInWorld)) - .Distinct(); + return actors.SelectMany(bin => bin.Where(actor => actor.IsInWorld)); } } }