Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvements #5

Merged
merged 2 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions Warcraft.NET/Extensions/ExtendedIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -645,15 +645,25 @@ public static bool SeekChunk(this BinaryReader reader, string chunkSignature, bo

try
{
var foundChuckSignature = reader.ReadBinarySignature(reverseSignature);
while (foundChuckSignature != chunkSignature)
var foundChunkSignature = reader.ReadBinarySignature(reverseSignature);
while (foundChunkSignature != chunkSignature)
{
var size = reader.ReadUInt32();

// Return if we are about to seek outside of range
if ((reader.BaseStream.Position + size) > reader.BaseStream.Length)
return false;

reader.BaseStream.Position += size;
foundChuckSignature = reader.ReadBinarySignature(reverseSignature);

// Return if we're done reading
if (reader.BaseStream.Position == reader.BaseStream.Length)
return false;

foundChunkSignature = reader.ReadBinarySignature(reverseSignature);
}

if (foundChuckSignature == chunkSignature)
if (foundChunkSignature == chunkSignature)
{
if (!skipSignature)
reader.BaseStream.Position -= sizeof(uint);
Expand Down
17 changes: 16 additions & 1 deletion Warcraft.NET/Extensions/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
Expand All @@ -7,6 +8,8 @@ namespace Warcraft.NET.Extensions
{
public static class ReflectionExtensions
{
private static ConcurrentDictionary<string, MethodInfo> extensionMethodCache = new();

/// <summary>
/// Get all extension methods by Assembly
/// </summary>
Expand All @@ -27,7 +30,19 @@ where m.GetParameters()[0].ParameterType == type

public static MethodInfo GetExtensionMethod(this Type type, Assembly extensionsAssembly, string name)
{
return type.GetExtensionMethods(extensionsAssembly).FirstOrDefault(m => m.Name == name);
// This is a bit dirty, but it allows us to cache the MethodInfo for future use as reflecting is very expensive.
var uniqueKey = type.ToString() + "-" + extensionsAssembly.ToString() + "-" + name;

if (extensionMethodCache.TryGetValue(uniqueKey, out var cachedMethodInfo))
{
return cachedMethodInfo;
}
else
{
var methodInfo = type.GetExtensionMethods(extensionsAssembly).FirstOrDefault(m => m.Name == name);
extensionMethodCache.TryAdd(uniqueKey, methodInfo);
return methodInfo;
}
}

/// <summary>
Expand Down
Loading