Add MotionCache.

This commit is contained in:
2023-01-21 20:13:40 +08:00
parent 94428d9e18
commit c1c354959d
4 changed files with 85 additions and 24 deletions

View File

@@ -485,6 +485,7 @@ namespace Cryville.Crtr {
workerTimer = new diag::Stopwatch();
workerTimer.Start();
RMVPool.Prepare();
MotionCachePool.Prepare();
LoadChart(info);
workerTimer.Stop();
Logger.Log("main", 1, "Load/WorkerThread", "Worker thread done ({0}ms)", workerTimer.Elapsed.TotalMilliseconds);

View File

@@ -65,16 +65,10 @@ namespace Cryville.Crtr.Event {
}
readonly RMVPool RMVPool = new RMVPool();
protected Dictionary<StampedEvent, RealtimeMotionValue> PlayingMotions = new Dictionary<StampedEvent, RealtimeMotionValue>();
protected Dictionary<Identifier, RealtimeMotionValue> Values;
protected Dictionary<Identifier, CacheEntry> CachedValues;
protected class CacheEntry {
public bool Valid { get; set; }
public Vector Value { get; set; }
public CacheEntry Clone() {
return new CacheEntry { Valid = Valid, Value = Value == null ? null : Value.Clone() };
}
}
readonly MotionCachePool MCPool = new MotionCachePool();
Dictionary<StampedEvent, RealtimeMotionValue> PlayingMotions = new Dictionary<StampedEvent, RealtimeMotionValue>(4);
Dictionary<Identifier, RealtimeMotionValue> Values;
Dictionary<Identifier, MotionCache> CachedValues;
/// <summary>
/// Gets a motion value.
@@ -89,9 +83,9 @@ namespace Cryville.Crtr.Event {
}
void InvalidateMotion(Identifier name) {
CacheEntry cache;
MotionCache cache;
if (!CachedValues.TryGetValue(name, out cache))
CachedValues.Add(name, cache = new CacheEntry());
CachedValues.Add(name, cache = MCPool.Rent(name));
cache.Valid = false;
ValidateChildren();
foreach (var c in WorkingChildren)
@@ -107,7 +101,7 @@ namespace Cryville.Crtr.Event {
}
Values = new Dictionary<Identifier, RealtimeMotionValue>(ChartPlayer.motionRegistry.Count);
CachedValues = new Dictionary<Identifier, CacheEntry>(ChartPlayer.motionRegistry.Count);
CachedValues = new Dictionary<Identifier, MotionCache>(ChartPlayer.motionRegistry.Count);
foreach (var m in ChartPlayer.motionRegistry)
Values.Add(m.Key, new RealtimeMotionValue().Init(Parent == null ? m.Value.GlobalInitValue : m.Value.InitValue));
}
@@ -128,9 +122,11 @@ namespace Cryville.Crtr.Event {
}
r.Values = mvs;
var cvs = new Dictionary<Identifier, CacheEntry>(ChartPlayer.motionRegistry.Count);
var cvs = new Dictionary<Identifier, MotionCache>(ChartPlayer.motionRegistry.Count);
foreach (var cv in CachedValues) {
cvs.Add(cv.Key, cv.Value.Clone());
var dv = r.MCPool.Rent(cv.Key);
cv.Value.CopyTo(dv);
cvs.Add(cv.Key, dv);
}
r.CachedValues = cvs;
@@ -169,12 +165,11 @@ namespace Cryville.Crtr.Event {
foreach (var cv in dest.CachedValues) cv.Value.Valid = false;
foreach (var cv in CachedValues) {
CacheEntry dv;
if (dest.CachedValues.TryGetValue(cv.Key, out dv)) {
dv.Valid = cv.Value.Valid;
if (cv.Value.Value != null) cv.Value.Value.CopyTo(dv.Value);
MotionCache dv;
if (!dest.CachedValues.TryGetValue(cv.Key, out dv)) {
dest.CachedValues.Add(cv.Key, dv = dest.MCPool.Rent(cv.Key));
}
else dest.CachedValues.Add(cv.Key, cv.Value.Clone());
cv.Value.CopyTo(dv);
}
if (ct != 1) foreach (var cev in WorkingChildren)
@@ -198,6 +193,7 @@ namespace Cryville.Crtr.Event {
foreach (var s in Children)
s.Value.Dispose();
RMVPool.ReturnAll();
MCPool.ReturnAll();
}
public void AttachHandler(ContainerHandler h) {
@@ -212,11 +208,9 @@ namespace Cryville.Crtr.Event {
}
public Vector GetRawValue(Identifier key) {
CacheEntry tr;
MotionCache tr;
if (!CachedValues.TryGetValue(key, out tr))
CachedValues.Add(key, tr = new CacheEntry { });
if (tr.Value == null)
tr.Value = RMVPool.Rent(key).AbsoluteValue;
CachedValues.Add(key, tr = MCPool.Rent(key));
Vector r = tr.Value;
#if !DISABLE_CACHE
if (tr.Valid) return r;

View File

@@ -0,0 +1,55 @@
using Cryville.Common;
using Cryville.Common.Buffers;
using System.Collections.Generic;
namespace Cryville.Crtr.Event {
internal class MotionCache {
public bool Valid { get; set; }
public Vector Value { get; set; }
public void CopyTo(MotionCache dest) {
dest.Valid = Valid;
Value.CopyTo(dest.Value);
}
}
internal class MotionCachePool {
private class Bucket : ObjectPool<MotionCache> {
readonly MotionRegistry _reg;
public Bucket(string name, int capacity) : base(capacity) {
_reg = ChartPlayer.motionRegistry[name];
}
protected override MotionCache Construct() {
var result = new MotionCache();
result.Value = (Vector)ReflectionHelper.InvokeEmptyConstructor(_reg.Type);
return result;
}
}
static Dictionary<Identifier, Bucket> _buckets;
public static void Prepare() {
_buckets = new Dictionary<Identifier, Bucket>(ChartPlayer.motionRegistry.Count);
foreach (var reg in ChartPlayer.motionRegistry)
_buckets.Add(reg.Key, new Bucket(reg.Key, 4096));
}
static readonly SimpleObjectPool<Dictionary<MotionCache, Identifier>> _dictPool
= new SimpleObjectPool<Dictionary<MotionCache, Identifier>>(1024);
Dictionary<MotionCache, Identifier> _rented;
public MotionCache Rent(Identifier name) {
var obj = _buckets[name].Rent();
obj.Valid = false;
if (_rented == null) _rented = _dictPool.Rent();
_rented.Add(obj, name);
return obj;
}
public void Return(MotionCache obj) {
_buckets[_rented[obj]].Return(obj);
_rented.Remove(obj);
}
public void ReturnAll() {
if (_rented == null) return;
foreach (var obj in _rented)
_buckets[obj.Value].Return(obj.Key);
_rented.Clear();
_dictPool.Return(_rented);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5d11b50a98974254f87273c94ed20de7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: