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

Hunting allocations 3 - introduce AccountStruct #6605

Merged
merged 20 commits into from
Jan 26, 2024

Conversation

LukaszRozmej
Copy link
Member

Changes

  • Introduces AccountStruct to reduce allocations

Types of changes

What types of changes does your code introduce?

  • Bugfix (a non-breaking change that fixes an issue)
  • New feature (a non-breaking change that adds functionality)
  • Breaking change (a change that causes existing functionality not to work as expected)
  • Optimization
  • Refactoring
  • Documentation update
  • Build-related changes
  • Other: Description

Testing

Requires testing

  • Yes
  • No

If yes, did you write tests?

  • Yes
  • No

@LukaszRozmej LukaszRozmej changed the title Hunting Allocations 3 - introduce AccountStruct Hunting allocations 3 - introduce AccountStruct Jan 24, 2024
Copy link
Member

@benaadams benaadams left a comment

Choose a reason for hiding this comment

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

You ike ternary operators very much 😄

@benaadams
Copy link
Member

Nethermind.State/StateTree.cs(47,32): error CS0029: Cannot implicitly convert type 'System.ReadOnlySpan' to 'System.Span'

@@ -8,7 +8,7 @@ namespace Nethermind.Core
{
public class Account
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah still exist?

Copy link
Member Author

Choose a reason for hiding this comment

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

There are places where we need to put it on heap, not sure if worth paying for boxing?

Copy link
Member Author

Choose a reason for hiding this comment

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

But will try to move some usages to AccountStruct in next PR.

@LukaszRozmej LukaszRozmej marked this pull request as ready for review January 26, 2024 14:25
@LukaszRozmej LukaszRozmej requested a review from asdacap January 26, 2024 14:42
Base automatically changed from optimize/allocations-2 to master January 26, 2024 15:08
# Conflicts:
#	src/Nethermind/Nethermind.Serialization.Json/EthereumJsonSerializer.cs
#	src/Nethermind/Nethermind.Serialization.Rlp/AccountDecoder.cs
#	src/Nethermind/Nethermind.State/StateReader.cs
#	src/Nethermind/Nethermind.State/StateTree.cs
@LukaszRozmej LukaszRozmej merged commit 4440998 into master Jan 26, 2024
67 checks passed
@LukaszRozmej LukaszRozmej deleted the optimize/allocations branch January 26, 2024 17:02
public AccountStruct ToStruct() => new(Nonce, Balance, StorageRoot, CodeHash);
}

public readonly struct AccountStruct
Copy link
Member

Choose a reason for hiding this comment

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

This is a 128 bytes struct (0.125 kB); want to make sure are passing by in etc everywhere otherwise big copies (if low GC)

Copy link
Contributor

@Scooletz Scooletz Jan 30, 2024

Choose a reason for hiding this comment

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

How this comment ended? I see that in lot of places this is returned, no TryGet or ref semantics there? I'm asking as I'm applying these changes to Paprika and it looks that we're potentially copying to much here?

@benaadams @LukaszRozmej

@@ -162,7 +162,7 @@ public ResultWrapper<IEnumerable<Address>> eth_accounts()
return Task.FromResult(GetStateFailureResult<UInt256?>(header));
}

Account account = _stateReader.GetAccount(header!.StateRoot!, address);
AccountStruct? account = _stateReader.GetAccount(header!.StateRoot!, address);
Copy link
Member

Choose a reason for hiding this comment

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

Does it have to be nullable? Extra byte it adds is problematic on copy size (128 bytes -> 129 bytes)

better to init with empy code hash and empty storage has and pass that back if null?

Copy link
Member Author

Choose a reason for hiding this comment

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

So been thinking about it, how to avoid copies to. Maybe the answer is something like:

bool TryGetAccount(root, address, ref AccountStruct account)

not sure if we could write directly to stack allocated by the calling method?

}
else
{
storageRoot = rlpStream.DecodeValueKeccak()!.Value;
Copy link
Member

Choose a reason for hiding this comment

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

if for some reason DecodeValueKeccak() returns null, the underlaying value will probably be a zero-value struct(?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants