Skip to content

Commit 1f14464

Browse files
committed
Undo perf improvements for customs access.
1 parent 8a5b0dd commit 1f14464

File tree

3 files changed

+58
-85
lines changed

3 files changed

+58
-85
lines changed

source/CustomComponents/CCLight/Database.cs

Lines changed: 39 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,56 +8,42 @@ namespace CustomComponents;
88

99
public class Database
1010
{
11-
internal static bool AddCustom(string identifier, object target, ICustom cc)
11+
internal static T GetCustom<T>(object target)
1212
{
13-
ref var customs = ref DefToCustoms(target);
14-
return AddCustomInternal(identifier, ref customs, cc);
15-
}
16-
private static ref object[] DefToCustoms(object target)
17-
{
18-
switch (target)
13+
var identifier = Identifier(target);
14+
15+
if (identifier == null)
1916
{
20-
case MechComponentDef def1:
21-
return ref def1.ccCustoms;
22-
case MechDef def2:
23-
return ref def2.ccCustoms;
24-
case ChassisDef def3:
25-
return ref def3.ccCustoms;
26-
case VehicleChassisDef def4:
27-
return ref def4.ccCustoms;
28-
default:
29-
throw new ArgumentException();
17+
return default;
3018
}
31-
}
3219

33-
internal static bool AddCustom(string identifier, ref object[] customs, ICustom cc)
34-
{
35-
return AddCustomInternal(identifier, ref customs, cc);
36-
}
20+
if (!Customs.TryGetValue(identifier, out var customs))
21+
{
22+
return default;
23+
}
3724

38-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
39-
internal static IEnumerable<T> GetCustoms<T>(object[] customs)
40-
{
41-
return GetCustomsInternal<T>(customs);
42-
}
25+
for (var index = 0; index < customs.Length; index++)
26+
{
27+
if (customs[index] is T csT)
28+
{
29+
return csT;
30+
}
31+
}
4332

44-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
45-
internal static T GetCustom<T>(object[] customs)
46-
{
47-
return GetCustomInternalFast<T>(customs);
33+
return default;
4834
}
4935

5036
[MethodImpl(MethodImplOptions.AggressiveInlining)]
51-
internal static bool Is<T>(object[] customs, out T value)
37+
internal static bool Is<T>(object target, out T value)
5238
{
53-
value = GetCustom<T>(customs);
39+
value = GetCustom<T>(target);
5440
return value != null;
5541
}
5642

5743
[MethodImpl(MethodImplOptions.AggressiveInlining)]
58-
internal static bool Is<T>(object[] customs)
44+
internal static bool Is<T>(object target)
5945
{
60-
return GetCustom<T>(customs) != null;
46+
return GetCustom<T>(target) != null;
6147
}
6248

6349
internal static string Identifier(object target)
@@ -77,10 +63,16 @@ internal static string Identifier(object target)
7763
};
7864
}
7965

80-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
81-
private static IEnumerable<T> GetCustomsInternal<T>(object[] customs)
66+
internal static IEnumerable<T> GetCustoms<T>(object target)
8267
{
83-
if (customs == null)
68+
var identifier = Identifier(target);
69+
70+
if (identifier == null)
71+
{
72+
yield break;
73+
}
74+
75+
if (!Customs.TryGetValue(identifier, out var customs))
8476
{
8577
yield break;
8678
}
@@ -94,28 +86,18 @@ private static IEnumerable<T> GetCustomsInternal<T>(object[] customs)
9486
}
9587
}
9688

97-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
98-
private static T GetCustomInternalFast<T>(object[] customs)
89+
private static readonly Dictionary<string, object[]> Customs = new(StringComparer.Ordinal);
90+
internal static bool AddCustom(object target, ICustom cc)
9991
{
100-
if (customs == null)
101-
{
102-
return default;
103-
}
104-
105-
for (var index = 0; index < customs.Length; index++)
92+
var identifier = Identifier(target);
93+
if (identifier == null)
10694
{
107-
if (customs[index] is T csT)
108-
{
109-
return csT;
110-
}
95+
return false;
11196
}
11297

113-
return default;
114-
}
98+
Customs.TryGetValue(identifier, out var customs);
11599

116-
private static bool AddCustomInternal(string identifier, ref object[] customs, ICustom cc)
117-
{
118-
Log.CCLoading.Trace?.Log($"{nameof(AddCustomInternal)} identifier={identifier} cc={cc} cc.type={cc.GetType()}");
100+
Log.CCLoading.Trace?.Log($"{nameof(AddCustom)} identifier={identifier} cc={cc} cc.type={cc.GetType()}");
119101

120102
var attribute = GetAttributeByType(cc.GetType());
121103
Log.CCLoading.Trace?.Log($"--{nameof(CustomComponentAttribute)} {nameof(attribute.Name)}={attribute.Name} {nameof(attribute.AllowArray)}={attribute.AllowArray}");
@@ -137,14 +119,14 @@ private static bool AddCustomInternal(string identifier, ref object[] customs, I
137119
Log.CCLoading.Trace?.Log("--added");
138120
if (customs == null)
139121
{
140-
customs = [cc];
122+
Customs[identifier] = [cc];
141123
}
142124
else
143125
{
144126
var newCustoms = new object[customs.Length + 1];
145127
Array.Copy(customs, newCustoms, customs.Length);
146128
newCustoms[customs.Length] = cc;
147-
customs = newCustoms;
129+
Customs[identifier] = newCustoms;
148130
}
149131
return true;
150132
}

source/CustomComponents/CCLight/Extensions.cs

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,25 @@ public static class MechComponentDefExtensions
1414
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1515
public static T GetComponent<T>(this MechComponentDef target)
1616
{
17-
return Database.GetCustom<T>(target.ccCustoms);
17+
return Database.GetCustom<T>(target);
1818
}
1919

2020
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2121
public static IEnumerable<T> GetComponents<T>(this MechComponentDef target)
2222
{
23-
return Database.GetCustoms<T>(target.ccCustoms);
23+
return Database.GetCustoms<T>(target);
2424
}
2525

2626
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2727
public static bool Is<T>(this MechComponentDef target, out T res)
2828
{
29-
// strongly used during combat/ai due to ME ignore_damage
30-
// TODO migrate combat related flags to dedicated SimpleInjector fields
31-
// requires changes in calling mods!
32-
if (typeof(T) == typeof(Flags))
33-
{
34-
res = (T)target.ccFlags;
35-
return res != null;
36-
}
37-
38-
return Database.Is(target.ccCustoms, out res);
29+
return Database.Is(target, out res);
3930
}
4031

4132
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4233
public static bool Is<T>(this MechComponentDef target)
4334
{
44-
return Database.Is<T>(target.ccCustoms);
35+
return Database.Is<T>(target);
4536
}
4637

4738
public static T AddComponent<T>(this MechComponentDef target, T component) where T : ICustom
@@ -50,7 +41,7 @@ public static T AddComponent<T>(this MechComponentDef target, T component) where
5041
{
5142
simple.Def = target;
5243
}
53-
Database.AddCustom(target.Description.Id, ref target.ccCustoms, component);
44+
Database.AddCustom(target, component);
5445
return component;
5546
}
5647

