Skip to content

Commit

Permalink
Set the initial header list capacity to avoid resizing it while readi…
Browse files Browse the repository at this point in the history
…ng from disk
  • Loading branch information
Coding-Enthusiast committed Aug 14, 2024
1 parent a642aee commit 117a056
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions Src/Autarkysoft.Bitcoin/Blockchain/Chain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,20 @@ public Chain(IFileManager fileMan, IBlockVerifier blockVerifier, IConsensus c, I
Time = t ?? new ClientTime();

network = netType;
// TODO: find a better initial capacity
headerList = new List<BlockHeader>(1_000_000);

// Read Headers:
byte[] hadba = FileMan.ReadData(HeadersFile);
if (hadba is null || hadba.Length % BlockHeader.Size != 0)
byte[] hdrba = FileMan.ReadData(HeadersFile);
if (hdrba is null || hdrba.Length == 0 || hdrba.Length % BlockHeader.Size != 0)
{
headerList = new List<BlockHeader>(1_000_000);
// File doesn't exist or is corrupted
ResetHeaders();
}
else
{
int count = hadba.Length / BlockHeader.Size;
var stream = new FastStreamReader(hadba);
int count = hdrba.Length / BlockHeader.Size;
headerList = new List<BlockHeader>(count + 144);
var stream = new FastStreamReader(hdrba);
for (int i = 0; i < count; i++)
{
if (BlockHeader.TryDeserialize(stream, out BlockHeader res, out _))
Expand Down

0 comments on commit 117a056

Please sign in to comment.