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

StackSpace: use std::aligned_alloc() #907

Merged
merged 1 commit into from
May 28, 2024
Merged

Conversation

chfast
Copy link
Member

@chfast chfast commented May 28, 2024

Use std::aligned_alloc() (and _aligned_malloc() on MSVC) to allocate the memory for EVM stack.

This removes unnecessary initialization of the memory to zero (closes #403).

The memory alignment is set to 32 bytes (as previously) but allows transitioning to "over-aligned" allocation for additional optimization
(see #781).

Copy link

codecov bot commented May 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.26%. Comparing base (eef3fe1) to head (6779073).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #907   +/-   ##
=======================================
  Coverage   98.26%   98.26%           
=======================================
  Files         131      131           
  Lines       15709    15715    +6     
=======================================
+ Hits        15437    15443    +6     
  Misses        272      272           
Flag Coverage Δ
ethereum-tests 27.84% <100.00%> (+0.01%) ⬆️
ethereum-tests-silkpre 19.62% <100.00%> (+0.01%) ⬆️
execution-spec-tests 18.99% <100.00%> (+0.03%) ⬆️
unittests 94.14% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
lib/evmone/execution_state.hpp 97.05% <100.00%> (+0.28%) ⬆️


/// The storage allocated for maximum possible number of items.
/// Items are aligned to 256 bits for better packing in cache lines.
std::unique_ptr<uint256, Deleter> m_stack_space{allocate()};
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd put the allocate() call in a constructor.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added constructor.

Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

/// Returns the pointer to the "bottom", i.e. below the stack space.
[[nodiscard, clang::no_sanitize("bounds")]] uint256* bottom() noexcept
{
return m_stack_space - 1;
return m_stack_space.get() - 1;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why does it need to go outside the memory bounds?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is the convention we use currently: we keep the pointer to the top element on the stack. Therefore its starting position (stack empty) is one element below the allocated memory.

Use `std::aligned_alloc()` (and `_aligned_malloc()` on MSVC)
to allocate the memory for EVM stack.

This removes unnecessary initialization of the memory to zero
(closes #403).

The memory alignment is set to 32 bytes (as previously)
but allows transitioning to "over-aligned" allocation for additional
optimization
(see #781).
@chfast chfast force-pushed the stack_space_aligned_alloc branch from a8b779c to 6779073 Compare May 28, 2024 12:52
@chfast chfast merged commit 1398e88 into master May 28, 2024
27 checks passed
@chfast chfast deleted the stack_space_aligned_alloc branch May 28, 2024 13:14
public:
/// The maximum number of EVM stack items.
static constexpr auto limit = 1024;

StackSpace() noexcept : m_stack_space{allocate()} {}
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if

std::unique_ptr<uint256[]>(new (std::align_val_t(sizeof(uint256)) uint256[limit]);

achieves the same.

Copy link
Member Author

Choose a reason for hiding this comment

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

It looks like, however this specifies the array element alignment, so I wonder what happens if you specify 64 for uint256. I plan to align the allocation to 2048*32.

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.

Implement stack space as uninitialized memory
3 participants