@@ -70,25 +61,25 @@ public static class VehicleExtentions
7061
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7162
public static T GetComponent<T>(this VehicleChassisDef target)
7263
{
73-
return Database.GetCustom<T>(target.ccCustoms);
64+
return Database.GetCustom<T>(target);
7465
}
7566

7667
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7768
public static IEnumerable<T> GetComponents<T>(this VehicleChassisDef target)
7869
{
79-
return Database.GetCustoms<T>(target.ccCustoms);
70+
return Database.GetCustoms<T>(target);
8071
}
8172

8273
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8374
public static bool Is<T>(this VehicleChassisDef target, out T res)
8475
{
85-
return Database.Is(target.ccCustoms, out res);
76+
return Database.Is(target, out res);
8677
}
8778

8879
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8980
public static bool Is<T>(this VehicleChassisDef target)
9081
{
91-
return Database.Is<T>(target.ccCustoms);
82+
return Database.Is<T>(target);
9283
}
9384
}
9485

@@ -97,25 +88,25 @@ public static class MechDefExtensions
9788
[MethodImpl(MethodImplOptions.AggressiveInlining)]
9889
public static T GetComponent<T>(this MechDef target)
9990
{
100-
return Database.GetCustom<T>(target.ccCustoms);
91+
return Database.GetCustom<T>(target);
10192
}
10293

10394
[MethodImpl(MethodImplOptions.AggressiveInlining)]
10495
public static IEnumerable<T> GetComponents<T>(this MechDef target)
10596
{
106-
return Database.GetCustoms<T>(target.ccCustoms);
97+
return Database.GetCustoms<T>(target);
10798
}
10899

109100
[MethodImpl(MethodImplOptions.AggressiveInlining)]
110101
public static bool Is<T>(this MechDef target, out T res)
111102
{
112-
return Database.Is(target.ccCustoms, out res);
103+
return Database.Is(target, out res);
113104
}
114105

115106
[MethodImpl(MethodImplOptions.AggressiveInlining)]
116107
public static bool Is<T>(this MechDef target)
117108
{
118-
return Database.Is<T>(target.ccCustoms);
109+
return Database.Is<T>(target);
119110
}
120111

121112
public static bool IsBroken(this MechDef def)
@@ -159,25 +150,25 @@ public static class ChassisDefExtensions
159150
[MethodImpl(MethodImplOptions.AggressiveInlining)]
160151
public static T GetComponent<T>(this ChassisDef target)
161152
{
162-
return Database.GetCustom<T>(target.ccCustoms);
153+
return Database.GetCustom<T>(target);
163154
}
164155

165156
[MethodImpl(MethodImplOptions.AggressiveInlining)]
166157
public static IEnumerable<T> GetComponents<T>(this ChassisDef target)
167158
{
168-
return Database.GetCustoms<T>(target.ccCustoms);
159+
return Database.GetCustoms<T>(target);
169160
}
170161

171162
[MethodImpl(MethodImplOptions.AggressiveInlining)]
172163
public static bool Is<T>(this ChassisDef target, out T res)
173164
{
174-
return Database.Is(target.ccCustoms, out res);
165+
return Database.Is(target, out res);
175166
}
176167

177168
[MethodImpl(MethodImplOptions.AggressiveInlining)]
178169
public static bool Is<T>(this ChassisDef target)
179170
{
180-
return Database.Is<T>(target.ccCustoms);
171+
return Database.Is<T>(target);
181172
}
182173

183174
public static T AddComponent<T>(this ChassisDef target, T component) where T : ICustom
@@ -186,7 +177,7 @@ public static T AddComponent<T>(this ChassisDef target, T component) where T : I
186177
{
187178
simple.Def = target;
188179
}
189-
Database.AddCustom(target.Description.Id, ref target.ccCustoms, component);
180+
Database.AddCustom(target, component);
190181
return component;
191182
}
192183

source/CustomComponents/CCLight/Registry.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ private static void ProcessCustomFactory(
189189

190190
Log.CCLoading.Trace?.Log($"Created {component} for {identifier}");
191191

192-
if (Database.AddCustom(identifier, target, component))
192+
if (Database.AddCustom(target, component))
193193
{
194194
if (component is IAfterLoad load)
195195
{

0 commit comments

Comments
 (0)