← Back to Pull requests

Add ImageCacheKey::lock_hash() so cache locking derives from the key #1146

Closed
unknown closed this pull request without merging on Sep 22, 2026 00:36 UTC
View on GitHub
unknown unknown commented on Sep 07, 2026 02:00 UTC
Author

Implements #1066, addressing @mkepa-nv's review note on #1034:

This interleaved usage of index_hash and key looks error prone. Maybe in a future MPR it would be better to use key for lock()?

The problem

The cuslide2 tile path built a key from (ifd_hash, tile_index) and then, on an adjacent line, computed a separate index_hash from those same two values by hand:

// Hash for per-tile locking
const uint64_t index_hash = ifd_hash ^
    (static_cast<uint64_t>(tile_index) | (static_cast<uint64_t>(tile_index) << 32));

// --- Cache lookup (RAII lock guard for exception safety) ---
auto key = image_cache.create_key(ifd_hash, tile_index);

The same tile was described two different ways with nothing tying them together, so changing one would silently not reach the other.

Worth being explicit about why that is worse than ordinary duplication: the hash only selects which mutex of the pool guards an entry, so a divergence would not fail loudly or immediately. lock() and unlock() would simply operate on different mutexes, and the tile would be decoded and inserted with no lock actually held. That is a latent data race, not a crash.

The change

ImageCacheKey::lock_hash() derives the value from the key, and cuslide2 passes key->lock_hash() to its lock guard. The formula is exactly the one the IFD readers computed inline, so every tile still maps onto the mutex it mapped to before — no behavioural change.

Scope

Deliberately limited to cuslide2, the file the review comment was on. Two other places derive a lock hash by hand and are left alone:

  • Legacy cuslide ifd.cpp keeps its index_hash because a PROF_SCOPED_RANGE profiling marker consumes it earlier in the function, before any key exists.

  • NvJpegProcessor derives its lock hash with splitmix64(index) in both request() and wait_for_processing().
  • Both are self-consistent today, so converting them would change which mutex a tile maps to without any functional gain, on a code path I cannot test here. lock_hash() is available to them whenever that code is next touched with tests to back the change.

    Tests

    cpp/tests/test_image_cache.cpp covers the invariants callers actually depend on rather than restating the implementation:

  • the hash is a pure function of the key

  • separately constructed equal keys agree — this is the real-world case, where the lookup and the later insert each build their own key

  • the value still matches the previous inline formula, pinning the no-behaviour-change claim

  • consecutive tile indices land in distinct pool slots, which is the access pattern tile assembly produces

  • one tile index in different IFDs does not share a slot
  • Verification

    -fsyntax-only against the project's own compile_commands.json for image_cache.cpp, ifd.cpp and the new test, which compiles under the test target's -Werror -Wall -Wextra. All clean.

    I also executed the test assertions standalone, since compiling a test proves nothing about whether it passes:

      repeated calls agree                                       PASS
      equal keys agree                                           PASS
      matches inline IFD formula                                 PASS
      64 consecutive indices -> 64 distinct slots                PASS
      same index, different IFD -> different hash                PASS
      pool=8 -> all distinct                                     PASS
      pool=16 -> all distinct                                    PASS
      pool=32 -> all distinct                                    PASS
      pool=128 -> all distinct                                   PASS
      pool=256 -> all distinct                                   PASS
    

    The distinct-slot property holds across pool sizes 8 through 256, so the assertion is not overfit to the single size the test uses.

    Unrelated issue noticed nearby, not fixed here

    While tracing the key's hashing I found that PerProcessImageCache's equality comparator ignores file_hash:

    https://github.com/rapidsai/cucim/blob/main/cpp/src/cache/image_cache_per_process.cpp#L25-L29

    bool equal_to<std::shared_ptr<cucim::cache::ImageCacheKey>>::operator()(
        const std::shared_ptr<cucim::cache::ImageCacheKey>& lhs, const std::shared_ptr<cucim::cache::ImageCacheKey>& rhs) const
    {
        return lhs->location_hash == rhs->location_hash;
    }
    

    std::hash for the same key does combine both fields, so two keys differing only in file_hash normally land in different buckets and the comparator is never asked. But bucket assignment is hash % bucket_count, so a collision is possible, and when it happens this returns a tile belonging to a different image or IFD. SharedMemoryImageCache's comparator does compare both fields, which suggests this one is an oversight rather than intentional.

    Left out of this PR since it is a separate defect with its own behavioural risk. Happy to file it as its own issue if you agree it is one.

    Diff: 619 lines (+464 / -155) across 7 files Reviewer: gigony Review duration: 21515m
    All Vouch review confidence checks passed
    Residual risk 0.22 = Change Risk (0.50) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 619 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.385
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #1146"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50.2% (MEDIUM) · Higher = larger blast radius
    50.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 21.7% · Safe to merge
    21.7%
    Repository