Optimize GC for PropOp.Enum<T>.

This commit is contained in:
2022-12-23 17:45:07 +08:00
parent 617eddc030
commit 13c55dc23e
3 changed files with 6 additions and 4 deletions

View File

@@ -19,7 +19,7 @@ namespace Cryville.Crtr.Components {
public SectionalGameObject() { public SectionalGameObject() {
SubmitProperty("partial", new PropOp.Boolean(v => part = Part.idle)); SubmitProperty("partial", new PropOp.Boolean(v => part = Part.idle));
SubmitProperty("part", new PropOp.Enum<Part>(v => part = v)); SubmitProperty("part", new PropOp.Enum<Part>(v => part = v, v => (Part)v));
} }
protected override void OnDestroy() { protected override void OnDestroy() {

View File

@@ -61,7 +61,7 @@ namespace Cryville.Crtr.Components {
public class SpritePlane : SpriteBase { public class SpritePlane : SpriteBase {
public SpritePlane() { public SpritePlane() {
SubmitProperty("frame", new PropOp.String(v => Frame = v)); SubmitProperty("frame", new PropOp.String(v => Frame = v));
SubmitProperty("fit", new PropOp.Enum<FitMode>(v => Fit = v)); SubmitProperty("fit", new PropOp.Enum<FitMode>(v => Fit = v, v => (FitMode)v));
SubmitProperty("opacity", new PropOp.Float(v => Opacity = v)); SubmitProperty("opacity", new PropOp.Float(v => Opacity = v));
} }

View File

@@ -72,19 +72,21 @@ namespace Cryville.Crtr {
public class Enum<T> : PropOp { public class Enum<T> : PropOp {
readonly static Dictionary<int, int> _cache = new Dictionary<int, int>(); readonly static Dictionary<int, int> _cache = new Dictionary<int, int>();
readonly Action<T> _cb; readonly Action<T> _cb;
public Enum(Action<T> cb) { readonly Func<int, T> _caster;
public Enum(Action<T> cb, Func<int, T> caster) {
if (!typeof(T).IsEnum) if (!typeof(T).IsEnum)
throw new ArgumentException("Type is not enum"); throw new ArgumentException("Type is not enum");
var names = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static); var names = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static);
for (int i = 0; i < names.Length; i++) for (int i = 0; i < names.Length; i++)
_cache[IdentifierManager.SharedInstance.Request(names[i].Name)] = Convert.ToInt32(names[i].GetValue(null)); _cache[IdentifierManager.SharedInstance.Request(names[i].Name)] = Convert.ToInt32(names[i].GetValue(null));
_cb = cb; _cb = cb;
_caster = caster;
} }
protected override void Execute() { protected override void Execute() {
int result = 0; int result = 0;
for (int i = 0; i < LoadedOperandCount; i++) for (int i = 0; i < LoadedOperandCount; i++)
result |= _cache[GetOperand(0).AsIdentifier()]; result |= _cache[GetOperand(0).AsIdentifier()];
_cb((T)(object)result); _cb(_caster(result));
} }
} }
public class BeatTime : PropOp { public class BeatTime : PropOp {