-
Couldn't load subscription status.
- Fork 374
Optimize MPT node allocation with custom allocator and SharedPtr #1835
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
base: main
Are you sure you want to change the base?
Conversation
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.
Pull Request Overview
This PR optimizes memory allocation for MPT nodes by replacing UniquePtr-to-SharedPtr conversions with direct SharedPtr creation using a custom allocator. The optimization eliminates double allocations by allocating the shared_ptr control block and object in a single memory block.
- Introduces
variable_size_allocatorfor efficient SharedPtr allocation with variable-sized objects - Adds
make_sharedanddeserialize_node_from_buffer_sharedfunctions that directly create SharedPtr instances - Updates function signatures and call sites throughout the codebase to use SharedPtr instead of UniquePtr
Reviewed Changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| category/core/mem/allocators.hpp | Adds new variable_size_allocator template class for allocating variable-sized objects with std::allocate_shared |
| category/mpt/node.hpp | Adds make_shared static methods to Node and CacheNode classes, updates function signatures to return SharedPtr, adds deserialize_node_from_buffer_shared template function |
| category/mpt/node.cpp | Updates make_node, create_node_with_children, and ChildData::finalize to use SharedPtr, replaces Node::make calls with Node::make_shared |
| category/mpt/deserialize_node_from_receiver_result.hpp | Adds new deserialize_node_from_receiver_result_shared template function for direct SharedPtr deserialization |
| category/mpt/trie.cpp | Updates calls to use new _shared variants and changes return types to SharedPtr |
| category/mpt/copy_trie.cpp | Updates function signatures and variable types to use SharedPtr instead of UniquePtr |
| category/mpt/find_request_sender.hpp | Updates deserialization call to use new _shared variant |
| category/mpt/find_notify_fiber.cpp | Updates deserialization calls to use new _shared variant |
| category/mpt/db.cpp | Updates deserialization call to use new _shared variant |
| category/mpt/test/node_writer_test.cpp | Updates test helper function to return SharedPtr |
| category/mpt/test/node_test.cpp | Updates test node creation to use SharedPtr |
| category/mpt/test/node_lru_cache_test.cpp | Splits node creation into intermediate variable to work with new SharedPtr API |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| for (size_t i = 0; i < 16; ++i) { | ||
| MONAD_DEBUG_ASSERT( | ||
| !std::ranges::contains(children, i, &ChildData::branch) || |
Copilot
AI
Oct 27, 2025
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.
Removed debug assertions that validate data_size and value->size() constraints. These checks help catch issues early. Consider retaining them unless they're verified elsewhere in the call path.
| buffer.size() - buffer_off); | ||
| } | ||
| else { | ||
| static_assert(false); |
Copilot
AI
Oct 27, 2025
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.
Using static_assert(false) will fail compilation unconditionally. Use static_assert(false, \"message\") with a dependent false expression like static_assert(!sizeof(ResultType*), \"Unsupported ResultType\").
Replace UniquePtr→SharedPtr conversions with direct SharedPtr creation using std::allocate_shared and custom variable_size_allocator. This eliminates double memory allocations (separate control block and object) by allocating both in a single block. - Add variable_size_allocator for single-allocation SharedPtr creation - Create make_shared Node creation functions returning SharedPtr - Modify existing Node creation function to return SharedPtr Some functions now have both shared and unique ptr versions for optimal usage Partially generated using Claude Sonnet 4.5
b6dfbe4 to
1e170e8
Compare
Replace UniquePtr→SharedPtr conversions with direct SharedPtr creation using std::allocate_shared and custom variable_size_allocator. This eliminates double memory allocations (separate control block and object) by allocating both in a single block.
Partially generated using Claude Sonnet 4.5