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

Improve performance of BlockSet<T> #1013

Merged
merged 5 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ To be released.
- `BlockChain<T>` instead of `BlockPolicy<T>` became to validate `Block<T>`s to append
so that even if an empty implementation of `IBlockPolicy<T>` is used `Block<T>`s are
unable to be appended to `BlockChain<T>`. [[#1010]]
- Improved performance of `BlockSet<T>[HashDigest<SHA256>]` and `BlockChain<T>.Genesis`
by caching. [[#1013]]
dahlia marked this conversation as resolved.
Show resolved Hide resolved

### Bug fixes

Expand Down Expand Up @@ -359,6 +361,7 @@ To be released.
[#1004]: https://github.com/planetarium/libplanet/pull/1004
[#1010]: https://github.com/planetarium/libplanet/pull/1010
[#1012]: https://github.com/planetarium/libplanet/pull/1012
[#1013]: https://github.com/planetarium/libplanet/pull/1013
[sleep mode]: https://en.wikipedia.org/wiki/Sleep_mode


Expand Down
7 changes: 6 additions & 1 deletion Libplanet/Blockchain/BlockChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public class BlockChain<T>
/// </summary>
private IDictionary<TxId, Transaction<T>> _transactions;

/// <summary>
/// Cached genesis block.
/// </summary>
private Block<T> _genesis;
longfin marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Initializes a new instance of the <see cref="BlockChain{T}"/> class.
/// </summary>
Expand Down Expand Up @@ -235,7 +240,7 @@ public Block<T> Tip
/// <summary>
/// The first <see cref="Block{T}"/> in the <see cref="BlockChain{T}"/>.
/// </summary>
public Block<T> Genesis => this[0];
public Block<T> Genesis => _genesis ??= this[0];

public Guid Id { get; private set; }

Expand Down
36 changes: 33 additions & 3 deletions Libplanet/Store/BlockSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,29 @@
using System.Security.Cryptography;
using Libplanet.Action;
using Libplanet.Blocks;
using LruCacheNet;

namespace Libplanet.Store
{
public class BlockSet<T> : BaseIndex<HashDigest<SHA256>, Block<T>>
where T : IAction, new()
{
// FIXME it should be configurable
private const int DefaultCacheSize = 4096;
dahlia marked this conversation as resolved.
Show resolved Hide resolved
private readonly LruCache<HashDigest<SHA256>, Block<T>> _cache;

public BlockSet(IStore store)
: base(store)
{
_cache = new LruCache<HashDigest<SHA256>, Block<T>>(DefaultCacheSize);
}

public override ICollection<HashDigest<SHA256>> Keys =>
Store.IterateBlockHashes().ToList();

public override ICollection<Block<T>> Values =>
Store.IterateBlockHashes()
.Select(Store.GetBlock<T>)
.Select(GetBlock)
.ToList();

public override int Count => (int)Store.CountBlocks();
Expand All @@ -31,7 +37,7 @@ public override Block<T> this[HashDigest<SHA256> key]
{
get
{
Block<T> block = Store.GetBlock<T>(key);
Block<T> block = GetBlock(key);
if (block is null)
{
throw new KeyNotFoundException(
Expand All @@ -58,6 +64,7 @@ public override Block<T> this[HashDigest<SHA256> key]

value.Validate(DateTimeOffset.UtcNow);
Store.PutBlock(value);
_cache.Add(value.Hash, value);
}
}

Expand All @@ -74,7 +81,30 @@ public override bool ContainsKey(HashDigest<SHA256> key)

public override bool Remove(HashDigest<SHA256> key)
{
return Store.DeleteBlock(key);
bool deleted = Store.DeleteBlock(key);

if (deleted)
{
_cache.Remove(key);
longfin marked this conversation as resolved.
Show resolved Hide resolved
}
longfin marked this conversation as resolved.
Show resolved Hide resolved

return deleted;
}

private Block<T> GetBlock(HashDigest<SHA256> key)
{
if (_cache.TryGetValue(key, out Block<T> cached))
{
return cached;
}

Block<T> fetched = Store.GetBlock<T>(key);
if (!(fetched is null))
{
_cache.Add(key, fetched);
}

return fetched;
}
}
}