Blender V4.3
blender::SharedCache< T > Class Template Reference

#include <BLI_shared_cache.hh>

Public Member Functions

 SharedCache ()
 
void tag_dirty ()
 
void ensure (FunctionRef< void(T &data)> compute_cache)
 
void update (FunctionRef< void(T &data)> compute_cache)
 
const Tdata () const
 
bool is_dirty () const
 
bool is_cached () const
 

Detailed Description

template<typename T>
class blender::SharedCache< T >

A SharedCache is meant to share lazily computed data between equivalent objects. It allows saving unnecessary computation by making a calculated value accessible from any object that shares the cache. Unlike CacheMutex, the cached data is embedded inside of this object.

When data is copied (copy-on-evaluation before changing a mesh, for example), the cache is shared, allowing its calculation on either the source or original to make the result available on both objects. As soon as either object is changed in a way that invalidates the cache, the data is "un-shared", and they will no-longer influence each other.

One important use case is a typical copy-on-evaluation update loop of a persistent geometry data-block in Main. Even if bounds are only calculated on the evaluated copied geometry, if nothing changes them, they only need to be calculated on the first evaluation, because the same evaluated bounds are also accessible from the original geometry.

The cache is implemented with a shared pointer, so it is relatively cheap, but to avoid unnecessary overhead it should only be used for relatively expensive computations.

Definition at line 29 of file BLI_shared_cache.hh.

Constructor & Destructor Documentation

◆ SharedCache()

template<typename T >
blender::SharedCache< T >::SharedCache ( )
inline

Definition at line 39 of file BLI_shared_cache.hh.

Member Function Documentation

◆ data()

template<typename T >
const T & blender::SharedCache< T >::data ( ) const
inline

Retrieve the cached data.

Definition at line 83 of file BLI_shared_cache.hh.

References BLI_assert.

◆ ensure()

template<typename T >
void blender::SharedCache< T >::ensure ( FunctionRef< void(T &data)> compute_cache)
inline

If the cache is dirty, trigger its computation with the provided function which should set the proper data.

Definition at line 60 of file BLI_shared_cache.hh.

◆ is_cached()

template<typename T >
bool blender::SharedCache< T >::is_cached ( ) const
inline

Return true if the cache exists and is valid.

Definition at line 100 of file BLI_shared_cache.hh.

◆ is_dirty()

template<typename T >
bool blender::SharedCache< T >::is_dirty ( ) const
inline

Return true if the cache currently does not exist or has been invalidated.

Definition at line 92 of file BLI_shared_cache.hh.

Referenced by blender::bke::pbvh::update_normals_mesh().

◆ tag_dirty()

template<typename T >
void blender::SharedCache< T >::tag_dirty ( )
inline

Tag the data for recomputation and stop sharing the cache with other objects.

Definition at line 46 of file BLI_shared_cache.hh.

Referenced by blender::bke::Instances::tag_reference_handles_changed().

◆ update()

template<typename T >
void blender::SharedCache< T >::update ( FunctionRef< void(T &data)> compute_cache)
inline

Represents a combination of "tag dirty" and "update cache for new data." Existing cached values are kept available (copied from shared data if necessary). This can be helpful when the recalculation is only expected to make a small change to the cached data, since using tag_dirty() and ensure() separately may require rebuilding the cache from scratch.

Definition at line 71 of file BLI_shared_cache.hh.

Referenced by blender::bke::pbvh::update_normals_mesh().


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