Grok 20.3.2
grk::CompressedChunkCache Class Reference

Per-file LRU cache that manages compressed tile part data in memory. More...

#include <CompressedChunkCache.h>

Classes

struct  Entry

Public Member Functions

 CompressedChunkCache (size_t maxBytes=0, std::shared_ptr< DiskCache > diskCache=nullptr, GRK_CODEC_FORMAT codecFormat=GRK_CODEC_J2K)
 Construct the cache.
void put (uint16_t tileIndex, std::shared_ptr< TPFetchSeq > seq)
 Register a tile's fetched data with the cache.
std::shared_ptr< TPFetchSeqget (uint16_t tileIndex)
 Ensure a tile's compressed data is in memory.
bool contains (uint16_t tileIndex) const
 Check if a tile is tracked (memory or disk).
void clear ()
size_t currentBytes () const
size_t maxBytes () const
size_t size () const

Private Member Functions

void pushFrontLRU (uint16_t tileIndex)
void removeLRU (uint16_t tileIndex)
void promoteLRU (uint16_t tileIndex)
void evictToFit ()
void spillToDisk (uint16_t tileIndex, Entry &entry)
 Serialize tile part data to disk and release in-memory buffers.
bool reloadFromDisk (uint16_t tileIndex, Entry &entry)
 Reload tile part data from disk into the existing TPFetch objects.

Static Private Member Functions

static size_t calcSeqDataSize (const TPFetchSeq &seq)
static size_t resolveMaxBytes (size_t provided)
 Resolve max bytes from environment or use provided value.

Private Attributes

size_t maxBytes_
size_t currentBytes_
std::shared_ptr< DiskCachediskCache_
GRK_CODEC_FORMAT codecFormat_
std::mutex mutex_
std::list< uint16_t > lruList_
std::unordered_map< uint16_t, std::list< uint16_t >::iterator > lruMap_
std::unordered_map< uint16_t, Entryentries_

Static Private Attributes

static constexpr size_t kDefaultMaxBytes = 256 * 1024 * 1024

Detailed Description

Per-file LRU cache that manages compressed tile part data in memory.

Approach B (minimal): the cache does NOT duplicate TPFetch data. It holds shared ownership of each tile's TPFetchSeq and tracks total in-memory compressed bytes. When the high water mark is exceeded, the LRU tile's TPFetch::data_ buffers are serialized to the DiskCache and released. On reload, the buffers and mem-streams are recreated from disk.

Cache size is controlled by the GRK_CACHEMAX or GDAL_CACHEMAX environment variable. Defaults to 256 MB.

Constructor & Destructor Documentation

◆ CompressedChunkCache()

grk::CompressedChunkCache::CompressedChunkCache ( size_t maxBytes = 0,
std::shared_ptr< DiskCache > diskCache = nullptr,
GRK_CODEC_FORMAT codecFormat = GRK_CODEC_J2K )
inlineexplicit

Construct the cache.

Parameters
maxBytesmemory budget in bytes (0 = read from env)
diskCacheoptional disk cache for eviction spill
codecFormatcodec format used to recreate mem-streams on reload

References codecFormat_, currentBytes_, diskCache_, GRK_CODEC_J2K, maxBytes(), maxBytes_, and resolveMaxBytes().

Member Function Documentation

◆ calcSeqDataSize()

size_t grk::CompressedChunkCache::calcSeqDataSize ( const TPFetchSeq & seq)
inlinestaticprivate

References grk::SharedPtrSeq< T >::size().

Referenced by put().

◆ clear()

void grk::CompressedChunkCache::clear ( )
inline

◆ contains()

bool grk::CompressedChunkCache::contains ( uint16_t tileIndex) const
inline

Check if a tile is tracked (memory or disk).

References entries_, and mutex_.

◆ currentBytes()

size_t grk::CompressedChunkCache::currentBytes ( ) const
inline

References currentBytes_, and mutex_.

◆ evictToFit()

void grk::CompressedChunkCache::evictToFit ( )
inlineprivate

References currentBytes_, entries_, lruList_, lruMap_, maxBytes_, and spillToDisk().

Referenced by put().

