Skip to content

Multi-level: a hit on a lower level fetches the same object twice #2817

Closed

Description

@garysassano

Summary

In MultiLevelStorage::get(), a cache hit on any level above L0 reads the same object from that level twice: once through get() to build the entry returned to the compiler, and again through get_raw() to obtain bytes for backfilling the faster levels. For remote levels both calls perform a full object read.

The second read is awaited before the hit is returned, so it sits on the compiler's critical path rather than in the spawned backfill tasks.

Code

v0.17.0 (c037e11), src/cache/multilevel.rs L637-L714:

match level.get(key).await {                    // first read: full object read + CacheRead decode
    Ok(Cache::Hit(entry)) => {
        // ...
        if idx > 0 {
            match level.get_raw(key).await {    // second read: same object, fetched again
                Ok(Some(raw_bytes)) => {
                    for backfill_idx in 0..idx {
                        tokio::spawn(/* backfill using bytes from the second read */);
                    }
                }
                // ...
            }
        }
        return Ok(Cache::Hit(entry));
    }

For RemoteStorage both paths call operator.read():

So with SCCACHE_MULTILEVEL_CHAIN=disk,s3, every L1 hit issues two S3 GETs for the same key, transfers the object twice, and the compiler waits for both.

Suggested fix

Fetch the bytes once and derive both the returned entry and the backfill payload from them — for example call get_raw() first and construct CacheRead::from(io::Cursor::new(bytes)) locally, keeping the current path as a fallback for backends whose get_raw() returns None.

Impact

Request count and transferred bytes for lower-level hits are doubled, and the duplicate fetch is serialized ahead of the compiler result.

I want to be careful not to overstate the wall-clock cost: I found this by reading the source, and the multi-level trial in our CI benchmarks was also running SCCACHE_CLIENT_SIDE=1, which introduced a much larger penalty of its own. So I have not isolated how much of a real build this accounts for, and concurrent requests presumably hide part of it. The redundant fetch itself is unambiguous in the code.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions