1
0
forked from Alepha/Alepha

Blob based per-thread slab allocator

This permits "stateless" allocators which grab memory from a
`thread_local Alepha::Blob` instance.  Each allocation
sticks a malloc cookie of type
`std::shared_ptr< Alepha::Blob::StorageReservation >`
just before the base of the allocation.

The allocator object knows that it needs to `reinterpret_cast`
the malloc cookie into a shared pointer and run its destructor.
This causes the Blob's underlying reference counted allocation
to be tied to the lifetime of the allocated memory.  The intent
is to permit cheap allocation in one thread and deallocation
in another.  Each deallocation should be a single atomic
dereference operation.  Each allocation should be (usually) a
bit of pointer arithmetic and a single atomic increment operation.

This, hopefully, eliminates significant thread contention for
the global allocation mechanism between various threads in
an intensive multithreaded situation where each processing
thread thread may independently retire data objects allocated by
a single source.
This commit is contained in:
2024-09-05 17:06:52 -04:00
parent d4dfe9f90f
commit 6c165b1603
6 changed files with 191 additions and 3 deletions

View File

@ -17,6 +17,8 @@ static_assert( __cplusplus > 2020'99 );
#include "Buffer.h" #include "Buffer.h"
// TODO: Put this into the `Alepha::Memory::` namespace.
// TODO: Consider whether a "republish" into `Alepha::` namespace is a good idea.
namespace Alepha::Hydrogen ::detail:: Blob_m namespace Alepha::Hydrogen ::detail:: Blob_m
{ {
inline namespace exports inline namespace exports
@ -94,6 +96,9 @@ namespace Alepha::Hydrogen ::detail:: Blob_m
public: public:
~Blob() { reset(); } ~Blob() { reset(); }
using StorageReservation= IndirectStorage;
const StorageReservation &reservation() const { return storage; }
auto auto
swap_lens() noexcept swap_lens() noexcept
{ {
@ -218,7 +223,10 @@ namespace Alepha::Hydrogen ::detail:: Blob_m
* inside a large single physical backing. This helps maintain zero-copy semantics. * inside a large single physical backing. This helps maintain zero-copy semantics.
* *
* @param amount The amount of data to carve off. * @param amount The amount of data to carve off.
* @return A new `Blob` object referring to the same physical data, scoped to `amount` bytes. * @param alignment The size alignment that the new base should be at (the extra padding is
* considered part of the resulting `Blob`).
* @return A new `Blob` object referring to the same physical data, scoped to `amount` bytes (with
* possible extra space, due to alignment).
*/ */
Blob Blob
carveHead( const std::size_t amount ) carveHead( const std::size_t amount )

View File

@ -1 +1,2 @@
add_subdirectory( Blob.test ) add_subdirectory( Blob.test )
add_subdirectory( ThreadSlab.test )

View File

@ -52,7 +52,7 @@ namespace Alepha::inline Cavorite ::detail:: DataChain_m
friend DataChain; friend DataChain;
explicit Iterator( const ChainIter pos, cosnt std::size_t offset ) noexcept : pos( pos ), offset( offset ) {} explicit Iterator( const ChainIter pos, const std::size_t offset ) noexcept : pos( pos ), offset( offset ) {}
public: public:
auto auto
@ -165,7 +165,7 @@ namespace Alepha::inline Cavorite ::detail:: DataChain_m
std::copy_n( std::prev( end(), amount ), amount, rv.byte_data() ); std::copy_n( std::prev( end(), amount ), amount, rv.byte_data() );
return rv; return rv;
}
}; };
}; };
} }

129
Memory/ThreadSlab.h Normal file
View File

