Skip to content

Commit

Permalink
Merge pull request #3926 from greymistcube/chore/optimize-key-converter
Browse files Browse the repository at this point in the history
⚡ Optimized KeyConverters
  • Loading branch information
greymistcube authored Aug 21, 2024
2 parents df313ba + 2acd395 commit 93d548b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ To be released.
`BlockHash` in a given `BlockLocator`. [[#3913]]
- (Libplanet.Store) Optimized `HashNode.ToBencodex()` method.
[[#3922], [#3924]]
- (Libplanet.Store) Optimized internal conversions to `KeyBytes`. [[#3926]]

### Bug fixes

Expand All @@ -42,6 +43,7 @@ To be released.
[#3913]: https://github.com/planetarium/libplanet/pull/3913
[#3922]: https://github.com/planetarium/libplanet/issues/3922
[#3924]: https://github.com/planetarium/libplanet/pull/3924
[#3926]: https://github.com/planetarium/libplanet/pull/3926


Version 5.2.2
Expand Down
8 changes: 5 additions & 3 deletions src/Libplanet.Action/State/KeyConverters.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Immutable;
using System.Runtime.CompilerServices;
using Libplanet.Crypto;
using Libplanet.Store.Trie;
using Libplanet.Types.Assets;
Expand Down Expand Up @@ -43,7 +45,7 @@ public static KeyBytes ToStateKey(Address address)
buffer[i * 2 + 1] = _conversionTable[addressBytes[i] & 0xf];
}

return new KeyBytes(buffer);
return new KeyBytes(Unsafe.As<byte[], ImmutableArray<byte>>(ref buffer));
}

// $"_{ByteUtil.Hex(address.ByteArray)}_{ByteUtil.Hex(currency.Hash.ByteArray)}"
Expand All @@ -68,7 +70,7 @@ public static KeyBytes ToFungibleAssetKey(Address address, Currency currency)
buffer[offset + 2 + i * 2 + 1] = _conversionTable[currencyBytes[i] & 0xf];
}

return new KeyBytes(buffer);
return new KeyBytes(Unsafe.As<byte[], ImmutableArray<byte>>(ref buffer));
}

public static KeyBytes ToFungibleAssetKey(
Expand All @@ -90,7 +92,7 @@ public static KeyBytes ToTotalSupplyKey(Currency currency)
buffer[2 + i * 2 + 1] = _conversionTable[currencyBytes[i] & 0xf];
}

return new KeyBytes(buffer);
return new KeyBytes(Unsafe.As<byte[], ImmutableArray<byte>>(ref buffer));
}
}
}

0 comments on commit 93d548b

Please sign in to comment.