Skip to content

Commit aaebbf3

Browse files
committed
Reuse StringBuilder to get noteworthy performance speedup.
1 parent 4e304b2 commit aaebbf3

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

Source/Reloaded.Injector/Interop/ModuleCollector.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace Reloaded.Injector.Interop
1010
{
1111
internal static unsafe class ModuleCollector
1212
{
13+
private static StringBuilder _modulePathBuilder = new StringBuilder(32767);
14+
1315
/// <exception cref="DllInjectorException">Bytes to fill module list returned 0. The process is probably not yet initialized.</exception>
1416
public static List<Module> CollectModules(Process process)
1517
{
@@ -18,7 +20,6 @@ public static List<Module> CollectModules(Process process)
1820
int numberOfModules;
1921
int bytesNeeded;
2022

21-
2223
// Determine number of modules.
2324
if (!EnumProcessModulesEx(process.Handle, modulePointers, 0, out bytesNeeded, (uint)ModuleFilter.ListModulesAll))
2425
return collectedModules;
@@ -34,14 +35,13 @@ public static List<Module> CollectModules(Process process)
3435
{
3536
for (int x = 0; x < numberOfModules; x++)
3637
{
37-
StringBuilder modulePathBuilder = new StringBuilder(32767);
3838
ModuleInformation moduleInformation = new ModuleInformation();
3939

40-
GetModuleFileNameEx(process.Handle, modulePointers[x], modulePathBuilder, (uint)(modulePathBuilder.Capacity));
40+
GetModuleFileNameEx(process.Handle, modulePointers[x], _modulePathBuilder, (uint)(_modulePathBuilder.Capacity));
4141
GetModuleInformation(process.Handle, modulePointers[x], out moduleInformation, (uint)sizeof(ModuleInformation));
4242

4343
// Convert to a normalized module and add it to our list
44-
string modulePath = modulePathBuilder.ToString();
44+
string modulePath = _modulePathBuilder.ToString();
4545
Module module = new Module(modulePath, moduleInformation.lpBaseOfDll, moduleInformation.SizeOfImage, moduleInformation.EntryPoint);
4646
collectedModules.Add(module);
4747
}

Source/Reloaded.Injector/Reloaded.Injector.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<RepositoryUrl>https://github.com/Reloaded-Project/Reloaded.Injector</RepositoryUrl>
1414
<RepositoryType>git</RepositoryType>
1515
<GenerateDocumentationFile>true</GenerateDocumentationFile>
16-
<Version>1.2.1</Version>
16+
<Version>1.2.2</Version>
1717
<Copyright>LGPLV3</Copyright>
1818
<PackageLicenseFile>LICENSE</PackageLicenseFile>
1919
</PropertyGroup>

0 commit comments

Comments
 (0)