Skip to content

Commit 17b3334

Browse files
authored
Merge pull request #4042 from planetarium/prepare/5.5.2
Decrease HashNodeCache size & ActivitySource as singleton
2 parents 2f8dac9 + 5e1cf39 commit 17b3334

File tree

6 files changed

+25
-15
lines changed

6 files changed

+25
-15
lines changed

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ Version 5.5.2
66

77
To be released.
88

9+
### Optimize memory usage
10+
11+
- Decrease `HashNodeCache` size
12+
- `ActivitySource` as singleton
13+
914

1015
Version 5.5.1
1116
-------------

src/Libplanet.Action/State/AccountState.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ namespace Libplanet.Action.State
1515
/// </summary>
1616
public class AccountState : IAccountState
1717
{
18+
private static readonly ActivitySource ActivitySource =
19+
new ("Libplanet.Action.State");
20+
1821
private readonly ITrie _trie;
19-
private readonly ActivitySource _activitySource;
2022

2123
public AccountState(ITrie trie)
2224
{
2325
_trie = trie;
24-
_activitySource = new ActivitySource("Libplanet.Action.State");
2526
}
2627

2728
/// <inheritdoc cref="IAccountState.Trie"/>
@@ -30,7 +31,7 @@ public AccountState(ITrie trie)
3031
/// <inheritdoc cref="IAccountState.GetState"/>
3132
public IValue? GetState(Address address)
3233
{
33-
using Activity? a = _activitySource
34+
using Activity? a = ActivitySource
3435
.StartActivity(ActivityKind.Internal)?
3536
.AddTag("Address", address.ToString());
3637
return Trie.Get(ToStateKey(address));

src/Libplanet.Action/State/WorldBaseState.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ namespace Libplanet.Action.State
1717
/// </summary>
1818
public class WorldBaseState : IWorldState
1919
{
20+
private static readonly ActivitySource ActivitySource =
21+
new ("Libplanet.Action.WorldBaseState");
22+
2023
private readonly IStateStore _stateStore;
21-
private readonly ActivitySource _activitySource;
2224

2325
public WorldBaseState(ITrie trie, IStateStore stateStore)
2426
{
@@ -27,7 +29,6 @@ public WorldBaseState(ITrie trie, IStateStore stateStore)
2729
Version = trie.GetMetadata() is { } value
2830
? value.Version
2931
: 0;
30-
_activitySource = new ActivitySource("Libplanet.Action.WorldBaseState");
3132
}
3233

3334
/// <inheritdoc cref="IWorldState.Trie"/>
@@ -42,7 +43,7 @@ public WorldBaseState(ITrie trie, IStateStore stateStore)
4243
/// <inheritdoc cref="IWorldState.GetAccountState"/>
4344
public IAccountState GetAccountState(Address address)
4445
{
45-
using Activity? a = _activitySource
46+
using Activity? a = ActivitySource
4647
.StartActivity(ActivityKind.Internal)?
4748
.AddTag("Address", address.ToString());
4849
if (Legacy)

src/Libplanet.Net/Transports/NetMQTransport.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ namespace Libplanet.Net.Transports
2727
/// </summary>
2828
public class NetMQTransport : ITransport
2929
{
30+
private static readonly ActivitySource ActivitySource =
31+
new ("Libplanet.Net.Transports.NetMQTransport");
32+
3033
private readonly PrivateKey _privateKey;
3134
private readonly ILogger _logger;
3235
private readonly AppProtocolVersionOptions _appProtocolVersionOptions;
@@ -36,7 +39,6 @@ public class NetMQTransport : ITransport
3639
private readonly Channel<MessageRequest> _requests;
3740
private readonly Task _runtimeProcessor;
3841
private readonly AsyncManualResetEvent _runningEvent;
39-
private readonly ActivitySource _activitySource;
4042

4143
private NetMQQueue<(AsyncManualResetEvent, Message)>? _replyQueue;
4244

@@ -94,7 +96,6 @@ private NetMQTransport(
9496
_requests = Channel.CreateUnbounded<MessageRequest>();
9597
_runtimeCancellationTokenSource = new CancellationTokenSource();
9698
_turnCancellationTokenSource = new CancellationTokenSource();
97-
_activitySource = new ActivitySource("Libplanet.Net.Transports.NetMQTransport");
9899
_requestCount = 0;
99100
CancellationToken runtimeCt = _runtimeCancellationTokenSource.Token;
100101
_runtimeProcessor = Task.Factory.StartNew(
@@ -323,7 +324,7 @@ CancellationToken cancellationToken
323324
throw new ObjectDisposedException(nameof(NetMQTransport));
324325
}
325326

326-
using Activity? a = _activitySource
327+
using Activity? a = ActivitySource
327328
.StartActivity(ActivityKind.Producer)?
328329
.AddTag("Message", content.Type)
329330
.AddTag("Peer", peer.PeerString);
@@ -504,7 +505,7 @@ public void BroadcastMessage(IEnumerable<BoundPeer> peers, MessageContent conten
504505
Task.Run(
505506
async () =>
506507
{
507-
using Activity? a = _activitySource
508+
using Activity? a = ActivitySource
508509
.StartActivity(ActivityKind.Producer)?
509510
.AddTag("Message", content.Type)
510511
.AddTag("Peers", boundPeers.Select(x => x.PeerString));

src/Libplanet.Store/HashNodeCache.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ namespace Libplanet.Store
1414
public class HashNodeCache
1515
{
1616
// FIXME: Tuned to 9c mainnet. Should be refactored to accept cache size as an argument.
17-
private const int _cacheSize = 524_288;
17+
private const int _cacheSize = 100_000;
1818

1919
private ICache<HashDigest<SHA256>, IValue> _cache;
2020

2121
internal HashNodeCache()
2222
{
2323
_cache = new ConcurrentLruBuilder<HashDigest<SHA256>, IValue>()
2424
.WithMetrics()
25+
.WithConcurrencyLevel(Environment.ProcessorCount)
2526
.WithExpireAfterAccess(TimeSpan.FromMinutes(10))
2627
.WithCapacity(_cacheSize)
2728
.Build();

src/Libplanet/Blockchain/BlockChainStates.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,22 @@ namespace Libplanet.Blockchain
1515
/// </summary>
1616
public class BlockChainStates : IBlockChainStates
1717
{
18+
private static readonly ActivitySource ActivitySource =
19+
new ("Libplanet.Blockchain.BlockChainStates");
20+
1821
private readonly IStore _store;
1922
private readonly IStateStore _stateStore;
20-
private readonly ActivitySource _activitySource;
2123

2224
public BlockChainStates(IStore store, IStateStore stateStore)
2325
{
2426
_store = store;
2527
_stateStore = stateStore;
26-
_activitySource = new ActivitySource("Libplanet.Blockchain.BlockChainStates");
2728
}
2829

2930
/// <inheritdoc cref="IBlockChainStates.GetWorldState(BlockHash)"/>
3031
public IWorldState GetWorldState(BlockHash offset)
3132
{
32-
using Activity? a = _activitySource
33+
using Activity? a = ActivitySource
3334
.StartActivity(ActivityKind.Internal)?
3435
.AddTag("BlockHash", offset.ToString());
3536
return new WorldBaseState(GetTrie(offset), _stateStore);
@@ -65,7 +66,7 @@ public IWorldState GetWorldState(HashDigest<SHA256>? stateRootHash)
6566
/// </remarks>
6667
private ITrie GetTrie(BlockHash offset)
6768
{
68-
using Activity? a = _activitySource
69+
using Activity? a = ActivitySource
6970
.StartActivity(ActivityKind.Internal)?
7071
.AddTag("BlockHash", offset.ToString());
7172
if (_store.GetStateRootHash(offset) is { } stateRootHash)

0 commit comments

Comments
 (0)