Skip to content

Commit

Permalink
Merge pull request #5 from Marlamin/master
Browse files Browse the repository at this point in the history
Performance improvements
  • Loading branch information
Luzifix committed Jul 9, 2024
2 parents 0b0942b + 7695ed9 commit e6b2c8b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
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

0 comments on commit e6b2c8b

Please sign in to comment.