Repository: vercel/next.js
File: turbopack/crates/turbo-persistence/src/static_sorted_file.rs (Rust)

Code before:
                GuardResult::Timeout => unreachable!(),
            },
        )
    }

    /// Reads a key block from the file.
    fn read_key_block(&self, block_index: u16) -> Result<ArcBytes> {
        self.read_block(block_index)
    }

    /// Reads a small value block from the file.
    fn read_small_value_block(&self, block_index: u16) -> Result<ArcBytes> {
        self.read_block(block_index)
    }

    /// Reads a value block from the file.
    fn read_value_block(&self, block_index: u16) -> Result<ArcBytes> {
        self.read_block(block_index)
    }

    /// Verifies the CRC32 checksum of uncompressed block data. Returns an error on mismatch.
    fn verify_checksum(&self, data: &[u8], expected: u32, block_index: u16) -> Result<()> {
        let actual = checksum_block(data);
        if actual != expected {
            bail!(
                "Cache corruption detected: checksum mismatch in block {} of SST file seq:{} \
                 (expected {:08x}, got {:08x})",
                block_index,
                self.meta.sequence_number,
                expected,
                actual
            );
        }
        Ok(())
    }

    /// Reads a block from the file, decompressing if needed, and verifies its checksum.
    #[tracing::instrument(level = "info", name = "reading database block", skip_all)]
    fn read_block(&self, block_index: u16) -> Result<ArcBytes> {
        let (uncompressed_length, expected_checksum, block) =
            self.get_raw_block_slice(block_index)?;

        // 0 means the block was not compressed, return the mmap-backed ArcBytes directly
        if uncompressed_length == 0 {
            self.verify_checksum(block, expected_checksum, block_index)?;
            return Ok(self.mmap_slice_to_arc_bytes(block));
        }

        // Advise Sequential only here: we're about to linearly scan the block
        // through the decompressor. For uncompressed blocks (returned above)
        // and lazy medium values (which call get_raw_block directly without

Review comment (performance):
```suggestion
                "Cache corruption detected: checksum mismatch in block {} of {:08}.sst \
```

Code after:
                GuardResult::Timeout => unreachable!(),
            },
        )
    }

    /// Reads a key block from the file.
    fn read_key_block(&self, block_index: u16) -> Result<ArcBytes> {
        self.read_block(block_index)
    }

    /// Reads a small value block from the file.
    fn read_small_value_block(&self, block_index: u16) -> Result<ArcBytes> {
        self.read_block(block_index)
    }

    /// Reads a value block from the file.
    fn read_value_block(&self, block_index: u16) -> Result<ArcBytes> {
        self.read_block(block_index)
    }

    /// Verifies the CRC32 checksum of on-disk block data. Returns an error on mismatch.
    fn verify_checksum(&self, data: &[u8], expected: u32, block_index: u16) -> Result<()> {
        let actual = checksum_block(data);
        if actual != expected {
            bail!(
                "Cache corruption detected: checksum mismatch in block {} of {:08}.sst (expected \
                 {:08x}, got {:08x})",
                block_index,
                self.meta.sequence_number,
                expected,
                actual
            );
        }
        Ok(())
    }

    /// Reads a block from the file, decompressing if needed, and verifies its checksum.
    ///
    /// The checksum is verified on the raw on-disk data **before** decompression, so
    /// corruption is caught before passing data to LZ4.
    #[tracing::instrument(level = "info", name = "reading database block", skip_all)]
    fn read_block(&self, block_index: u16) -> Result<ArcBytes> {
        let (uncompressed_length, expected_checksum, block) =
            self.get_raw_block_slice(block_index)?;

        // Verify checksum on the raw on-disk data before decompression.
        self.verify_checksum(block, expected_checksum, block_index)?;

        // 0 means the block was not compressed, return the mmap-backed ArcBytes directly
        if uncompressed_length == 0 {
            return Ok(self.mmap_slice_to_arc_bytes(block));