Bit faster
MODIFY
WalletWasabi/Backend/Models/FilterModel.cs
WalletWasabi/Backend/Models/FilterModel.cs
@@ -35,19 +35,26 @@ public class FilterModel
{
try
{
- string[] parts = line.Split(':');
+ // Splitting lines using Split(':') requires allocations. Working with .NET spans is faster.
+ ReadOnlySpan<char> span = line;
- if (parts.Length < 5)
+ int m1 = line.IndexOf(':', 0);
+ int m2 = line.IndexOf(':', m1 + 1);
+ int m3 = line.IndexOf(':', m2 + 1);
+ int m4 = line.IndexOf(':', m3 + 1);
+
+ if (m1 == -1 || m2 == -1 || m3 == -1 || m4 == -1)
{
throw new ArgumentException(line, nameof(line));
}
- uint blockHeight = uint.Parse(parts[0]);
- uint256 blockHash = uint256.Parse(parts[1]);
- byte[] filterData = Encoders.Hex.DecodeData(parts[2]);
+ uint blockHeight = uint.Parse(span[0..m1]);
+ uint256 blockHash = new(Convert.FromHexString(span[(m1 + 1)..m2]), lendian: false);
+ byte[] filterData = Convert.FromHexString(span[(m2 + 1)..m3]);
+
Lazy<GolombRiceFilter> filter = new(() => new GolombRiceFilter(filterData, 20, 1 << 20), LazyThreadSafetyMode.ExecutionAndPublication);
- uint256 prevBlockHash = uint256.Parse(parts[3]);
- long blockTime = long.Parse(parts[4]);
+ uint256 prevBlockHash = new(Convert.FromHexString(span[(m3 + 1)..m4]), lendian: false);
+ long blockTime = long.Parse(span[(m4 + 1)..]);
return new FilterModel(new SmartHeader(blockHash, prevBlockHash, blockHeight, blockTime), filter);
}

Bit faster (#9630)