@ -0,0 +1,129 @@
static_assert( __cplusplus > 2020'99 );
#pragma once
#include <Alepha/Alepha.h>
#include <string>
#include <Alepha/Memory/Blob.h>
namespace Alepha::Hydrogen::Memory ::detail:: ThreadSlab_m
{
inline namespace exports
{
template< typename T >
class ThreadSlab;
using ThreadSlabString= std::basic_string< char, std::char_traits< char >, ThreadSlab< char > >;
}
namespace C
{
const std::size_t slabSize= 64 * 1024 * 1024;
const bool debug= false;
const bool debugLifecycle= false or C::debug;
const bool debugAllocation= false or C::debug;
const bool debugDeallocation= false or C::debug;
}
template< typename T >
class exports::ThreadSlab
{
public:
inline static thread_local Blob slab;
public:
using value_type= T;
using propagate_on_container_copy_assignment= std::true_type;
using propagate_on_container_move_assignment= std::true_type;
using propagate_on_container_swap= std::true_type;
using is_always_equal= std::true_type;
ThreadSlab select_on_container_copy_construction() { auto rv= ThreadSlab{}; }
ThreadSlab()= default;
ThreadSlab &operator= ( const ThreadSlab &other )= default;
ThreadSlab( const ThreadSlab &other )= default;
ThreadSlab( ThreadSlab &&other ) : ThreadSlab( std::as_const( other ) ) {}
~ThreadSlab()
{
if( C::debugLifecycle )
{
std::cerr << "Reporting " << slab.reservation().use_count() << " living allocations when "
<< (void *) this << " is retired." << std::endl;
}
}
[[nodiscard]] T *
allocate( const std::size_t amt )
{
// TODO: Alignment needs to be handled.
const std::size_t req= amt + sizeof( Blob::StorageReservation );
// TODO: Larger allocations may be worth bespoke allocations, if they're rare one-off cases
if( req > C::slabSize ) throw std::bad_alloc{}; //{ "Unable to allocate larger than the slab size." };
if( slab.size() < req ) slab.reset( std::max( req, C::slabSize ) );
if( C::debugAllocation )
{
std::cerr << "Reporting " << slab.reservation().use_count() << " living allocations when "
<< (void *) this << " made an allocation." << std::endl;
}
auto next= slab.carveHead( req + sizeof( Blob::StorageReservation ) );
const auto rv= reinterpret_cast< T * >( &next.template as< Blob::StorageReservation >() + 1 );
// FIXME: The placement new here is potentially unaligned -- this may significantly impact
// performance.
new ( &next.template as< Blob::StorageReservation >() ) Blob::StorageReservation{ std::move( next.reservation() ) };
if( C::debugAllocation )
{
std::cerr << "Reporting " << slab.reservation().use_count() << " living allocations when "
<< (void *) this << " made an allocation." << std::endl;
}
return rv;
}
template< typename SP >
static void
destroy( SP *p )
{
p->~SP();
}
void
deallocate( T *const p, const std::size_t /* ignored */ ) noexcept
{
if( C::debugDeallocation )
{
std::cerr << "Reporting " << slab.reservation().use_count() << " living allocations when "
<< (void *) this << " made a deallocation." << std::endl;
}
auto *const hidden= reinterpret_cast< Blob::StorageReservation * >( p ) - 1;
destroy( hidden );
if( C::debugDeallocation )
{
std::cerr << "Reporting " << slab.reservation().use_count() << " living allocations when "
<< (void *) this << " made a deallocation." << std::endl;
}
}
friend constexpr bool operator == ( const ThreadSlab &, const ThreadSlab & ) noexcept { return true; }
};
}
namespace Alepha::Hydrogen::Memory::inline exports::inline ThreadSlab_m
{
using namespace detail::ThreadSlab_m::exports;
}

View File

@ -0,0 +1,49 @@
static_assert( __cplusplus > 2020'99 );
#include "../ThreadSlab.h"
#include <Alepha/Testing/test.h>
#include <Alepha/Utility/evaluation_helpers.h>
static auto init= Alepha::Utility::enroll <=[]
{
using namespace Alepha::Testing::literals;
using namespace Alepha::Memory::exports::ThreadSlab_m;
using String= ThreadSlabString;
"Check slab usage"_test <=[]
{
std::cout << "I see " << Alepha::Memory::ThreadSlab< char >::slab.reservation().use_count() << " reservations in a separate test." <<
std::endl;
};
"Can we work with simple `ThreadSlabStrings` without errors?"_test <=[]
{
String s;
std::cerr << "s is empty" << std::endl;
String s2= "Hello World";
std::cerr << "small hello world string." << std::endl;
String s3= s2 + ": and bob";
for( int i= 0; i < 10; ++i )
{
std::cerr << "appended..." << std::endl;
s3= s3 + s3 + s2;
s2= std::move( s3 );
}
std::cout << s3 << std::endl;
};
"Check slab usage"_test <=[]
{
std::cout << "I see " << Alepha::Memory::ThreadSlab< char >::slab.reservation().use_count() << " reservations in a separate test." <<
std::endl;
};
};

View File

@ -0,0 +1 @@
unit_test( 0 )