Use a BitSet for representing target types.

- Rename Bits<T> to BitSet<T>.
- Implement set based helpers for BitSet<T>.
- When representing TargetTypes of ITargetable in various traits, use a BitSet<TargetableType> instead of HashSet<string> for better performance & reduced memory usage.
- Fix FieldLoader to trim input values when generating a BitSet<T>.
- Require T in BitSet<T> and BitSetAllocator<T> to be a class since it's just a marker value. This allows the JIT to instantiate generic code for these classes once, as they don't benefit from specialized code for T. (Typically JITs will generate shared code for all reference types, and unique code for every value type encountered).
This commit is contained in:
RoosterDragon
2018-03-18 18:45:16 +00:00
committed by abcdefg30
parent 8fa94c5301
commit 5bd5a384b7
36 changed files with 250 additions and 134 deletions

View File

@@ -13,13 +13,14 @@ using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.AI
{
abstract class AirStateBase : StateBase
{
static readonly string[] AirTargetTypes = new[] { "Air" };
static readonly BitSet<TargetableType> AirTargetTypes = new BitSet<TargetableType>("Air");
protected const int MissileUnitMultiplier = 3;

View File

@@ -105,7 +105,7 @@ namespace OpenRA.Mods.Common.AI
else
{
var enemies = owner.World.FindActorsInCircle(leader.CenterPosition, WDist.FromCells(owner.Bot.Info.AttackScanRadius))
.Where(a => !a.IsDead && leader.Owner.Stances[a.Owner] == Stance.Enemy && a.GetEnabledTargetTypes().Any());
.Where(a => !a.IsDead && leader.Owner.Stances[a.Owner] == Stance.Enemy && !a.GetEnabledTargetTypes().IsEmpty);
var target = enemies.ClosestTo(leader.CenterPosition);
if (target != null)
{

View File

@@ -132,7 +132,7 @@ namespace OpenRA.Mods.Common.AI
else
{
var enemies = owner.World.FindActorsInCircle(leader.CenterPosition, WDist.FromCells(owner.Bot.Info.AttackScanRadius))
.Where(a => !a.IsDead && leader.Owner.Stances[a.Owner] == Stance.Enemy && a.GetEnabledTargetTypes().Any());
.Where(a => !a.IsDead && leader.Owner.Stances[a.Owner] == Stance.Enemy && !a.GetEnabledTargetTypes().IsEmpty);
var target = enemies.ClosestTo(leader.CenterPosition);
if (target != null)
{

View File

@@ -65,7 +65,7 @@ namespace OpenRA.Mods.Common.AI
return false;
var targetTypes = target.GetEnabledTargetTypes();
if (!targetTypes.Any())
if (targetTypes.IsEmpty)
return false;
var arms = a.TraitsImplementing<Armament>();

View File

@@ -11,6 +11,7 @@
using System.Collections.Generic;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.AI
@@ -122,7 +123,7 @@ namespace OpenRA.Mods.Common.AI
public readonly Stance Against = Stance.Enemy;
[Desc("What types should the desired targets of this power be?")]
public readonly HashSet<string> Types = new HashSet<string> { "Air", "Ground", "Water" };
public readonly BitSet<TargetableType> Types = new BitSet<TargetableType>("Air", "Ground", "Water");
[Desc("How attractive are these types of targets?")]
public readonly int Attractiveness = 100;

View File

@@ -35,8 +35,8 @@ namespace OpenRA.Mods.Common.Lint
if (!deathTypes.Any() || spawnActorOnAnyDeathType)
continue;
var targetable = actorInfo.Value.TraitInfos<ITargetableInfo>().SelectMany(x => x.GetTargetTypes()).ToList();
if (!targetable.Any())
var targetable = actorInfo.Value.GetAllTargetTypes();
if (targetable.IsEmpty)
continue;
foreach (var weaponInfo in rules.Weapons)

View File

@@ -10,6 +10,7 @@
#endregion
using System.Collections.Generic;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Orders
@@ -67,9 +68,9 @@ namespace OpenRA.Mods.Common.Orders
public class TargetTypeOrderTargeter : UnitOrderTargeter
{
readonly HashSet<string> targetTypes;
readonly BitSet<TargetableType> targetTypes;
public TargetTypeOrderTargeter(HashSet<string> targetTypes, string order, int priority, string cursor, bool targetEnemyUnits, bool targetAllyUnits)
public TargetTypeOrderTargeter(BitSet<TargetableType> targetTypes, string order, int priority, string cursor, bool targetEnemyUnits, bool targetAllyUnits)
: base(order, priority, cursor, targetEnemyUnits, targetAllyUnits)
{
this.targetTypes = targetTypes;

View File

@@ -13,6 +13,7 @@ using System;
using System.Collections.Generic;
using Eluant;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Scripting;
using OpenRA.Traits;
@@ -302,7 +303,7 @@ namespace OpenRA.Mods.Common.Scripting
OnCapturedInternal(self);
}
void INotifyInfiltrated.Infiltrated(Actor self, Actor infiltrator, HashSet<string> types)
void INotifyInfiltrated.Infiltrated(Actor self, Actor infiltrator, BitSet<TargetableType> types)
{
if (world.Disposing)
return;

View File

@@ -13,6 +13,7 @@ using System.Collections.Generic;
using System.Drawing;
using OpenRA.Activities;
using OpenRA.Mods.Common.Orders;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
@@ -45,7 +46,7 @@ namespace OpenRA.Mods.Common.Traits
if (IsTraitDisabled)
yield break;
yield return new TargetTypeOrderTargeter(new HashSet<string> { "DetonateAttack" }, "DetonateAttack", 5, "attack", true, false) { ForceAttack = false };
yield return new TargetTypeOrderTargeter(new BitSet<TargetableType>("DetonateAttack"), "DetonateAttack", 5, "attack", true, false) { ForceAttack = false };
yield return new DeployOrderTargeter("Detonate", 5);
}
}

View File

@@ -284,7 +284,7 @@ namespace OpenRA.Mods.Common.Traits
continue;
// Check whether we can auto-target this actor
var targetTypes = actor.GetEnabledTargetTypes().ToArray();
var targetTypes = actor.GetEnabledTargetTypes();
var validPriorities = activePriorities.Where(ati =>
{
// Already have a higher priority target

View File

@@ -9,7 +9,7 @@
*/
#endregion
using System.Collections.Generic;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
@@ -18,10 +18,10 @@ namespace OpenRA.Mods.Common.Traits
public class AutoTargetPriorityInfo : ConditionalTraitInfo, Requires<AutoTargetInfo>
{
[Desc("Target types that can be AutoTargeted.")]
public readonly HashSet<string> ValidTargets = new HashSet<string> { "Ground", "Water", "Air" };
public readonly BitSet<TargetableType> ValidTargets = new BitSet<TargetableType>("Ground", "Water", "Air");
[Desc("Target types that can't be AutoTargeted.", "Overrules ValidTargets.")]
public readonly HashSet<string> InvalidTargets = new HashSet<string>();
public readonly BitSet<TargetableType> InvalidTargets;
[Desc("ValidTargets with larger priorities will be AutoTargeted before lower priorities.")]
public readonly int Priority = 1;

View File

@@ -33,7 +33,7 @@ namespace OpenRA.Mods.Common.Traits
public readonly int MaxRadius = 4;
[Desc("The list of unit target types we are allowed to duplicate.")]
public readonly HashSet<string> ValidTargets = new HashSet<string> { "Ground", "Water" };
public readonly BitSet<TargetableType> ValidTargets = new BitSet<TargetableType>("Ground", "Water");
[Desc("Which factions this crate action can occur for.")]
public readonly HashSet<string> ValidFactions = new HashSet<string>();

View File

@@ -11,6 +11,7 @@
using System.Collections.Generic;
using System.Linq;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
@@ -19,8 +20,8 @@ namespace OpenRA.Mods.Common.Traits
public class TargetableInfo : ConditionalTraitInfo, ITargetableInfo
{
[Desc("Target type. Used for filtering (in)valid targets.")]
public readonly HashSet<string> TargetTypes = new HashSet<string>();
public HashSet<string> GetTargetTypes() { return TargetTypes; }
public readonly BitSet<TargetableType> TargetTypes;
public BitSet<TargetableType> GetTargetTypes() { return TargetTypes; }
public bool RequiresForceFire = false;
@@ -53,7 +54,7 @@ namespace OpenRA.Mods.Common.Traits
return cloaks.All(c => c.IsTraitDisabled || c.IsVisible(self, viewer.Owner));
}
public virtual HashSet<string> TargetTypes { get { return Info.TargetTypes; } }
public virtual BitSet<TargetableType> TargetTypes { get { return Info.TargetTypes; } }
public bool RequiresForceFire { get { return Info.RequiresForceFire; } }
}

View File

@@ -118,7 +118,7 @@ namespace OpenRA.Mods.Common.Traits
public interface ISeedableResource { void Seed(Actor self); }
[RequireExplicitImplementation]
public interface INotifyInfiltrated { void Infiltrated(Actor self, Actor infiltrator, HashSet<string> types); }
public interface INotifyInfiltrated { void Infiltrated(Actor self, Actor infiltrator, BitSet<TargetableType> types); }
[RequireExplicitImplementation]
public interface INotifyBlockingMove { void OnNotifyBlockingMove(Actor self, Actor blocking); }

View File

@@ -14,6 +14,7 @@ using System.Linq;
using OpenRA.GameRules;
using OpenRA.Mods.Common.Effects;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Warheads
@@ -48,7 +49,7 @@ namespace OpenRA.Mods.Common.Warheads
[Desc("Whether to consider actors in determining whether the explosion should happen. If false, only terrain will be considered.")]
public readonly bool ImpactActors = true;
static readonly string[] TargetTypeAir = new string[] { "Air" };
static readonly BitSet<TargetableType> TargetTypeAir = new BitSet<TargetableType>("Air");
public ImpactType GetImpactType(World world, CPos cell, WPos pos, Actor firedBy)
{

View File

@@ -12,6 +12,7 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Warheads
@@ -35,10 +36,10 @@ namespace OpenRA.Mods.Common.Warheads
public abstract class Warhead : IWarhead
{
[Desc("What types of targets are affected.")]
public readonly HashSet<string> ValidTargets = new HashSet<string> { "Ground", "Water" };
public readonly BitSet<TargetableType> ValidTargets = new BitSet<TargetableType>("Ground", "Water");
[Desc("What types of targets are unaffected.", "Overrules ValidTargets.")]
public readonly HashSet<string> InvalidTargets = new HashSet<string>();
public readonly BitSet<TargetableType> InvalidTargets;
[Desc("What diplomatic stances are affected.")]
public readonly Stance ValidStances = Stance.Ally | Stance.Neutral | Stance.Enemy;
@@ -54,7 +55,7 @@ namespace OpenRA.Mods.Common.Warheads
[Desc("The color used for this warhead's visualization in the world's `WarheadDebugOverlay` trait.")]
public readonly Color DebugOverlayColor = Color.Red;
public bool IsValidTarget(IEnumerable<string> targetTypes)
public bool IsValidTarget(BitSet<TargetableType> targetTypes)
{
return ValidTargets.Overlaps(targetTypes) && !InvalidTargets.Overlaps(targetTypes);
}