Skip to content

Commit

Permalink
Further optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
greymistcube committed Nov 29, 2024
1 parent 5736872 commit 49ae37f
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/Libplanet.RocksDBStore/RocksDBStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ public partial class RocksDBStore : BaseStore
private readonly ReaderWriterLockSlim _rwBlockCommitLock;
private readonly ReaderWriterLockSlim _rwNextStateRootHashLock;
private readonly ReaderWriterLockSlim _rwEvidenceLock;
private bool _pruned = false;
private bool _disposed = false;
private object _chainForkDeleteLock = new object();
private LruCache<Guid, LruCache<(int, int?), List<BlockHash>>> _indexCache;
Expand Down Expand Up @@ -284,8 +283,6 @@ public RocksDBStore(
new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
_rwEvidenceLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);

_pruned = ListChainIds().Count() <= 1;

_blockDbCache = new LruCache<string, RocksDb>(dbConnectionCacheSize);
_blockDbCache.SetPreRemoveDataMethod(db =>
{
Expand All @@ -300,6 +297,8 @@ public RocksDBStore(
});
}

private bool IsPruned => ListChainIds().Count() <= 1;

public static bool MigrateChainDBFromColumnFamilies(string path)
{
var opt = new DbOptions();
Expand Down Expand Up @@ -1820,7 +1819,7 @@ private IEnumerable<BlockHash> IterateIndexes(
Guid chainId,
long offset,
long? limit,
bool includeDeleted) => _pruned
bool includeDeleted) => IsPruned
? IterateIndexesPruned(chainId, offset, limit, includeDeleted)
: IterateIndexesUnpruned(chainId, offset, limit, includeDeleted);

Expand All @@ -1836,15 +1835,25 @@ private IEnumerable<BlockHash> IterateIndexesPruned(
}

long count = 0;
long limitUpperBound = CountIndex(chainId) - offset;
long actualLimit = limit is { } l
? Math.Min(l, limitUpperBound)
: limitUpperBound;

if (actualLimit <= 0)
{
yield break;
}

foreach (BlockHash hash in IterateIndexesInnerPruned(chainId, offset))
{
if (count >= limit)
yield return hash;
count += 1;

if (count >= actualLimit)
{
yield break;
}

yield return hash;
count += 1;
}
}

Expand Down

0 comments on commit 49ae37f

Please sign in to comment.