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:
@@ -339,21 +339,23 @@ namespace OpenRA
|
||||
return defaultVisibility.IsVisible(this, player);
|
||||
}
|
||||
|
||||
public IEnumerable<string> GetAllTargetTypes()
|
||||
public BitSet<TargetableType> GetAllTargetTypes()
|
||||
{
|
||||
// PERF: Avoid LINQ.
|
||||
var targetTypes = new BitSet<TargetableType>();
|
||||
foreach (var targetable in Targetables)
|
||||
foreach (var targetType in targetable.TargetTypes)
|
||||
yield return targetType;
|
||||
targetTypes = targetTypes.Union(targetable.TargetTypes);
|
||||
return targetTypes;
|
||||
}
|
||||
|
||||
public IEnumerable<string> GetEnabledTargetTypes()
|
||||
public BitSet<TargetableType> GetEnabledTargetTypes()
|
||||
{
|
||||
// PERF: Avoid LINQ.
|
||||
var targetTypes = new BitSet<TargetableType>();
|
||||
foreach (var targetable in Targetables)
|
||||
if (targetable.IsTraitEnabled())
|
||||
foreach (var targetType in targetable.TargetTypes)
|
||||
yield return targetType;
|
||||
targetTypes = targetTypes.Union(targetable.TargetTypes);
|
||||
return targetTypes;
|
||||
}
|
||||
|
||||
public bool IsTargetableBy(Actor byActor)
|
||||
|
||||
@@ -607,14 +607,13 @@ namespace OpenRA
|
||||
|
||||
return InvalidValueAction(value, fieldType, fieldName);
|
||||
}
|
||||
else if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(Bits<>))
|
||||
else if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(BitSet<>))
|
||||
{
|
||||
if (value != null)
|
||||
{
|
||||
var parts = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
var argTypes = new Type[] { typeof(string[]) };
|
||||
var argValues = new object[] { parts };
|
||||
return fieldType.GetConstructor(argTypes).Invoke(argValues);
|
||||
var ctor = fieldType.GetConstructor(new[] { typeof(string[]) });
|
||||
return ctor.Invoke(new object[] { parts.Select(p => p.Trim()).ToArray() });
|
||||
}
|
||||
|
||||
return InvalidValueAction(value, fieldType, fieldName);
|
||||
|
||||
@@ -18,6 +18,7 @@ using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Primitives;
|
||||
|
||||
namespace OpenRA
|
||||
{
|
||||
@@ -98,6 +99,11 @@ namespace OpenRA
|
||||
return "{0},{1},{2},{3}".F(r.X, r.Y, r.Width, r.Height);
|
||||
}
|
||||
|
||||
if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(BitSet<>))
|
||||
{
|
||||
return ((IEnumerable<string>)v).Select(FormatValue).JoinWith(", ");
|
||||
}
|
||||
|
||||
if (t.IsArray && t.GetArrayRank() == 1)
|
||||
{
|
||||
return ((Array)v).Cast<object>().Select(FormatValue).JoinWith(", ");
|
||||
|
||||
@@ -162,5 +162,14 @@ namespace OpenRA
|
||||
public T TraitInfo<T>() where T : ITraitInfoInterface { return traits.Get<T>(); }
|
||||
public T TraitInfoOrDefault<T>() where T : ITraitInfoInterface { return traits.GetOrDefault<T>(); }
|
||||
public IEnumerable<T> TraitInfos<T>() where T : ITraitInfoInterface { return traits.WithInterface<T>(); }
|
||||
|
||||
public BitSet<TargetableType> GetAllTargetTypes()
|
||||
{
|
||||
// PERF: Avoid LINQ.
|
||||
var targetTypes = new BitSet<TargetableType>();
|
||||
foreach (var targetable in TraitInfos<ITargetableInfo>())
|
||||
targetTypes = targetTypes.Union(targetable.GetTargetTypes());
|
||||
return targetTypes;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Effects;
|
||||
using OpenRA.Primitives;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.GameRules
|
||||
@@ -64,10 +65,10 @@ namespace OpenRA.GameRules
|
||||
public readonly int Burst = 1;
|
||||
|
||||
[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("Delay in ticks between firing shots from the same ammo magazine. If one entry, it will be used for all bursts.",
|
||||
"If multiple entries, their number needs to match Burst - 1.")]
|
||||
@@ -116,7 +117,7 @@ namespace OpenRA.GameRules
|
||||
return retList;
|
||||
}
|
||||
|
||||
public bool IsValidTarget(IEnumerable<string> targetTypes)
|
||||
public bool IsValidTarget(BitSet<TargetableType> targetTypes)
|
||||
{
|
||||
return ValidTargets.Overlaps(targetTypes) && !InvalidTargets.Overlaps(targetTypes);
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using OpenRA.FileSystem;
|
||||
using OpenRA.Primitives;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA
|
||||
{
|
||||
@@ -60,7 +61,7 @@ namespace OpenRA
|
||||
static readonly TerrainTypeInfo Default = new TerrainTypeInfo();
|
||||
|
||||
public readonly string Type;
|
||||
public readonly HashSet<string> TargetTypes = new HashSet<string>();
|
||||
public readonly BitSet<TargetableType> TargetTypes;
|
||||
public readonly HashSet<string> AcceptsSmudgeType = new HashSet<string>();
|
||||
public readonly Color Color;
|
||||
public readonly bool RestrictPlayerColor = false;
|
||||
|
||||
@@ -98,6 +98,7 @@
|
||||
<HintPath>..\thirdparty\download\MaxMind.Db.dll</HintPath>
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Numerics" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Activities\Activity.cs" />
|
||||
@@ -272,7 +273,7 @@
|
||||
<Compile Include="Graphics\Vertex.cs" />
|
||||
<Compile Include="FileFormats\PngLoader.cs" />
|
||||
<Compile Include="Primitives\ActionQueue.cs" />
|
||||
<Compile Include="Primitives\Bits.cs" />
|
||||
<Compile Include="Primitives\BitSet.cs" />
|
||||
<Compile Include="Primitives\Cache.cs" />
|
||||
<Compile Include="Primitives\DisposableAction.cs" />
|
||||
<Compile Include="Primitives\float2.cs" />
|
||||
|
||||
151
OpenRA.Game/Primitives/BitSet.cs
Normal file
151
OpenRA.Game/Primitives/BitSet.cs
Normal file
@@ -0,0 +1,151 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2018 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, either version 3 of
|
||||
* the License, or (at your option) any later version. For more
|
||||
* information, see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using BitSetIndex = System.Numerics.BigInteger;
|
||||
|
||||
namespace OpenRA.Primitives
|
||||
{
|
||||
static class BitSetAllocator<T> where T : class
|
||||
{
|
||||
static readonly Cache<string, BitSetIndex> Bits = new Cache<string, BitSetIndex>(Allocate);
|
||||
static BitSetIndex nextBits = 1;
|
||||
|
||||
static BitSetIndex Allocate(string value)
|
||||
{
|
||||
lock (Bits)
|
||||
{
|
||||
var bits = nextBits;
|
||||
nextBits <<= 1;
|
||||
return bits;
|
||||
}
|
||||
}
|
||||
|
||||
public static BitSetIndex GetBits(string[] values)
|
||||
{
|
||||
BitSetIndex bits = 0;
|
||||
lock (Bits)
|
||||
foreach (var value in values)
|
||||
bits |= Bits[value];
|
||||
|
||||
return bits;
|
||||
}
|
||||
|
||||
public static IEnumerable<string> GetStrings(BitSetIndex bits)
|
||||
{
|
||||
var values = new List<string>();
|
||||
lock (Bits)
|
||||
foreach (var kvp in Bits)
|
||||
if ((kvp.Value & bits) != 0)
|
||||
values.Add(kvp.Key);
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
public static bool BitsContainString(BitSetIndex bits, string value)
|
||||
{
|
||||
BitSetIndex valueBit;
|
||||
lock (Bits)
|
||||
if (!Bits.TryGetValue(value, out valueBit))
|
||||
return false;
|
||||
return (bits & valueBit) != 0;
|
||||
}
|
||||
}
|
||||
|
||||
public struct BitSet<T> : IEnumerable<string>, IEquatable<BitSet<T>> where T : class
|
||||
{
|
||||
readonly BitSetIndex bits;
|
||||
|
||||
public BitSet(params string[] values) : this(BitSetAllocator<T>.GetBits(values)) { }
|
||||
BitSet(BitSetIndex bits) { this.bits = bits; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return BitSetAllocator<T>.GetStrings(bits).JoinWith(",");
|
||||
}
|
||||
|
||||
public static bool operator ==(BitSet<T> me, BitSet<T> other) { return me.bits == other.bits; }
|
||||
public static bool operator !=(BitSet<T> me, BitSet<T> other) { return !(me == other); }
|
||||
|
||||
public bool Equals(BitSet<T> other) { return other == this; }
|
||||
public override bool Equals(object obj) { return obj is BitSet<T> && Equals((BitSet<T>)obj); }
|
||||
public override int GetHashCode() { return bits.GetHashCode(); }
|
||||
|
||||
public bool IsEmpty { get { return bits == 0; } }
|
||||
|
||||
public bool IsProperSubsetOf(BitSet<T> other)
|
||||
{
|
||||
return IsSubsetOf(other) && !SetEquals(other);
|
||||
}
|
||||
|
||||
public bool IsProperSupersetOf(BitSet<T> other)
|
||||
{
|
||||
return IsSupersetOf(other) && !SetEquals(other);
|
||||
}
|
||||
|
||||
public bool IsSubsetOf(BitSet<T> other)
|
||||
{
|
||||
return (bits | other.bits) == other.bits;
|
||||
}
|
||||
|
||||
public bool IsSupersetOf(BitSet<T> other)
|
||||
{
|
||||
return (bits | other.bits) == bits;
|
||||
}
|
||||
|
||||
public bool Overlaps(BitSet<T> other)
|
||||
{
|
||||
return (bits & other.bits) != 0;
|
||||
}
|
||||
|
||||
public bool SetEquals(BitSet<T> other)
|
||||
{
|
||||
return bits == other.bits;
|
||||
}
|
||||
|
||||
public bool Contains(string value)
|
||||
{
|
||||
return BitSetAllocator<T>.BitsContainString(bits, value);
|
||||
}
|
||||
|
||||
public IEnumerator<string> GetEnumerator()
|
||||
{
|
||||
return BitSetAllocator<T>.GetStrings(bits).GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
public BitSet<T> Except(BitSet<T> other)
|
||||
{
|
||||
return new BitSet<T>(bits & ~other.bits);
|
||||
}
|
||||
|
||||
public BitSet<T> Intersect(BitSet<T> other)
|
||||
{
|
||||
return new BitSet<T>(bits & other.bits);
|
||||
}
|
||||
|
||||
public BitSet<T> SymmetricExcept(BitSet<T> other)
|
||||
{
|
||||
return new BitSet<T>(bits ^ other.bits);
|
||||
}
|
||||
|
||||
public BitSet<T> Union(BitSet<T> other)
|
||||
{
|
||||
return new BitSet<T>(bits | other.bits);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2018 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, either version 3 of
|
||||
* the License, or (at your option) any later version. For more
|
||||
* information, see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace OpenRA.Primitives
|
||||
{
|
||||
static class BitAllocator<T> where T : struct
|
||||
{
|
||||
static int nextVal = 1;
|
||||
static Cache<string, int> bits = new Cache<string, int>(_ => Allocate());
|
||||
|
||||
static int Allocate()
|
||||
{
|
||||
if (nextVal == 0)
|
||||
throw new InvalidOperationException(
|
||||
"Too many values in BitAllocator<{0}>".F(typeof(T).Name));
|
||||
|
||||
var val = nextVal;
|
||||
nextVal <<= 1;
|
||||
return val;
|
||||
}
|
||||
|
||||
public static int GetValue(string[] val)
|
||||
{
|
||||
return val.Select(a => bits[a]).Aggregate(0, (a, b) => a | b);
|
||||
}
|
||||
|
||||
public static IEnumerable<string> GetStrings(int val)
|
||||
{
|
||||
for (var i = 0; i < 32; i++)
|
||||
{
|
||||
var x = 1 << i;
|
||||
if ((val & x) != 0)
|
||||
yield return bits.Single(a => a.Value == x).Key;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public struct Bits<T> : IEquatable<Bits<T>> where T : struct
|
||||
{
|
||||
public int Value;
|
||||
|
||||
public Bits(string[] val) { Value = BitAllocator<T>.GetValue(val); }
|
||||
public Bits(Bits<T> other) { Value = other.Value; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return BitAllocator<T>.GetStrings(Value).JoinWith(",");
|
||||
}
|
||||
|
||||
public static bool operator ==(Bits<T> me, Bits<T> other) { return me.Value == other.Value; }
|
||||
public static bool operator !=(Bits<T> me, Bits<T> other) { return !(me == other); }
|
||||
|
||||
public bool Equals(Bits<T> other) { return other == this; }
|
||||
public override bool Equals(object obj) { return obj is Bits<T> && Equals((Bits<T>)obj); }
|
||||
public override int GetHashCode() { return Value.GetHashCode(); }
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,7 @@ namespace OpenRA.Traits
|
||||
readonly Shroud shroud;
|
||||
|
||||
public Player Owner { get; private set; }
|
||||
public HashSet<string> TargetTypes { get; private set; }
|
||||
public BitSet<TargetableType> TargetTypes { get; private set; }
|
||||
|
||||
public ITooltipInfo TooltipInfo { get; private set; }
|
||||
public Player TooltipOwner { get; private set; }
|
||||
@@ -84,7 +84,6 @@ namespace OpenRA.Traits
|
||||
footprint.Select(p => shroud.Contains(p).ToString()).JoinWith("|")));
|
||||
|
||||
CenterPosition = actor.CenterPosition;
|
||||
TargetTypes = new HashSet<string>();
|
||||
|
||||
tooltips = actor.TraitsImplementing<ITooltip>().ToArray();
|
||||
health = actor.TraitOrDefault<IHealth>();
|
||||
@@ -101,10 +100,7 @@ namespace OpenRA.Traits
|
||||
public void RefreshState()
|
||||
{
|
||||
Owner = actor.Owner;
|
||||
|
||||
// PERF: Reuse collection to avoid allocations.
|
||||
TargetTypes.Clear();
|
||||
TargetTypes.UnionWith(actor.GetEnabledTargetTypes());
|
||||
TargetTypes = actor.GetEnabledTargetTypes();
|
||||
|
||||
if (health != null)
|
||||
{
|
||||
|
||||
@@ -365,15 +365,20 @@ namespace OpenRA.Traits
|
||||
bool SpatiallyPartitionable { get; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicates target types as defined on <see cref="Traits.ITargetable"/> are present in a <see cref="Primitives.BitSet{T}"/>.
|
||||
/// </summary>
|
||||
public sealed class TargetableType { TargetableType() { } }
|
||||
|
||||
public interface ITargetableInfo : ITraitInfoInterface
|
||||
{
|
||||
HashSet<string> GetTargetTypes();
|
||||
BitSet<TargetableType> GetTargetTypes();
|
||||
}
|
||||
|
||||
public interface ITargetable
|
||||
{
|
||||
// Check IsTraitEnabled or !IsTraitDisabled first
|
||||
HashSet<string> TargetTypes { get; }
|
||||
BitSet<TargetableType> TargetTypes { get; }
|
||||
bool TargetableBy(Actor self, Actor byActor);
|
||||
bool RequiresForceFire { get; }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user