Checked LINQ queries and collections for inefficiencies.
- Made Array.IndexOf available via extension method. - Made ToHashSet extension method. - Change collections queried often via Contains into sets. - Avoid Count() extension if Count or Length property exist. - Made Count() > 0 checks and variations calls to Any() instead. - Don't call ToList/ToArray if there is no benefit to materializing the sequence. - If the sequence does benefit from materialization, follow this general pattern: - Collection queried often via Contains use ToHashSet to speed up lookups. - Short lived variables use ToList. This is because ToArray requires an extra copy to output the final size. - Collections persisted into fields or for a long time use ToArray to minimize memory overhead.
This commit is contained in:
committed by
RoosterDragon
parent
f5f3747338
commit
82bea961ba
@@ -18,7 +18,7 @@ namespace OpenRA
|
||||
{
|
||||
public class Selection
|
||||
{
|
||||
List<Actor> actors = new List<Actor>();
|
||||
readonly HashSet<Actor> actors = new HashSet<Actor>();
|
||||
public void Add(World w, Actor a)
|
||||
{
|
||||
actors.Add(a);
|
||||
@@ -30,20 +30,32 @@ namespace OpenRA
|
||||
|
||||
public bool Contains(Actor a)
|
||||
{
|
||||
return actors.AsEnumerable().Contains(a);
|
||||
return actors.Contains(a);
|
||||
}
|
||||
|
||||
public void Combine(World world, IEnumerable<Actor> newSelection, bool isCombine, bool isClick)
|
||||
{
|
||||
var oldSelection = actors.AsEnumerable();
|
||||
|
||||
if (isClick)
|
||||
{
|
||||
var adjNewSelection = newSelection.Take(1); /* TODO: select BEST, not FIRST */
|
||||
actors = (isCombine ? oldSelection.SymmetricDifference(adjNewSelection) : adjNewSelection).ToList();
|
||||
if (isCombine)
|
||||
actors.SymmetricExceptWith(adjNewSelection);
|
||||
else
|
||||
{
|
||||
actors.Clear();
|
||||
actors.UnionWith(adjNewSelection);
|
||||
}
|
||||
}
|
||||
else
|
||||
actors = (isCombine ? oldSelection.Union(newSelection) : newSelection).ToList();
|
||||
{
|
||||
if (isCombine)
|
||||
actors.UnionWith(newSelection);
|
||||
else
|
||||
{
|
||||
actors.Clear();
|
||||
actors.UnionWith(newSelection);
|
||||
}
|
||||
}
|
||||
|
||||
var voicedUnit = actors.FirstOrDefault(a => a.Owner == world.LocalPlayer && a.IsInWorld && a.HasVoices());
|
||||
if (voicedUnit != null)
|
||||
@@ -57,11 +69,11 @@ namespace OpenRA
|
||||
}
|
||||
|
||||
public IEnumerable<Actor> Actors { get { return actors; } }
|
||||
public void Clear() { actors = new List<Actor>(); }
|
||||
public void Clear() { actors.Clear(); }
|
||||
|
||||
public void Tick(World world)
|
||||
{
|
||||
actors.RemoveAll(a => !a.IsInWorld || (!a.Owner.IsAlliedWith(world.RenderPlayer) && world.FogObscures(a)));
|
||||
actors.RemoveWhere(a => !a.IsInWorld || (!a.Owner.IsAlliedWith(world.RenderPlayer) && world.FogObscures(a)));
|
||||
|
||||
foreach (var cg in controlGroups.Values)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user