170#ifndef __ADS_ConcurrentFlatTable
171 #define __ADS_ConcurrentFlatTable
192#include <type_traits>
197 #include <boost/atomic/atomic_ref.hpp>
221template< std::
unsigned_
integral S = std::u
int32_t >
222 requires( ! std::same_as< S , bool > )
239 using Stamp = std::uint8_t;
240 static constexpr bool epochs =
false;
244 requires( ! std::same_as< S , bool > )
245struct ClearStamps< EpochClear< S > >
248 static constexpr bool epochs =
true;
259 std::same_as< C , ZeroingClear > || detail::ClearStamps< C >::epochs;
268template<
class K ,
class V ,
class H = DefaultHash< K > ,
269 class Eq = std::equal_to< K > ,
class C = ZeroingClear >
274 "the policy of clear() is ZeroingClear or EpochClear" );
277 static constexpr std::size_t valueBytes(
void ) {
278 return( std::is_empty_v< V > ? 0 :
sizeof( V ) );
282 static constexpr bool plainKey(
void ) {
283 return( std::has_unique_object_representations_v< K > ||
284 std::is_floating_point_v< K > );
294 static constexpr bool epochs = detail::ClearStamps< C >::epochs;
296 static_assert( !
epochs || ( std::is_trivially_destructible_v< K > &&
297 std::is_trivially_destructible_v< V > ) ,
298 "the epochs drop the pairs with no destructor" );
303 std::is_trivially_copyable_v< K > && std::is_trivially_copyable_v< V > &&
304 plainKey() && (
sizeof( K ) + valueBytes() <= 8 );
312 std::is_trivially_copyable_v< V > && plainKey() && (
sizeof( K ) <= 8 ) &&
313 (
sizeof( V ) <= 8 );
325 using Ref = std::conditional_t< packed || inlined , V , Entry & >;
340 [[nodiscard]] std::uint64_t
hash(
const K & k )
const;
417 requires(
packed && ( valueBytes() == 0 ) );
421 bool insert(
const K & k ,
const V & v );
444 [[nodiscard]] std::optional< V >
find(
const K & k , std::uint64_t h )
const;
447 [[nodiscard]] std::optional< V >
find(
const K & k )
const;
462 [[nodiscard]] std::optional< K >
findKey(
const K & k ,
463 std::uint64_t h )
const;
466 [[nodiscard]] std::optional< K >
findKey(
const K & k )
const;
470 [[nodiscard]]
bool contains(
const K & k , std::uint64_t h )
const;
484 [[nodiscard]]
Entry *
entry(
const K & k , std::uint64_t h )
const
496 [[nodiscard]] std::size_t
size(
void )
const;
499 [[nodiscard]]
bool empty(
void )
const {
return(
size() == 0 ); }
521 [[nodiscard]] std::uint64_t
epoch(
void )
const {
return( now() ); }
535 static constexpr int kLeast = 6;
536 static constexpr int kStripes = 64;
537 static constexpr int kProbe = 128;
539 static constexpr int kBlock = 4096;
540 static constexpr int kSlab = 64;
541 static constexpr int kLgSeg = 12;
542 static constexpr int kSegs = 29;
543 static constexpr int kFp = 40;
545 using Stamp =
typename detail::ClearStamps< C >::Stamp;
548 static constexpr Stamp kBusy = std::numeric_limits< Stamp >::max();
555 using Stamps = std::conditional_t< epochs , std::atomic< Stamp > * ,
559 [[nodiscard]] Stamp now(
void )
const {
577 struct alignas( 64 ) Stripe
580 std::atomic< std::uint64_t > w{ 0 };
589 std::atomic< std::uint64_t > * W;
593 std::atomic< std::uint64_t > * G;
598 std::atomic< Table * > next{
nullptr };
600 std::atomic< std::size_t > claimed{ 0 } , copied{ 0 };
602 Table(
int l , Stamp e );
610 [[nodiscard]] std::size_t places(
void )
const {
611 return( std::size_t( 1 ) << lg );
614 [[nodiscard]] std::size_t groups(
void )
const {
return( places() >> 3 ); }
616 [[nodiscard]] std::uint8_t tagAt( std::size_t i )
const {
617 return( std::uint8_t( G[ i >> 3 ].
load( std::memory_order_acquire ) >>
618 ( 8 * ( i & 7 ) ) ) );
621 [[nodiscard]] std::size_t blocks(
void )
const {
622 return( ( places() + kBlock - 1 ) / kBlock );
630 std::uint64_t
id = 0;
632 std::uint64_t next = 0 , end = 0;
637 [[nodiscard]]
static std::uint64_t bits(
const X & x , std::size_t at );
645 static constexpr std::size_t kWords =
inlined ? 2 : 1;
648 [[nodiscard]]
static V inValue( std::uint64_t w );
653 static bool cas16( std::atomic< std::uint64_t > * p , std::uint64_t & k0 ,
654 std::uint64_t & v0 , std::uint64_t k1 , std::uint64_t v1 );
659 std::pair< std::uint64_t , bool > placeWord(
const K & k , std::uint64_t h ,
662 std::pair< Ref , bool > placeInline(
const K & k , std::uint64_t h ,
667 [[nodiscard]]
const std::atomic< std::uint64_t > *
668 locate(
const K & k , std::uint64_t h )
const;
671 [[nodiscard]]
static std::uint64_t pack(
const K & k ,
const V & v );
674 [[nodiscard]]
static K keyOf( std::uint64_t w );
677 [[nodiscard]]
static V valueOf( std::uint64_t w );
680 [[nodiscard]]
static std::uint64_t mark(
void );
683 [[nodiscard]]
static bool zeroKey(
const K & k );
686 [[nodiscard]]
static std::size_t home( std::uint64_t h ,
const Table & T );
689 [[nodiscard]]
static std::uint8_t tag( std::uint64_t h );
693 std::uint8_t tagged( Table & T , std::size_t i , std::uint64_t w )
const;
697 std::uint8_t tagWith( Table & T , std::size_t i , std::uint64_t h )
const;
700 [[nodiscard]]
static std::uint64_t print( std::uint64_t h );
703 [[nodiscard]] Node * node( std::uint64_t i )
const;
706 [[nodiscard]]
bool holds( std::uint64_t w ,
const K & k ,
707 std::uint64_t h )
const;
710 [[nodiscard]] std::uint64_t hashOf( std::uint64_t w )
const;
713 [[nodiscard]] std::uint64_t lookup(
const K & k , std::uint64_t h )
const;
716 [[nodiscard]] Stripe & stripe(
void )
const;
719 Table * enter( Stripe & s );
723 void segment(
int j ,
bool touch );
727 void nodes( std::size_t n );
730 std::uint64_t claim(
void );
734 void consume( std::uint64_t i );
737 [[nodiscard]]
static Slab * slabs(
void );
743 void copy( Table & T , Table & N );
746 void grow( Table * T ,
int lg );
750 [[nodiscard]]
static std::uint64_t period(
int lg );
753 [[nodiscard]] std::size_t counted(
void )
const;
757 [[nodiscard]]
bool live(
const Table & T , std::size_t g )
const;
782 void renew( Table & T , std::size_t g )
const;
786 [[nodiscard]] std::uint64_t wordAt(
const Table & T , std::size_t i )
const;
790 [[nodiscard]]
bool reached(
const Table & T , std::size_t i )
const;
792 std::unique_ptr< Stripe[] > f_S;
793 std::atomic< bool > f_grow{
false };
794 std::atomic< Table * > f_table{
nullptr };
795 std::vector< std::unique_ptr< Table > > f_old;
796 std::unique_ptr< Table > f_cur;
799 alignas( 16 ) std::atomic< std::uint64_t > f_zero[ 2 ];
800 std::atomic< Node * > f_seg[ kSegs ];
801 std::atomic< std::uint64_t > f_nodes{ 0 };
802 std::atomic< std::uint64_t > f_id;
803 std::atomic< std::size_t > f_rehashes{ 0 };
806 NoStamps > f_epoch{};
822#include "ADS/concurrent/impl/ConcurrentFlatTable.tpp"
The name of the configuration of the layouts, an inline namespace: the headers whose classes change t...
#define ADS_FLAT_CAS16
Definition Abi.h:78
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The engineered hash functions of ADS, beyond the book [see HashFunctions.h for those of section 7....
An allocator for the large blocks of a structure, beyond the book: a request of at least one huge pag...
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
std::optional< V > find(const K &k) const
find() above, with the hash computed
static constexpr bool epochs
< the policy of clear()
Definition ConcurrentFlatTable.h:294
bool update(const K &k, F &&f)
sets the value of the key to f( x ), by a compare-and-swap of its word (of its place),...
std::optional< V > find(const K &k, std::uint64_t h) const
a copy of the value of the key of hash , or nothing
~ConcurrentFlatTable()
the frees of the pairs and of the vectors
V Value
< the type of the keys
Definition ConcurrentFlatTable.h:290
std::pair< Ref, bool > findOrInsert(const K &k, std::uint64_t h, const V &v)
the pair of the key , of hash , inserted with the value if absent; true if it was inserted
void clear(void)
removes every pair, keeping the capacity; with no other operation running.
static constexpr bool packed
true if the pairs lie in the words: the key and the value trivially copyable, the bytes of the key al...
Definition ConcurrentFlatTable.h:302
C Clear
< the type of the values
Definition ConcurrentFlatTable.h:291
bool contains(const K &k) const
contains() above, with the hash computed
K Key
Definition ConcurrentFlatTable.h:289
std::uint64_t epoch(void) const
the epoch of the table, raised by each clear() and back to 1 after the largest; always 1 without Epoc...
Definition ConcurrentFlatTable.h:521
ConcurrentFlatTable & operator=(const ConcurrentFlatTable &)=delete
no copy, since the threads share it by its address
std::optional< K > findKey(const K &k, std::uint64_t h) const
a copy of the key held equal to by Eq, of hash , or nothing; never inserts
ConcurrentFlatTable(const ConcurrentFlatTable &)=delete
no copy, since the threads share it by its address
bool insert(const K &k, const V &v)
inserts the pair if is absent; true if it was, as findOrInsert()
std::pair< K, bool > findOrInsertKey(const K &k, std::uint64_t h)
the key held equal to , of hash , which is itself if absent and then inserted; true if it was insert...
std::size_t rehashes(void) const
the number of the growths of the table so far
void reserve(std::size_t n)
room for pairs before the table grows: the vector grown to places at least, as by an insertion,...
static constexpr bool inlined
true if the pairs lie in places of two words written by one compare-and-swap of 16 bytes: not packed,...
Definition ConcurrentFlatTable.h:310
ConcurrentFlatTable(std::size_t n=0, H h=H(), Eq eq=Eq())
the empty table, with room for pairs (and their nodes) before it grows, the hash h and the equality ...
std::pair< Ref, bool > findOrInsert(const K &k, const V &v)
findOrInsert() above, with the hash computed
Entry * entry(const K &k, std::uint64_t h) const
the pair of the key of hash , or nil, as find(); for a table neither packed nor inlined
std::size_t capacity(void) const
the number of the places of the vector in use
std::size_t size(void) const
the number of the pairs, the sum of the counters, exact when no operation runs
void forEach(F &&f) const
applies f( const K & , const V & ) to every pair; with no insertion running
bool consistent(void) const
each key once, reached from its place without a free place in between, and the counters summing to th...
bool empty(void) const
whether size() is 0
Definition ConcurrentFlatTable.h:499
std::uint64_t hash(const K &k) const
the hash of 64 bits of the key that the operations take
std::conditional_t< packed||inlined, V, Entry & > Ref
what an operation returns of the pair held: a copy of the value if packed or inlined,...
Definition ConcurrentFlatTable.h:325
bool contains(const K &k, std::uint64_t h) const
whether the key of hash is there, as find()
std::optional< K > findKey(const K &k) const
findKey() above, with the hash computed
void prefetch(std::uint64_t h) const
the lines that a lookup of the hash reads first (its tags and its word) fetched into the cache,...
a policy of clear() of ConcurrentFlatTable
Definition ConcurrentFlatTable.h:258
#define ADS_ABI_BEGIN
opens the inline namespace of the configuration
Definition Abi.h:106
#define ADS_ABI_END
closes the inline namespace of the configuration
Definition Abi.h:111
#define ADS_NO_UNIQUE_ADDRESS
an empty member that takes no storage, by the attribute that does so (the standard one is accepted bu...
Definition Portable.h:81
void load(Ar &a, T &x)
the structure read from the archive of cereal
Definition Cereal.h:238
std::vector< std::vector< int > > Table
the table of the costs
Definition ApproximateMatching.h:80
a pair of a table neither packed nor inlined, in a node that never moves
Definition ConcurrentFlatTable.h:318
const K key
the key
Definition ConcurrentFlatTable.h:319
V value
the value, the caller's to synchronize
Definition ConcurrentFlatTable.h:320
the clear() of ConcurrentFlatTable in by epochs: a stamp of the unsigned type per group of 8 places...
Definition ConcurrentFlatTable.h:224
S Stamp
Definition ConcurrentFlatTable.h:225
the clear() of ConcurrentFlatTable that writes 0 to every place, in time linear in its capacity; the ...
Definition ConcurrentFlatTable.h:216