Ultra 0.9.4
Loading...
Searching...
No Matches
ultra::cache< F, LOCK_GROUP_SIZE > Class Template Reference

#include <cache.h>

Public Member Functions

 cache (bitwidth)
bitwidth bits () const
void resize (bitwidth)
void clear () noexcept
void clear (const hash_t &)
void insert (const hash_t &, const F &)
std::optional< F > find (const hash_t &) const
bool is_valid () const
bool load (std::istream &)
bool save (std::ostream &) const

Detailed Description

template<Fitness F, std::size_t LOCK_GROUP_SIZE = 128>
class ultra::cache< F, LOCK_GROUP_SIZE >
Template Parameters
LOCK_GROUP_SIZEset to 1 for maximum granularity (one lock per slot) or to the size of the table for a single mutex covering all slots. The default value balances performance and thread safety for most use cases.

This class implements a hash table that maps individuals' signatures to their fitness. It's primarily used by the evaluator_proxy class to optimise performance.

During evolution, semantically equivalent but syntactically distinct individuals are often generated. By using this cache, the system avoids redundant computations of shared information, resulting in significant speed improvements.

Constructor & Destructor Documentation

◆ cache()

template<Fitness F, std::size_t LOCK_GROUP_SIZE>
cache::cache ( bitwidth n)
explicit

Creates a new, not empty, hash table.

Parameters
[in]n2^n is the number of elements of the table. A value of zero creates a valid disabled cache object. Operations requiring table indexing are still guarded and are expected to be bypassed by callers

Member Function Documentation

◆ bits()

template<Fitness F, std::size_t LOCK_GROUP_SIZE>
bitwidth cache::bits ( ) const
nodiscard
Returns
number of bits used for hash table initialization

◆ clear() [1/2]

template<Fitness F, std::size_t LOCK_GROUP_SIZE>
void cache::clear ( )
noexcept

Invalidates all cached entries without changing the allocated size.

Warning
Not concurrency-safe.
Note
Allocated size isn't changed.

◆ clear() [2/2]

template<Fitness F, std::size_t LOCK_GROUP_SIZE>
void cache::clear ( const hash_t & h)

Clears the cached information for a specific individual.

Parameters
[in]hindividual's signature whose information we have to clear

◆ find()

template<Fitness F, std::size_t LOCK_GROUP_SIZE>
std::optional< F > cache::find ( const hash_t & h) const
nodiscard

Looks for the fitness of an individual in the transposition table.

Parameters
[in]hindividual's signature to look for
Returns
the fitness of the individual. If the individuals isn't present returns an empty std::optional

◆ insert()

template<Fitness F, std::size_t LOCK_GROUP_SIZE>
void cache::insert ( const hash_t & h,
const F & fitness )

Stores fitness information in the transposition table.

Parameters
[in]ha (possibly) new individual's signature to be stored in the table
[in]fitnessthe fitness of the individual

◆ is_valid()

template<Fitness F, std::size_t LOCK_GROUP_SIZE>
bool cache::is_valid ( ) const
nodiscard
Returns
true if the object passes the internal consistency check
Warning
Not concurrency-safe.

◆ load()

template<Fitness F, std::size_t LOCK_GROUP_SIZE>
bool cache::load ( std::istream & in)
Parameters
[in]ininput stream
Returns
true if the object is correctly loaded
Remarks
Even if the load operation isn't successful, the current object might still change. A stronger guarantee would require twice the memory allocated for the cache.
Warning
Not concurrency-safe.

◆ resize()

template<Fitness F, std::size_t LOCK_GROUP_SIZE>
void cache::resize ( bitwidth n)

Resize the cache.

Parameters
[in]n2^n is the new number of elements of the table
Warning
  • This is a destructive operation: content of the cache will be lost.
  • Not concurrency-safe.

◆ save()

template<Fitness F, std::size_t LOCK_GROUP_SIZE>
bool cache::save ( std::ostream & out) const
Parameters
[out]outoutput stream
Returns
true if the object was saved correctly
Warning
Not concurrency-safe.

The documentation for this class was generated from the following files: