Skip to content

Zstd doesn't always create RLE blocks #4457

Open

Description

@terrelln
cat <(head -c 1GB /dev/zero) <(echo Hello world) <(head -c 1GB /dev/zero) > file
zstd file

There is a small percent of blocks that aren't emitted as RLE blocks, and instead are emitted as a block with a single sequence.

Activity

  1. Cyan4973 commented on Aug 11, 2025

    @Cyan4973
    Contributor

    Yes, this is a known issue. There is a heuristic that attempts to determine when to test a block for conversion to RLE, but it does not always trigger. We could likely improve this by relaxing the condition, thereby increasing the likelihood that the RLE test is executed.

  2. terrelln commented on Aug 12, 2025

    @terrelln
    ContributorAuthor

    I think there could be a small improvement to the heuristic. The blocks found are a single match. with 0 literals.

  3. Cyan4973 commented on Aug 12, 2025

    @Cyan4973
    Contributor

    I believed the heuristic is based on the compressed size of the block,
    i.e. if the compressed size is "small enough", then it's a hint that it might be a candidate for RLE.

    Blocks consisting of a single match with 0 literals should fit in this description.
    I'm surprised if they are not.

  4. eyupcanakman commented on Jul 17, 2026

    @eyupcanakman
    Contributor

    These blocks already pass the RLE heuristic, so relaxing it wouldn't catch them. On the default path ZSTD_compressBlock_internal emits RLE when !zc->isFirstBlock && cSize < rleMaxLength && ZSTD_isRLE(...). For an all-zeros block cSize is 8 and ZSTD_isRLE is true, so the only thing blocking RLE is !zc->isFirstBlock.

    isFirstBlock gets reset to 1 by every worker job. ZSTDMT_compressionJob calls ZSTD_compressBegin, so the first block of each ~8MB job is treated like the frame's first block and skips the RLE path. Only the frame's real first block needs that guard for the pre-v1.4.3 decoder, not every job.

    --single-thread on the repro emits them correctly, leaving only the real first block and the Hello world block as non-RLE. With -T the number of extra single-sequence blocks tracks the job count, e.g. -T4 on a 33 MB all-zeros input gives 4 of them.

  5. terrelln commented on Jul 17, 2026

    @terrelln
    ContributorAuthor

    Thanks for looking into that @eyupcanakman! Looks like it will be an easy fix in ZSTDMT to ensure that zc->isFirstBlock = false for all but the first job. We have to keep the isFistBlock check around to work around this bug in the CLI.

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions