-
Notifications
You must be signed in to change notification settings - Fork 432
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
Perf/spec binary search #3647
Perf/spec binary search #3647
Changes from all commits
6fa5a67
84fd5da
cb715ca
d097c02
037d7da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,9 @@ public class ChainHeadSpecProvider : IChainHeadSpecProvider | |
{ | ||
private readonly ISpecProvider _specProvider; | ||
private readonly IBlockFinder _blockFinder; | ||
private long _lastHeader = -1; | ||
private IReleaseSpec? _headerSpec = null; | ||
private readonly object _lock = new(); | ||
|
||
public ChainHeadSpecProvider(ISpecProvider specProvider, IBlockFinder blockFinder) | ||
{ | ||
|
@@ -42,6 +45,28 @@ public ChainHeadSpecProvider(ISpecProvider specProvider, IBlockFinder blockFinde | |
|
||
public long[] TransitionBlocks => _specProvider.TransitionBlocks; | ||
|
||
public IReleaseSpec GetSpec() => GetSpec(_blockFinder.FindBestSuggestedHeader()?.Number ?? 0); | ||
public IReleaseSpec GetCurrentHeadSpec() | ||
{ | ||
long headerNumber = _blockFinder.FindBestSuggestedHeader()?.Number ?? 0; | ||
|
||
// we are fine with potential concurrency issue here, that the spec will change | ||
// between this if and getting actual header spec | ||
// this is used only in tx pool and this is not a problem there | ||
if (headerNumber == _lastHeader) | ||
{ | ||
IReleaseSpec releaseSpec = _headerSpec; | ||
if (releaseSpec is not null) | ||
{ | ||
return releaseSpec; | ||
} | ||
} | ||
|
||
// we want to make sure updates to both fields are consistent though | ||
lock (_lock) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, what if headerNumber changed between ln50 and ln65? |
||
{ | ||
_lastHeader = headerNumber; | ||
return _headerSpec = GetSpec(headerNumber); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Nethermind.Core.Collections; | ||
using Nethermind.Core.Specs; | ||
using Nethermind.Int256; | ||
|
||
|
@@ -170,28 +171,15 @@ private void BuildTransitions() | |
|
||
public IReleaseSpec GenesisSpec => _transitions.Length == 0 ? null : _transitions[0].Release; | ||
|
||
public IReleaseSpec GetSpec(long blockNumber) | ||
{ | ||
if (_transitions.Length == 0) | ||
{ | ||
return null; | ||
} | ||
public IReleaseSpec GetSpec(long blockNumber) => | ||
_transitions.TryGetSearchedItem(blockNumber, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unnecessary lambda allocation on GetSpec which is called very often There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed by static method |
||
CompareTransitionOnBlock, | ||
out (long BlockNumber, IReleaseSpec Release) transition) | ||
? transition.Release | ||
: null; | ||
|
||
IReleaseSpec spec = _transitions[0].Release; | ||
for (int i = 1; i < _transitions.Length; i++) | ||
{ | ||
if (blockNumber >= _transitions[i].BlockNumber) | ||
{ | ||
spec = _transitions[i].Release; | ||
} | ||
else | ||
{ | ||
break; | ||
} | ||
} | ||
|
||
return spec; | ||
} | ||
private static int CompareTransitionOnBlock(long blockNumber, (long BlockNumber, IReleaseSpec Release) transition) => | ||
blockNumber.CompareTo(transition.BlockNumber); | ||
|
||
public long? DaoBlockNumber => _chainSpec.DaoForkBlockNumber; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
potential race condition
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed