Skip to content

Conversation

@greg7mdp
Copy link
Contributor

@greg7mdp greg7mdp commented Dec 16, 2024

Resolves #1049.

Now this integrates the changes from AntelopeIO/chainbase#54, which can be referred to for previous comments.

As the issue describes, the current chainbase does a lot of small buffer allocations directly through the bip allocator which hits the segment manager directly. Not only this is inefficient (the rbtree of free buffers need to be rebalanced for every alloc/free), it also has a higher memory overhead.

The slab allocators for small buffers (in the updated chainbase submodule) batch allocations of small buffers into larger blocks, reducing the fragmentattion within the shared memory segment, and providing better efficiency and a smaller memory footprint.

For comparison, here are some results comparing snapshot loading on my linux box (release build)

snapshot from Aug 11, 2024

time (average) chainbase memory usage memory saving
main branch 55s 23.67GB -
this branch 28s 22.41GB 5.3%

snapshot from Sept 29, 2024

time (average) chainbase memory usage memory saving
main branch 392s 65.30GB -
this branch 331s 63.12GB 3.3%

I also expect a slight performance increase for processing transactions, as the _undo_stack deque allocations used to go through the segment allocator as well (for blocks of 16 undo_state), and these allocations are now batched.

@ericpassmore
Copy link
Contributor

Note:start
category: System Enhancements
component: Internal
summary: Improve chainbase to use utilize slab allocator for small buffers. Reduces memory contention and frequency of rebalancing.
Note:end

@greg7mdp greg7mdp changed the base branch from main to release/2.0 April 23, 2025 20:55
@greg7mdp greg7mdp mentioned this pull request Apr 24, 2025
@greg7mdp greg7mdp changed the title Use version of chainbase which implements a slab allocator for small size buffers. Update chaindb to use a slab allocator for small size buffers. Apr 28, 2025
@greg7mdp greg7mdp requested review from heifner and spoonincode April 28, 2025 19:56
_file_mapping = bip::file_mapping(_data_file_path.generic_string().c_str(), bip::read_write);
_file_mapped_region = bip::mapped_region(_file_mapping, bip::read_write);
file_mapped_segment_manager = new ((char*)_file_mapped_region.get_address()+header_size) segment_manager(shared_file_size-header_size);
new (_file_mapped_region.get_address()) db_header;
Copy link
Contributor

@heifner heifner Nov 17, 2025

Choose a reason for hiding this comment

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

This line must remain when merged.
The change from: 5153e29#diff-4598b34f65fa412d3263ad3b6d1d1711c29a7c1b7689ed659c63a5ab1cc0b682R141

Doesn't work with the small allocator.

if(hs.fail())
BOOST_THROW_EXCEPTION(std::system_error(make_error_code(db_error_code::bad_header)));

db_header* dbheader = reinterpret_cast<db_header*>(header);
Copy link
Contributor

Choose a reason for hiding this comment

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

This also must remain when merged.

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.

Create slab allocator for small allocs (< 512 bytes) to avoid hitting the segment allocator constantly

4 participants