◆ get()

std::shared_ptr< TPFetchSeq > grk::CompressedChunkCache::get ( uint16_t tileIndex)
inline

Ensure a tile's compressed data is in memory.

If the data was spilled to disk, it is reloaded and the TPFetch::data_ / stream_ pointers are repopulated.

Returns
the TPFetchSeq, or nullptr if tile not in cache.

References entries_, mutex_, promoteLRU(), and reloadFromDisk().

◆ maxBytes()

size_t grk::CompressedChunkCache::maxBytes ( ) const
inline

References maxBytes_.

Referenced by CompressedChunkCache().

◆ promoteLRU()

void grk::CompressedChunkCache::promoteLRU ( uint16_t tileIndex)
inlineprivate

References lruList_, and lruMap_.

Referenced by get().

◆ pushFrontLRU()

void grk::CompressedChunkCache::pushFrontLRU ( uint16_t tileIndex)
inlineprivate

References lruList_, and lruMap_.

Referenced by put().

◆ put()

void grk::CompressedChunkCache::put ( uint16_t tileIndex,
std::shared_ptr< TPFetchSeq > seq )
inline

Register a tile's fetched data with the cache.

Takes shared ownership of the TPFetchSeq. The actual byte buffers live in TPFetch::data_; we only track their sizes.

References calcSeqDataSize(), currentBytes_, grk::CompressedChunkCache::Entry::dataSize, entries_, evictToFit(), grk::CompressedChunkCache::Entry::inMemory, mutex_, pushFrontLRU(), removeLRU(), and grk::CompressedChunkCache::Entry::seq.

◆ reloadFromDisk()

bool grk::CompressedChunkCache::reloadFromDisk ( uint16_t tileIndex,
Entry & entry )
inlineprivate

◆ removeLRU()

void grk::CompressedChunkCache::removeLRU ( uint16_t tileIndex)
inlineprivate

References lruList_, and lruMap_.

Referenced by put().

◆ resolveMaxBytes()

size_t grk::CompressedChunkCache::resolveMaxBytes ( size_t provided)
inlinestaticprivate

Resolve max bytes from environment or use provided value.

Checks GRK_CACHEMAX first, then GDAL_CACHEMAX.

References kDefaultMaxBytes.

Referenced by CompressedChunkCache().

◆ size()

size_t grk::CompressedChunkCache::size ( ) const
inline

References entries_, and mutex_.

◆ spillToDisk()

void grk::CompressedChunkCache::spillToDisk ( uint16_t tileIndex,
Entry & entry )
inlineprivate

Serialize tile part data to disk and release in-memory buffers.

Disk format: [uint16_t numParts] For each part: [uint64_t offset][uint64_t length] For each part: [length bytes of data]

References currentBytes_, grk::CompressedChunkCache::Entry::dataSize, diskCache_, grk::CompressedChunkCache::Entry::inMemory, and grk::CompressedChunkCache::Entry::seq.

Referenced by evictToFit().

Member Data Documentation

◆ codecFormat_

GRK_CODEC_FORMAT grk::CompressedChunkCache::codecFormat_
private

◆ currentBytes_

size_t grk::CompressedChunkCache::currentBytes_
private

◆ diskCache_

std::shared_ptr<DiskCache> grk::CompressedChunkCache::diskCache_
private

◆ entries_

std::unordered_map<uint16_t, Entry> grk::CompressedChunkCache::entries_
private

Referenced by clear(), contains(), evictToFit(), get(), put(), and size().

◆ kDefaultMaxBytes

size_t grk::CompressedChunkCache::kDefaultMaxBytes = 256 * 1024 * 1024
staticconstexprprivate

Referenced by resolveMaxBytes().

◆ lruList_

std::list<uint16_t> grk::CompressedChunkCache::lruList_
private

◆ lruMap_

std::unordered_map<uint16_t, std::list<uint16_t>::iterator> grk::CompressedChunkCache::lruMap_
private

◆ maxBytes_

size_t grk::CompressedChunkCache::maxBytes_
private

◆ mutex_

std::mutex grk::CompressedChunkCache::mutex_
mutableprivate

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