Add MotionCache.
This commit is contained in:
@@ -485,6 +485,7 @@ namespace Cryville.Crtr {
|
|||||||
workerTimer = new diag::Stopwatch();
|
workerTimer = new diag::Stopwatch();
|
||||||
workerTimer.Start();
|
workerTimer.Start();
|
||||||
RMVPool.Prepare();
|
RMVPool.Prepare();
|
||||||
|
MotionCachePool.Prepare();
|
||||||
LoadChart(info);
|
LoadChart(info);
|
||||||
workerTimer.Stop();
|
workerTimer.Stop();
|
||||||
Logger.Log("main", 1, "Load/WorkerThread", "Worker thread done ({0}ms)", workerTimer.Elapsed.TotalMilliseconds);
|
Logger.Log("main", 1, "Load/WorkerThread", "Worker thread done ({0}ms)", workerTimer.Elapsed.TotalMilliseconds);
|
||||||
|
|||||||
@@ -65,16 +65,10 @@ namespace Cryville.Crtr.Event {
|
|||||||
}
|
}
|
||||||
|
|
||||||
readonly RMVPool RMVPool = new RMVPool();
|
readonly RMVPool RMVPool = new RMVPool();
|
||||||
protected Dictionary<StampedEvent, RealtimeMotionValue> PlayingMotions = new Dictionary<StampedEvent, RealtimeMotionValue>();
|
readonly MotionCachePool MCPool = new MotionCachePool();
|
||||||
protected Dictionary<Identifier, RealtimeMotionValue> Values;
|
Dictionary<StampedEvent, RealtimeMotionValue> PlayingMotions = new Dictionary<StampedEvent, RealtimeMotionValue>(4);
|
||||||
protected Dictionary<Identifier, CacheEntry> CachedValues;
|
Dictionary<Identifier, RealtimeMotionValue> Values;
|
||||||
protected class CacheEntry {
|
Dictionary<Identifier, MotionCache> CachedValues;
|
||||||
public bool Valid { get; set; }
|
|
||||||
public Vector Value { get; set; }
|
|
||||||
public CacheEntry Clone() {
|
|
||||||
return new CacheEntry { Valid = Valid, Value = Value == null ? null : Value.Clone() };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a motion value.
|
/// Gets a motion value.
|
||||||
@@ -89,9 +83,9 @@ namespace Cryville.Crtr.Event {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void InvalidateMotion(Identifier name) {
|
void InvalidateMotion(Identifier name) {
|
||||||
CacheEntry cache;
|
MotionCache cache;
|
||||||
if (!CachedValues.TryGetValue(name, out cache))
|
if (!CachedValues.TryGetValue(name, out cache))
|
||||||
CachedValues.Add(name, cache = new CacheEntry());
|
CachedValues.Add(name, cache = MCPool.Rent(name));
|
||||||
cache.Valid = false;
|
cache.Valid = false;
|
||||||
ValidateChildren();
|
ValidateChildren();
|
||||||
foreach (var c in WorkingChildren)
|
foreach (var c in WorkingChildren)
|
||||||
@@ -107,7 +101,7 @@ namespace Cryville.Crtr.Event {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Values = new Dictionary<Identifier, RealtimeMotionValue>(ChartPlayer.motionRegistry.Count);
|
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)
|
foreach (var m in ChartPlayer.motionRegistry)
|
||||||
Values.Add(m.Key, new RealtimeMotionValue().Init(Parent == null ? m.Value.GlobalInitValue : m.Value.InitValue));
|
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;
|
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) {
|
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;
|
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 dest.CachedValues) cv.Value.Valid = false;
|
||||||
foreach (var cv in CachedValues) {
|
foreach (var cv in CachedValues) {
|
||||||
CacheEntry dv;
|
MotionCache dv;
|
||||||
if (dest.CachedValues.TryGetValue(cv.Key, out dv)) {
|
if (!dest.CachedValues.TryGetValue(cv.Key, out dv)) {
|
||||||
dv.Valid = cv.Value.Valid;
|
dest.CachedValues.Add(cv.Key, dv = dest.MCPool.Rent(cv.Key));
|
||||||
if (cv.Value.Value != null) cv.Value.Value.CopyTo(dv.Value);
|
|
||||||
}
|
}
|
||||||
else dest.CachedValues.Add(cv.Key, cv.Value.Clone());
|
cv.Value.CopyTo(dv);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ct != 1) foreach (var cev in WorkingChildren)
|
if (ct != 1) foreach (var cev in WorkingChildren)
|
||||||
@@ -198,6 +193,7 @@ namespace Cryville.Crtr.Event {
|
|||||||
foreach (var s in Children)
|
foreach (var s in Children)
|
||||||
s.Value.Dispose();
|
s.Value.Dispose();
|
||||||
RMVPool.ReturnAll();
|
RMVPool.ReturnAll();
|
||||||
|
MCPool.ReturnAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AttachHandler(ContainerHandler h) {
|
public void AttachHandler(ContainerHandler h) {
|
||||||
@@ -212,11 +208,9 @@ namespace Cryville.Crtr.Event {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Vector GetRawValue(Identifier key) {
|
public Vector GetRawValue(Identifier key) {
|
||||||
CacheEntry tr;
|
MotionCache tr;
|
||||||
if (!CachedValues.TryGetValue(key, out tr))
|
if (!CachedValues.TryGetValue(key, out tr))
|
||||||
CachedValues.Add(key, tr = new CacheEntry { });
|
CachedValues.Add(key, tr = MCPool.Rent(key));
|
||||||
if (tr.Value == null)
|
|
||||||
tr.Value = RMVPool.Rent(key).AbsoluteValue;
|
|
||||||
Vector r = tr.Value;
|
Vector r = tr.Value;
|
||||||
#if !DISABLE_CACHE
|
#if !DISABLE_CACHE
|
||||||
if (tr.Valid) return r;
|
if (tr.Valid) return r;
|
||||||
|
|||||||
55
Assets/Cryville/Crtr/Event/MotionCache.cs
Normal file
55
Assets/Cryville/Crtr/Event/MotionCache.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Assets/Cryville/Crtr/Event/MotionCache.cs.meta
Normal file
11
Assets/Cryville/Crtr/Event/MotionCache.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 5d11b50a98974254f87273c94ed20de7
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
Reference in New Issue
Block a user