Merge pull request #7231 from RoosterDragon/linq-checks

Checked LINQ queries and collections for inefficiencies.
This commit is contained in:
Matthias Mailänder
2015-02-01 08:56:15 +01:00
44 changed files with 151 additions and 126 deletions

View File

@@ -37,7 +37,7 @@ namespace OpenRA.Mods.Common.Traits
{
public readonly CargoInfo Info;
readonly Actor self;
readonly List<Actor> cargo = new List<Actor>();
readonly Stack<Actor> cargo = new Stack<Actor>();
readonly HashSet<Actor> reserves = new HashSet<Actor>();
readonly Lazy<IFacing> facing;
@@ -59,7 +59,7 @@ namespace OpenRA.Mods.Common.Traits
if (init.Contains<RuntimeCargoInit>())
{
cargo = init.Get<RuntimeCargoInit, Actor[]>().ToList();
cargo = new Stack<Actor>(init.Get<RuntimeCargoInit, Actor[]>());
totalWeight = cargo.Sum(c => GetWeight(c));
}
else if (init.Contains<CargoInit>())
@@ -69,7 +69,7 @@ namespace OpenRA.Mods.Common.Traits
var unit = self.World.CreateActor(false, u.ToLowerInvariant(),
new TypeDictionary { new OwnerInit(self.Owner) });
cargo.Add(unit);
cargo.Push(unit);
}
totalWeight = cargo.Sum(c => GetWeight(c));
@@ -81,7 +81,7 @@ namespace OpenRA.Mods.Common.Traits
var unit = self.World.CreateActor(false, u.ToLowerInvariant(),
new TypeDictionary { new OwnerInit(self.Owner) });
cargo.Add(unit);
cargo.Push(unit);
}
totalWeight = cargo.Sum(c => GetWeight(c));
@@ -185,13 +185,12 @@ namespace OpenRA.Mods.Common.Traits
public bool HasSpace(int weight) { return totalWeight + reservedWeight + weight <= Info.MaxWeight; }
public bool IsEmpty(Actor self) { return cargo.Count == 0; }
public Actor Peek(Actor self) { return cargo[0]; }
public Actor Peek(Actor self) { return cargo.Peek(); }
public Actor Unload(Actor self)
{
var a = cargo[0];
var a = cargo.Pop();
cargo.RemoveAt(0);
totalWeight -= GetWeight(a);
SetPassengerFacing(a);
@@ -248,7 +247,7 @@ namespace OpenRA.Mods.Common.Traits
public void Load(Actor self, Actor a)
{
cargo.Add(a);
cargo.Push(a);
var w = GetWeight(a);
totalWeight += w;
if (reserves.Contains(a))

View File

@@ -103,8 +103,8 @@ namespace OpenRA.Mods.Common.Traits
CPos? ChooseEmptyCellNear(Actor a, string unit)
{
var possibleCells = GetSuitableCells(a.Location, unit).Where(c => !usedCells.Contains(c)).ToArray();
if (possibleCells.Length == 0)
var possibleCells = GetSuitableCells(a.Location, unit).Where(c => !usedCells.Contains(c)).ToList();
if (possibleCells.Count == 0)
return null;
return possibleCells.Random(self.World.SharedRandom);

View File

@@ -43,7 +43,7 @@ namespace OpenRA.Mods.Common.Traits
dudesValue /= 100;
var eligibleLocations = FootprintUtils.Tiles(self).ToList();
var actorTypes = info.ActorTypes.Select(a => new { Name = a, Cost = self.World.Map.Rules.Actors[a].Traits.Get<ValuedInfo>().Cost }).ToArray();
var actorTypes = info.ActorTypes.Select(a => new { Name = a, Cost = self.World.Map.Rules.Actors[a].Traits.Get<ValuedInfo>().Cost }).ToList();
while (eligibleLocations.Count > 0 && actorTypes.Any(a => a.Cost <= dudesValue))
{

View File

@@ -29,9 +29,9 @@ namespace OpenRA.Mods.Common.Traits
public void WorldLoaded(World world, WorldRenderer wr)
{
domainIndexes = new Dictionary<uint, MovementClassDomainIndex>();
var movementClasses = new HashSet<uint>(
var movementClasses =
world.Map.Rules.Actors.Where(ai => ai.Value.Traits.Contains<MobileInfo>())
.Select(ai => (uint)ai.Value.Traits.Get<MobileInfo>().GetMovementClass(world.TileSet)));
.Select(ai => (uint)ai.Value.Traits.Get<MobileInfo>().GetMovementClass(world.TileSet)).Distinct();
foreach (var mc in movementClasses)
domainIndexes[mc] = new MovementClassDomainIndex(world, mc);
@@ -45,7 +45,7 @@ namespace OpenRA.Mods.Common.Traits
/// Regenerate the domain index for a group of cells
public void UpdateCells(World world, IEnumerable<CPos> cells)
{
var dirty = new HashSet<CPos>(cells);
var dirty = cells.ToHashSet();
foreach (var index in domainIndexes)
index.Value.UpdateCells(world, dirty);
}

View File

@@ -28,7 +28,7 @@ namespace OpenRA.Mods.Common.Traits
public void WorldLoaded(World world, WorldRenderer wr)
{
var spawns = world.Map.GetSpawnPoints().ToList();
var spawns = world.Map.GetSpawnPoints();
var taken = world.LobbyInfo.Clients.Where(c => c.SpawnPoint != 0 && c.Slot != null)
.Select(c => spawns[c.SpawnPoint - 1]).ToList();
var available = spawns.Except(taken).ToList();
@@ -85,4 +85,4 @@ namespace OpenRA.Mods.Common.Traits
return sp;
}
}
}
}

View File

@@ -70,7 +70,7 @@ namespace OpenRA.Mods.Common.Traits
}
// Add map smudges
foreach (var s in w.Map.Smudges.Value.Where(s => smudges.Keys.Contains(s.Type)))
foreach (var s in w.Map.Smudges.Value.Where(s => smudges.ContainsKey(s.Type)))
{
var smudge = new Smudge
{