Pull up CategorizedPool. Add Reset method for ObjectPool.

This commit is contained in:
2023-02-11 23:08:43 +08:00
parent b364005741
commit 9fd685b8b3
7 changed files with 90 additions and 69 deletions

View File

@@ -1,6 +1,7 @@
//#define DISABLE_CACHE
using Cryville.Common;
using Cryville.Common.Buffers;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
@@ -90,6 +91,9 @@ namespace Cryville.Crtr.Event {
Parent = parent;
}
_rmvpa = new CategorizedPoolAccessor<Identifier, RealtimeMotionValue>(RMVPool);
_mcpa = new CategorizedPoolAccessor<Identifier, MotionCache>(MCPool);
Values = new Dictionary<Identifier, RealtimeMotionValue>(ChartPlayer.motionRegistry.Count);
CachedValues = new Dictionary<Identifier, MotionCache>(ChartPlayer.motionRegistry.Count);
foreach (var m in ChartPlayer.motionRegistry)
@@ -159,7 +163,7 @@ namespace Cryville.Crtr.Event {
foreach (var cv in CachedValues) {
MotionCache dv;
if (!dest.CachedValues.TryGetValue(cv.Key, out dv)) {
dest.CachedValues.Add(cv.Key, dv = dest.MCPool.Rent(cv.Key));
dest.CachedValues.Add(cv.Key, dv = dest._mcpa.Rent(cv.Key));
}
cv.Value.CopyTo(dv);
}
@@ -187,8 +191,8 @@ namespace Cryville.Crtr.Event {
Disposed = true;
if (CloneType == 1) Handler.Dispose();
if (CloneType == 1 || CloneType == 17) {
RMVPool.ReturnAll();
MCPool.ReturnAll();
_rmvpa.ReturnAll();
_mcpa.ReturnAll();
}
foreach (var s in Children)
s.Value.Dispose();
@@ -212,8 +216,10 @@ namespace Cryville.Crtr.Event {
#endregion
#region Motion
readonly RMVPool RMVPool = new RMVPool();
readonly MotionCachePool MCPool = new MotionCachePool();
internal static RMVPool RMVPool;
internal static MotionCachePool MCPool;
readonly CategorizedPoolAccessor<Identifier, RealtimeMotionValue> _rmvpa;
readonly CategorizedPoolAccessor<Identifier, MotionCache> _mcpa;
Dictionary<StampedEvent, RealtimeMotionValue> PlayingMotions = new Dictionary<StampedEvent, RealtimeMotionValue>(4);
Dictionary<Identifier, RealtimeMotionValue> Values;
Dictionary<Identifier, MotionCache> CachedValues;
@@ -233,7 +239,7 @@ namespace Cryville.Crtr.Event {
void InvalidateMotion(Identifier name) {
MotionCache cache;
if (!CachedValues.TryGetValue(name, out cache))
CachedValues.Add(name, cache = MCPool.Rent(name));
CachedValues.Add(name, cache = _mcpa.Rent(name));
cache.Valid = false;
foreach (var c in ActiveChildren)
Children[c].InvalidateMotion(name);
@@ -242,7 +248,7 @@ namespace Cryville.Crtr.Event {
public Vector GetRawValue(Identifier key) {
MotionCache tr;
if (!CachedValues.TryGetValue(key, out tr))
CachedValues.Add(key, tr = MCPool.Rent(key));
CachedValues.Add(key, tr = _mcpa.Rent(key));
Vector r = tr.Value;
#if !DISABLE_CACHE
if (tr.Valid) return r;
@@ -351,14 +357,14 @@ namespace Cryville.Crtr.Event {
if (ev != null) {
if (ev.Unstamped is Chart.Motion) {
var tev = (Chart.Motion)ev.Unstamped;
var mv = RMVPool.Rent(tev.Name);
var mv = _rmvpa.Rent(tev.Name);
mv.CloneTypeFlag = CloneType;
GetMotionValue(tev.Name).CopyTo(mv);
PlayingMotions.Add(ev, mv);
Update(ev);
if (!ev.Unstamped.IsLong) {
PlayingMotions.Remove(ev);
RMVPool.Return(mv);
_rmvpa.Return(mv);
}
}
else if (ev.Unstamped is EventContainer) {
@@ -376,7 +382,7 @@ namespace Cryville.Crtr.Event {
if (nev is Chart.Motion) {
Update(ev);
var mv = PlayingMotions[ev.Origin];
if (mv.CloneTypeFlag == CloneType) RMVPool.Return(mv);
if (mv.CloneTypeFlag == CloneType) _rmvpa.Return(mv);
PlayingMotions.Remove(ev.Origin);
}
else if (nev is EventContainer) {