Introduce IntKeyedDictionary to improve performance.

This commit is contained in:
2023-03-24 17:06:47 +08:00
parent 89f391f040
commit e2c683567e
18 changed files with 1274 additions and 94 deletions

View File

@@ -1,5 +1,6 @@
using Cryville.Common;
using Cryville.Common.Buffers;
using Cryville.Common.Collections.Specialized;
using Cryville.Crtr.Components;
using System;
using System.Collections.Generic;
@@ -65,7 +66,7 @@ namespace Cryville.Crtr.Event {
static readonly int _var_current_time = IdentifierManager.SharedInstance.Request("current_time");
static readonly int _var_invisible_bounds = IdentifierManager.SharedInstance.Request("invisible_bounds");
public readonly Dictionary<int, PropSrc> PropSrcs = new Dictionary<int, PropSrc>();
public readonly IntKeyedDictionary<PropSrc> PropSrcs = new IntKeyedDictionary<PropSrc>();
SkinContainer skinContainer;
protected Judge judge;
public void AttachSystems(PdtSkin skin, Judge judge) {
@@ -73,9 +74,9 @@ namespace Cryville.Crtr.Event {
this.judge = judge;
}
public readonly Dictionary<int, List<Anchor>> Anchors = new Dictionary<int, List<Anchor>>();
public readonly Dictionary<int, Anchor> DynamicAnchors = new Dictionary<int, Anchor>();
public readonly Dictionary<int, double> DynamicAnchorSetTime = new Dictionary<int, double>();
public readonly IntKeyedDictionary<List<Anchor>> Anchors = new IntKeyedDictionary<List<Anchor>>();
public readonly IntKeyedDictionary<Anchor> DynamicAnchors = new IntKeyedDictionary<Anchor>();
public readonly IntKeyedDictionary<double> DynamicAnchorSetTime = new IntKeyedDictionary<double>();
Anchor a_cur;
Anchor a_head;
Anchor a_tail;

View File

@@ -2,6 +2,7 @@
using Cryville.Common;
using Cryville.Common.Buffers;
using Cryville.Common.Collections.Specialized;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
@@ -91,13 +92,13 @@ namespace Cryville.Crtr.Event {
Parent = parent;
}
_rmvpa = new CategorizedPoolAccessor<Identifier, RealtimeMotionValue>(RMVPool);
_mcpa = new CategorizedPoolAccessor<Identifier, MotionCache>(MCPool);
_rmvpa = new CategorizedPoolAccessor<int, RealtimeMotionValue>(RMVPool);
_mcpa = new CategorizedPoolAccessor<int, MotionCache>(MCPool);
Values = new Dictionary<Identifier, RealtimeMotionValue>(ChartPlayer.motionRegistry.Count);
CachedValues = new Dictionary<Identifier, MotionCache>(ChartPlayer.motionRegistry.Count);
Values = new IntKeyedDictionary<RealtimeMotionValue>(ChartPlayer.motionRegistry.Count);
CachedValues = new IntKeyedDictionary<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));
Values.Add(m.Key.Key, new RealtimeMotionValue().Init(Parent == null ? m.Value.GlobalInitValue : m.Value.InitValue));
rootPrototype = this;
}
@@ -114,13 +115,13 @@ namespace Cryville.Crtr.Event {
public ContainerState Clone(byte ct) {
var r = (ContainerState)MemberwiseClone();
var mvs = new Dictionary<Identifier, RealtimeMotionValue>(ChartPlayer.motionRegistry.Count);
var mvs = new IntKeyedDictionary<RealtimeMotionValue>(ChartPlayer.motionRegistry.Count);
foreach (var mv in Values) {
mvs.Add(mv.Key, mv.Value.Clone());
}
r.Values = mvs;
var cvs = new Dictionary<Identifier, MotionCache>(ChartPlayer.motionRegistry.Count);
var cvs = new IntKeyedDictionary<MotionCache>(ChartPlayer.motionRegistry.Count);
r.CachedValues = cvs;
r.Children = new Dictionary<EventContainer, ContainerState>();
@@ -218,11 +219,11 @@ namespace Cryville.Crtr.Event {
#region Motion
internal static RMVPool RMVPool;
internal static MotionCachePool MCPool;
readonly CategorizedPoolAccessor<Identifier, RealtimeMotionValue> _rmvpa;
readonly CategorizedPoolAccessor<Identifier, MotionCache> _mcpa;
readonly CategorizedPoolAccessor<int, RealtimeMotionValue> _rmvpa;
readonly CategorizedPoolAccessor<int, MotionCache> _mcpa;
Dictionary<StampedEvent, RealtimeMotionValue> PlayingMotions = new Dictionary<StampedEvent, RealtimeMotionValue>(4);
Dictionary<Identifier, RealtimeMotionValue> Values;
Dictionary<Identifier, MotionCache> CachedValues;
IntKeyedDictionary<RealtimeMotionValue> Values;
IntKeyedDictionary<MotionCache> CachedValues;
/// <summary>
/// Gets a motion value.
@@ -230,13 +231,13 @@ namespace Cryville.Crtr.Event {
/// <param name="name">The motion name.</param>
/// <param name="clone">Returns a cloned motion value instead.</param>
/// <returns>A motion value.</returns>
RealtimeMotionValue GetMotionValue(Identifier name, bool clone = false) {
RealtimeMotionValue GetMotionValue(int name, bool clone = false) {
RealtimeMotionValue value = Values[name];
if (clone) return value.Clone();
return value;
}
void InvalidateMotion(Identifier name) {
void InvalidateMotion(int name) {
MotionCache cache;
if (!CachedValues.TryGetValue(name, out cache))
CachedValues.Add(name, cache = _mcpa.Rent(name));
@@ -245,7 +246,7 @@ namespace Cryville.Crtr.Event {
Children[c].InvalidateMotion(name);
}
public Vector GetRawValue(Identifier key) {
public Vector GetRawValue(int key) {
MotionCache tr;
if (!CachedValues.TryGetValue(key, out tr))
CachedValues.Add(key, tr = _mcpa.Rent(key));
@@ -263,11 +264,11 @@ namespace Cryville.Crtr.Event {
return r;
}
public T GetRawValue<T>(Identifier key) where T : Vector {
public T GetRawValue<T>(int key) where T : Vector {
return (T)GetRawValue(key);
}
static readonly Identifier n_pt = new Identifier("pt");
static readonly int n_pt = IdentifierManager.SharedInstance.Request("pt");
public Vector2 ScreenPoint {
get {
var mv = GetRawValue<VecPt>(n_pt);
@@ -275,7 +276,7 @@ namespace Cryville.Crtr.Event {
}
}
static readonly Identifier n_dir = new Identifier("dir");
static readonly int n_dir = IdentifierManager.SharedInstance.Request("dir");
public Vector3 Direction {
get {
Vec3 r = GetRawValue<Vec3>(n_dir);
@@ -283,7 +284,7 @@ namespace Cryville.Crtr.Event {
}
}
static readonly Identifier n_normal = new Identifier("normal");
static readonly int n_normal = IdentifierManager.SharedInstance.Request("normal");
public Vector3 Normal {
get {
Vec3 r = GetRawValue<Vec3>(n_normal);
@@ -297,8 +298,8 @@ namespace Cryville.Crtr.Event {
}
}
static readonly Identifier n_sv = new Identifier("sv");
static readonly Identifier n_svm = new Identifier("svm");
static readonly int n_sv = IdentifierManager.SharedInstance.Request("sv");
static readonly int n_svm = IdentifierManager.SharedInstance.Request("svm");
public float ScrollVelocity {
get {
return GetRawValue<VecPtComp>(n_sv).ToFloat(ChartPlayer.hitRect)
@@ -306,7 +307,7 @@ namespace Cryville.Crtr.Event {
}
}
static readonly Identifier n_dist = new Identifier("dist");
static readonly int n_dist = IdentifierManager.SharedInstance.Request("dist");
public float Distance {
get {
var mv = GetRawValue<VecPtComp>(n_dist);
@@ -314,15 +315,15 @@ namespace Cryville.Crtr.Event {
}
}
static readonly Identifier n_corner = new Identifier("corner");
static readonly int n_corner = IdentifierManager.SharedInstance.Request("corner");
public bool Corner {
get {
return GetRawValue<VecI1>(n_corner).Value % 2 >= 1;
}
}
static readonly Identifier n_ctrl0 = new Identifier("ctrl0");
static readonly Identifier n_ctrl1 = new Identifier("ctrl1");
static readonly int n_ctrl0 = IdentifierManager.SharedInstance.Request("ctrl0");
static readonly int n_ctrl1 = IdentifierManager.SharedInstance.Request("ctrl1");
public Vector3 GetControlPoint(bool alt1, float deltaz) {
var mv = GetRawValue<VecCtrl>(alt1 ? n_ctrl1 : n_ctrl0);
if (alt1 && mv.IsZero()) {
@@ -331,7 +332,7 @@ namespace Cryville.Crtr.Event {
return mv.ToVector3(ChartPlayer.hitRect, deltaz);
}
static readonly Identifier n_track = new Identifier("track");
static readonly int n_track = IdentifierManager.SharedInstance.Request("track");
public float Track {
get {
return GetRawValue<Vec1>(n_track).Value;
@@ -357,9 +358,9 @@ namespace Cryville.Crtr.Event {
if (ev != null) {
if (ev.Unstamped is Chart.Motion) {
var tev = (Chart.Motion)ev.Unstamped;
var mv = _rmvpa.Rent(tev.Name);
var mv = _rmvpa.Rent(tev.Name.Key);
mv.CloneTypeFlag = CloneType;
GetMotionValue(tev.Name).CopyTo(mv);
GetMotionValue(tev.Name.Key).CopyTo(mv);
PlayingMotions.Add(ev, mv);
Update(ev);
if (!ev.Unstamped.IsLong) {
@@ -407,8 +408,8 @@ namespace Cryville.Crtr.Event {
foreach (var m in PlayingMotions) {
var tev = (Chart.Motion)m.Key.Unstamped;
if (tev.RelativeNode != null && CloneType == 2) continue;
var value = GetMotionValue(tev.Name/*, true*/);
InvalidateMotion(tev.Name);
var value = GetMotionValue(tev.Name.Key/*, true*/);
InvalidateMotion(tev.Name.Key);
if (m.Key.Duration == 0) {
if (tev.RelativeNode != null) {
value.SetRelativeNode(tev.RelativeNode);
@@ -428,7 +429,7 @@ namespace Cryville.Crtr.Event {
tev.AbsoluteValue.LerpWith(m.Value.AbsoluteValue, lerpedTime, ref value.AbsoluteValue);
}
}
Values[tev.Name] = value;
Values[tev.Name.Key] = value;
}
}

View File

@@ -1,5 +1,6 @@
using Cryville.Common;
using Cryville.Common.Buffers;
using Cryville.Common.Collections.Specialized;
using System.Collections.Generic;
namespace Cryville.Crtr.Event {
@@ -11,7 +12,7 @@ namespace Cryville.Crtr.Event {
Value.CopyTo(dest.Value);
}
}
internal class MotionCachePool : CategorizedPool<Identifier, MotionCache> {
internal class MotionCachePool : CategorizedPool<int, MotionCache> {
private class Bucket : ObjectPool<MotionCache> {
readonly MotionRegistry _reg;
public Bucket(Identifier name, int capacity) : base(capacity) {
@@ -26,12 +27,12 @@ namespace Cryville.Crtr.Event {
obj.Valid = false;
}
}
readonly Dictionary<Identifier, ObjectPool<MotionCache>> m_buckets;
protected override IReadOnlyDictionary<Identifier, ObjectPool<MotionCache>> Buckets { get { return m_buckets; } }
readonly IntKeyedDictionary<ObjectPool<MotionCache>> m_buckets;
protected override IReadOnlyDictionary<int, ObjectPool<MotionCache>> Buckets { get { return m_buckets; } }
public MotionCachePool() {
m_buckets = new Dictionary<Identifier, ObjectPool<MotionCache>>(ChartPlayer.motionRegistry.Count);
m_buckets = new IntKeyedDictionary<ObjectPool<MotionCache>>(ChartPlayer.motionRegistry.Count);
foreach (var reg in ChartPlayer.motionRegistry)
m_buckets.Add(reg.Key, new Bucket(reg.Key, 4096));
m_buckets.Add(reg.Key.Key, new Bucket(reg.Key, 4096));
}
}
}

View File

@@ -1,9 +1,10 @@
using Cryville.Common;
using Cryville.Common.Buffers;
using Cryville.Common.Collections.Specialized;
using System.Collections.Generic;
namespace Cryville.Crtr.Event {
internal class RMVPool : CategorizedPool<Identifier, RealtimeMotionValue> {
internal class RMVPool : CategorizedPool<int, RealtimeMotionValue> {
private class Bucket : ObjectPool<RealtimeMotionValue> {
readonly MotionRegistry _reg;
public Bucket(Identifier name, int capacity) : base(capacity) {
@@ -13,12 +14,12 @@ namespace Cryville.Crtr.Event {
return new RealtimeMotionValue().Init(_reg.InitValue);
}
}
readonly Dictionary<Identifier, ObjectPool<RealtimeMotionValue>> m_buckets;
protected override IReadOnlyDictionary<Identifier, ObjectPool<RealtimeMotionValue>> Buckets { get { return m_buckets; } }
readonly IntKeyedDictionary<ObjectPool<RealtimeMotionValue>> m_buckets;
protected override IReadOnlyDictionary<int, ObjectPool<RealtimeMotionValue>> Buckets { get { return m_buckets; } }
public RMVPool() {
m_buckets = new Dictionary<Identifier, ObjectPool<RealtimeMotionValue>>(ChartPlayer.motionRegistry.Count);
m_buckets = new IntKeyedDictionary<ObjectPool<RealtimeMotionValue>>(ChartPlayer.motionRegistry.Count);
foreach (var reg in ChartPlayer.motionRegistry)
m_buckets.Add(reg.Key, new Bucket(reg.Key, 4096));
m_buckets.Add(reg.Key.Key, new Bucket(reg.Key, 4096));
}
}
}