From d501a1cf717cdfacd04e2de6074d4d16b326cee5 Mon Sep 17 00:00:00 2001 From: goyuken Date: Fri, 13 Jun 2014 19:32:36 +0000 Subject: [PATCH] speed up overall load speed of N64 text savestates (uncommonly used) by 25% --- BizHawk.Common/Extensions/Extensions.cs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/BizHawk.Common/Extensions/Extensions.cs b/BizHawk.Common/Extensions/Extensions.cs index 6342694e21..d03ec15893 100644 --- a/BizHawk.Common/Extensions/Extensions.cs +++ b/BizHawk.Common/Extensions/Extensions.cs @@ -363,11 +363,22 @@ namespace BizHawk.Common } } - public static void ReadFromHexFast(this byte[] buffer, string hex) + public static unsafe void ReadFromHexFast(this byte[] buffer, string hex) { - for (int i = 0; i < buffer.Length && i * 2 < hex.Length; i++) + if (buffer.Length * 2 != hex.Length) + throw new Exception("Data size mismatch"); + int count = buffer.Length; + fixed (byte* _dst = buffer) + fixed (char* _src = hex) { - buffer[i] = (byte)(Hex2Int(hex[i * 2]) * 16 + Hex2Int(hex[i * 2 + 1])); + byte* dst = _dst; + char* src = _src; + while (count > 0) + { + // in my tests, replacing Hex2Int() with a 256 entry LUT slowed things down slightly + *dst = (byte)(Hex2Int(*src++) << 4 | Hex2Int(*src++)); + count--; + } } }