Repository navigation
[Improvement][C++] Complete and adopt the existing LRU cache infrastructure #943
Copy link
Copy link
Closed
Labels
c++Pull requests that update c++ codePull requests that update c++ codeenhancementNew feature or requestNew feature or requestfeatureNew featureNew featurehelp wantedExtra attention is neededExtra attention is neededperformance
Description
Motivation
C++ performance improvements such as #885 and #934 need a reusable bounded cache. Without a production-ready cache abstraction, individual components may introduce unbounded per-object caches or duplicate cache implementations, making memory usage and ownership harder to control.
Current cache mechanisms
The repository currently contains several cache-related implementations:
cpp/src/common/cache/lru_cache.hprovides a genericstd::list+ associative-map LRU skeleton with configurable maximum size and elasticity.TsFileIOReader::device_node_cache_is an active, reader-specific cache protected by a mutex. It is unbounded and backed by a sharedPageArena, so individual eviction cannot reclaim the cached allocations.- The Java implementation has a working
LRUCachebased onLinkedHashMap, which can serve as a behavioral reference but cannot be reused directly by C++.
Problems with the existing C++ LRU
The generic C++ LRU is not currently used as a working production cache:
getCopy()andgetRef()call an undefinedget_nolock(), so these APIs fail when instantiated.tryGetRef()copies the cached value into an output parameter despite its name. This is unsuitable for entries containing vectors or other expensive values.- The comments mention thread safety, but the implementation contains no synchronization.
MetadataQuerierpassesstd::mutexas the third template argument even though that argument is defined as the associative map type.- The cache operations in
MetadataQuerierare commented out, and there are no focused C++ unit tests covering the cache behavior.
Proposed work
- Define the cache contract clearly, including ownership, reference lifetime, capacity semantics, and whether synchronization is internal or the caller's responsibility.
- Fix all public APIs and add a non-copying lookup mechanism, such as a pointer/reference accessor with a documented lifetime or a callback-based API.
- Add unit tests covering insertion, lookup promotion, update, eviction, removal, clearing, bounded capacity, and disabled/unbounded configurations.
- Correct or remove the inactive
MetadataQuerierintegration. - Adopt the completed cache in at least one real component, such as the bounded writer-level schema lookup cache discussed in perf(cpp): cache per-device schema check for repeated tablet writes (#885) #934.
- Add benchmarks or memory measurements demonstrating that the cache improves the target workload while keeping additional memory bounded.
Acceptance criteria
- All exposed cache APIs compile and have tested behavior.
- The eviction and capacity contract is deterministic and documented.
- Expensive cached values can be accessed without an unconditional copy.
- Thread-safety expectations and cached-value lifetime are explicit.
- At least one production C++ consumer uses the cache.
Related
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
c++Pull requests that update c++ codePull requests that update c++ codeenhancementNew feature or requestNew feature or requestfeatureNew featureNew featurehelp wantedExtra attention is neededExtra attention is neededperformance
Activity
Rayan-and-beyond commentedon Sep 17, 2026
I'd like to take this. I plan to first pin the C++ LRU contract with focused tests (capacity/elasticity, promotion, update, eviction, remove/clear, disabled/unbounded modes), then repair the currently broken lookup APIs and add a non-copying access pattern with an explicit lifetime/thread-safety contract. After that I'll either fix or remove the stale MetadataQuerier integration and adopt the cache in one bounded production C++ path, with a small benchmark/memory check to show the bound is real. I'll keep validation C++-scoped and build with optional codecs disabled where possible so the change stays reviewable and reproducible.