惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
I
InfoQ
H
Hackread – Cybersecurity News, Data Breaches, AI and More
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
有赞技术团队
有赞技术团队
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
Engineering at Meta
Engineering at Meta
B
Blog RSS Feed
博客园_首页
Y
Y Combinator Blog
V
Visual Studio Blog
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence
雷峰网
雷峰网
博客园 - 司徒正美
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
H
Help Net Security
P
Proofpoint News Feed
B
Blog
云风的 BLOG
云风的 BLOG
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

kkocdko's blog

Tasker 永久试用修改思路 - kkocdko's blog 极限压缩文档扫描件 - kkocdko's blog Bypass the Blank Chars Detection crknob - Chrome portable patch 我如何将博客首页体积降至 3 KB - kkocdko's blog 如何极限优化压缩文件体积 - kkocdko's blog EmEditor 精简版 - kkocdko's blog Backup Data from VSCode Web 优雅地游玩 Minecraft - kkocdko's blog 让程序以管理员权限运行的 PE 清单 - kkocdko's blog 注销 ZEIT 账号之后遇到的坑 - kkocdko's blog 本博客的协议已更改为 CC0 - kkocdko's blog mobp - 使 MSOffice 后台常驻 在安装 VS 生成工具时规避 .NET 安装错误 各种格式的占位空文件 - kkocdko's blog 优雅地游玩 Flash 游戏 - kkocdko's blog WPS 2016 极限精简版 - kkocdko's blog Convert Const to Let by Terser 规避 Visual Studio 许可证过期的 Bug 方便地使用 iPod - kkocdko's blog 用 UserJS 注入 UserCSS - kkocdko's blog 注册表修改文件关联(默认程序) - kkocdko's blog 周期精准的太阳系模型 - kkocdko's blog 关闭屏幕的 WinAPI - kkocdko's blog 重置 Windows 图标缓存 - kkocdko's blog AIDA64 极限精简单文件版 - kkocdko's blog 几何画板单文件精简版 - kkocdko's blog 世界之窗浏览器单文件版 - kkocdko's blog 在 MBR + Legacy 下用 Bootmgr 引导 Ubuntu 简洁的 Markdown 预览工具 - kkocdko's blog
Compressibility detection in btrfs and zstd
2026-09-19 · via kkocdko's blog

Based on linux kernel 7.2.4.

For btrfs + zstd, in fact, there are several measures to "avoid ineffective compression". There's indeed some redundant logic that could be removed, but for now, the results seem acceptable.

From top to bottom:

Check flags and attrs

In ./fs/btrfs/btrfs_inode.h:474:btrfs_inode_can_compress, it checks no-cow and no-sum flags of inode, which allow chattr +m xxx to disable compression for file and dir manually.

// ./fs/btrfs/inode.c:729:inode_need_compress
if (unlikely(!btrfs_inode_can_compress(inode))) {
    DEBUG_WARN("BTRFS: unexpected compression for ino %llu", btrfs_ino(inode));
    return 0;
}

Then, other checking logics:

// ... check_inline
// ... defrag_compress
if (end + 1 - start <= fs_info->sectorsize &&
    (!check_inline || (start > 0 || end + 1 < inode->disk_i_size)))
    return 0;
// ... inode->flags & BTRFS_INODE_NOCOMPRESS
  • Compress if there's compress-force in mount options.
  • Skip for inline inode.
  • Skip if already mark as NOCOMPRESS.

Heuristic detection

In the end of inode_need_compress function, it calls ./fs/btrfs/compression.c:1560:btrfs_compress_heuristic.

The specific implementation is complex, so I won't paste code here. In summary, it samples the byte distribution.

  • Byte set: count of byte values in buckets.
  • Core byte set: how many bytes cover 90% of the sample.

Sampling rules ./fs/btrfs/compression.c:620-643:

  • Sample 16 bytes for every 256 bytes;
  • A maximum of approximately 8 KiB is sampled per 128 KiB compression unit.
  • Thresholds:
    • ./fs/btrfs/compression.c:1272-1273: Entropy thresholds 65 / 80.
    • ./fs/btrfs/compression.c:1423-1424: Core byte set threshold 64 / 200.
    • ./fs/btrfs/compression.c:1462: Byte set threshold 64.

For high-entropy data like h264 video, common results are: byte sets approaches 256, with large core byte set.

Fallback after compression

After heuristic detection, ./fs/btrfs/inode.c:947 calls btrfs_compress_bio() to compress then compare with origin size, if it is even bigger, mark as NOCOMPRESS.

if (total_compressed + blocksize > total_in)
    goto mark_incompressible;

Inside zstd

The zstd is trimmed and adapted, in ./lib/zstd of the kernel.

It works like this, for 128 KiB block:

  • Run LZ match finder.
  • Collect matches and literals.
  • Try Huffman/FSE encoding.
  • If the result is not good enough, emit a raw/RLE block instead.

So zstd avoids keeping a bad compressed result, unlike btrfs avoids starting work.

At the start of a block (./lib/zstd/compress/zstd_compress.c:3187):

if (srcSize < MIN_CBLOCK_SIZE + ZSTD_blockHeaderSize + 1 + 1) {
    ...
    return ZSTDbss_noCompress;
}

TODO: