Skip to content

Commit b357957

Browse files
Update Constants with cached type arrays in order to avoid allocations when patching plugins via DirectCallMethod
1 parent 4fe0cb1 commit b357957

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

src/Common/Constants.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.IO;
1+
using System;
2+
using System.IO;
23
using System.Text;
34
using System.Text.RegularExpressions;
45
using Oxide.Core;
@@ -42,5 +43,10 @@ internal static class Constants
4243
internal static readonly string IncludePath = Path.Combine(Interface.Oxide.PluginDirectory, "include");
4344
internal static readonly string CSharpPath = Path.Combine(Interface.Oxide.ExtensionDirectory, "Oxide.CSharp.dll");
4445
internal static readonly string MscorlibPath = Path.Combine(Interface.Oxide.ExtensionDirectory, "mscorlib.dll");
46+
47+
internal static readonly Type[] StringGetLengthTypeArray = [];
48+
internal static readonly Type[] StringGetCharsTypeArray = [typeof(int)];
49+
internal static readonly Type[] StringIsNullOrEmptyTypeArray = [typeof(string)];
50+
internal static readonly Type[] StringEqualsTypeArray = [typeof(string)];
4551
}
4652
}

src/DirectCallMethod.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public class DirectCallMethod
2323
private readonly MethodReference _isNullOrEmpty;
2424
private readonly MethodReference _stringEquals;
2525
private readonly string _hookAttribute;
26-
private readonly Dictionary<Instruction, Node> _jumpToEdgePlaceholderTargets = new();
27-
private readonly List<Instruction> _jumpToEndPlaceholders = new();
28-
private readonly Dictionary<string, MethodDefinition> _hookMethods = new();
26+
private readonly Dictionary<Instruction, Node> _jumpToEdgePlaceholderTargets;
27+
private readonly List<Instruction> _jumpToEndPlaceholders;
28+
private readonly Dictionary<string, MethodDefinition> _hookMethods;
2929

3030
public DirectCallMethod(ModuleDefinition module, TypeDefinition type, AssemblyDefinition baseAssembly)
3131
{
@@ -46,6 +46,10 @@ public DirectCallMethod(ModuleDefinition module, TypeDefinition type, AssemblyDe
4646

4747
_hookAttribute = typeof(HookMethodAttribute).FullName;
4848

49+
_jumpToEdgePlaceholderTargets = new Dictionary<Instruction, Node>();
50+
_jumpToEndPlaceholders = new List<Instruction>();
51+
_hookMethods = new Dictionary<string, MethodDefinition>();
52+
4953
// Copy method definition from base class
5054
ModuleDefinition baseModule = baseAssembly.MainModule;
5155
TypeDefinition baseType = module.ImportReference(baseAssembly.MainModule.GetType("Oxide.Plugins.CSharpPlugin")).Resolve();

0 commit comments

Comments
 (0)