From cc985844cd16d0e7003d05d68a60da513e037d44 Mon Sep 17 00:00:00 2001 From: PopSlime Date: Fri, 27 Jan 2023 16:03:27 +0800 Subject: [PATCH] Optimize GC for identifier and beat time property source. --- Assets/Cryville/Crtr/PropSrc.cs | 44 +++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/Assets/Cryville/Crtr/PropSrc.cs b/Assets/Cryville/Crtr/PropSrc.cs index 48bb99b..7517fbb 100644 --- a/Assets/Cryville/Crtr/PropSrc.cs +++ b/Assets/Cryville/Crtr/PropSrc.cs @@ -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 _cb; - bool m_invalidated = true; - protected override bool Invalidated { get { return m_invalidated; } } - public override void Invalidate() { m_invalidated = true; } - public Boolean(Func cb) : base(PdtInternalType.Number) { _cb = cb; buf = new byte[4]; } + public Boolean(Func 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 _cb; - bool m_invalidated = true; - protected override bool Invalidated { get { return m_invalidated; } } - public override void Invalidate() { m_invalidated = true; } - public Float(Func cb) : base(PdtInternalType.Number) { _cb = cb; buf = new byte[4]; } + public Float(Func 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 _cb; - public Identifier(Func cb) : base(PdtInternalType.Undefined) { _cb = cb; } - protected override void InternalGet() { - buf = BitConverter.GetBytes(_cb()); + public Identifier(Func 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 _cb; - public BeatTime(Func cb) : base(PdtInternalType.Vector) { _cb = cb; } + public BeatTime(Func 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;