Optimize GC for identifier and beat time property source.

This commit is contained in:
2023-01-27 16:03:27 +08:00
parent feffbaa5a6
commit cc985844cd

View File

@@ -17,6 +17,15 @@ namespace Cryville.Crtr {
value = buf;
}
protected abstract void InternalGet();
public abstract class FixedBuffer : PropSrc {
bool m_invalidated = true;
protected override bool Invalidated { get { return m_invalidated; } }
public override void Invalidate() { m_invalidated = true; }
public FixedBuffer(int type, int size) : base(type) { buf = new byte[size]; }
protected override void InternalGet() {
m_invalidated = false;
}
}
public class Arbitrary : PropSrc {
readonly byte[] _value;
public Arbitrary(int type, byte[] value) : base(type) {
@@ -26,25 +35,19 @@ namespace Cryville.Crtr {
buf = _value;
}
}
public class Boolean : PropSrc {
public class Boolean : FixedBuffer {
readonly Func<bool> _cb;
bool m_invalidated = true;
protected override bool Invalidated { get { return m_invalidated; } }
public override void Invalidate() { m_invalidated = true; }
public Boolean(Func<bool> cb) : base(PdtInternalType.Number) { _cb = cb; buf = new byte[4]; }
public Boolean(Func<bool> cb) : base(PdtInternalType.Number, 4) { _cb = cb; }
protected override void InternalGet() {
m_invalidated = false;
base.InternalGet();
buf[0] = _cb() ? (byte)1 : (byte)0;
}
}
public class Float : PropSrc {
public class Float : FixedBuffer {
readonly Func<float> _cb;
bool m_invalidated = true;
protected override bool Invalidated { get { return m_invalidated; } }
public override void Invalidate() { m_invalidated = true; }
public Float(Func<float> cb) : base(PdtInternalType.Number) { _cb = cb; buf = new byte[4]; }
public Float(Func<float> cb) : base(PdtInternalType.Number, 4) { _cb = cb; }
protected override unsafe void InternalGet() {
m_invalidated = false;
base.InternalGet();
fixed (byte* _ptr = buf) {
*(float*)_ptr = _cb();
}
@@ -65,19 +68,22 @@ namespace Cryville.Crtr {
}
}
}
public class Identifier : PropSrc {
public class Identifier : FixedBuffer {
readonly Func<int> _cb;
public Identifier(Func<int> cb) : base(PdtInternalType.Undefined) { _cb = cb; }
protected override void InternalGet() {
buf = BitConverter.GetBytes(_cb());
public Identifier(Func<int> cb) : base(PdtInternalType.Undefined, 4) { _cb = cb; }
protected override unsafe void InternalGet() {
base.InternalGet();
fixed (byte* _ptr = buf) {
*(int*)_ptr = _cb();
}
}
}
public class BeatTime : PropSrc {
public class BeatTime : FixedBuffer {
readonly Func<RBeatTime> _cb;
public BeatTime(Func<RBeatTime> cb) : base(PdtInternalType.Vector) { _cb = cb; }
public BeatTime(Func<RBeatTime> cb) : base(PdtInternalType.Vector, 4 * sizeof(int)) { _cb = cb; }
protected override unsafe void InternalGet() {
base.InternalGet();
var bt = _cb();
buf = new byte[4 * sizeof(int)];
fixed (byte* _ptr = buf) {
int* ptr = (int*)_ptr;
*ptr++ = bt.b;