Skip to content

Conversation

@maxkozlovsky
Copy link
Contributor

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

Copilot AI review requested due to automatic review settings October 27, 2025 22:16
Copy link

Copilot AI left a 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_allocator for efficient SharedPtr allocation with variable-sized objects
  • Adds make_shared and deserialize_node_from_buffer_shared functions 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.

Comment on lines 538 to 540
for (size_t i = 0; i < 16; ++i) {
MONAD_DEBUG_ASSERT(
!std::ranges::contains(children, i, &ChildData::branch) ||
Copy link

Copilot AI Oct 27, 2025

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.

Copilot uses AI. Check for mistakes.
buffer.size() - buffer_off);
}
else {
static_assert(false);
Copy link

Copilot AI Oct 27, 2025

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\").

Copilot uses AI. Check for mistakes.
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
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.

2 participants