Skip to content

Commit

Permalink
Reuse StringBuilder to get noteworthy performance speedup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed Jul 20, 2019
1 parent 4e304b2 commit aaebbf3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions Source/Reloaded.Injector/Interop/ModuleCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace Reloaded.Injector.Interop
{
internal static unsafe class ModuleCollector
{
private static StringBuilder _modulePathBuilder = new StringBuilder(32767);

/// <exception cref="DllInjectorException">Bytes to fill module list returned 0. The process is probably not yet initialized.</exception>
public static List<Module> CollectModules(Process process)
{
Expand All @@ -18,7 +20,6 @@ public static List<Module> CollectModules(Process process)
int numberOfModules;
int bytesNeeded;


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

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

// Convert to a normalized module and add it to our list
string modulePath = modulePathBuilder.ToString();
string modulePath = _modulePathBuilder.ToString();
Module module = new Module(modulePath, moduleInformation.lpBaseOfDll, moduleInformation.SizeOfImage, moduleInformation.EntryPoint);
collectedModules.Add(module);
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Reloaded.Injector/Reloaded.Injector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<RepositoryUrl>https://github.com/Reloaded-Project/Reloaded.Injector</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Version>1.2.1</Version>
<Version>1.2.2</Version>
<Copyright>LGPLV3</Copyright>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
</PropertyGroup>
Expand Down

0 comments on commit aaebbf3

Please sign in to comment.