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

Perf/spec binary search #3647

Merged
merged 5 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -42,6 +45,26 @@ public ChainHeadSpecProvider(ISpecProvider specProvider, IBlockFinder blockFinde

public long[] TransitionBlocks => _specProvider.TransitionBlocks;

public IReleaseSpec GetSpec() => GetSpec(_blockFinder.FindBestSuggestedHeader()?.Number ?? 0);
public IReleaseSpec GetSpec()
{
long headerNumber = _blockFinder.FindBestSuggestedHeader()?.Number ?? 0;
if (headerNumber == _lastHeader)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

potential race condition

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

{
IReleaseSpec releaseSpec = _headerSpec;
if (headerNumber == _lastHeader)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know the functionality so I need some clarification :)
GetSpec returns the best cached spec.
Does it have to be the latest? We don't have a monitor here, so it can change between ln54 and ln58. Which means we don't care if it's the latest. So if we don't care that it's the latest then we don't need that double check.

{
if (releaseSpec is not null)
{
return releaseSpec;
}
}
}

lock (_lock)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, what if headerNumber changed between ln50 and ln65?
We will overwrite _headerSpec with the previous one.

{
_lastHeader = headerNumber;
return _headerSpec = GetSpec(headerNumber);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary lambda allocation on GetSpec which is called very often

Copy link
Member Author

Choose a reason for hiding this comment

The 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;

Expand Down