Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Same-Fill Detection

Same-fill optimization provides 2048:1 compression for pages containing a single repeated byte value.

Why Same-Fill Matters

Memory pages often contain:

  • Zero pages: ~30-40% of typical memory (uninitialized, freed)
  • Same-fill pages: ~5-10% additional (memset patterns)

Detecting and encoding these specially provides massive compression wins.

Detection

#![allow(unused)]
fn main() {
use trueno_zram_core::samefill::detect_same_fill;
use trueno_zram_core::PAGE_SIZE;

let zero_page = [0u8; PAGE_SIZE];
let pattern_page = [0xAA; PAGE_SIZE];
let mixed_page = [0u8; PAGE_SIZE];

// Zero page detected
assert!(detect_same_fill(&zero_page).is_some());

// Pattern page detected
assert!(detect_same_fill(&pattern_page).is_some());

// Mixed content not detected
let mut mixed = [0u8; PAGE_SIZE];
mixed[100] = 0xFF;
assert!(detect_same_fill(&mixed).is_none());
}

Compact Encoding

Same-fill pages compress to just 2 bytes:

#![allow(unused)]
fn main() {
use trueno_zram_core::samefill::CompactSameFill;

let fill_value = 0u8;
let compact = CompactSameFill::new(fill_value);

// Serialize (2 bytes)
let bytes = compact.to_bytes();
assert_eq!(bytes.len(), 2);

// Deserialize
let restored = CompactSameFill::from_bytes(&bytes)?;
assert_eq!(restored.fill_value(), 0);

// Expand back to full page
let page = restored.expand();
assert_eq!(page.len(), PAGE_SIZE);
assert!(page.iter().all(|&b| b == 0));
}

Compression Ratio

Page TypeOriginalCompressedRatio
Zero-fill4096 B2 B2048:1
0xFF-fill4096 B2 B2048:1
Any same-fill4096 B2 B2048:1

Integration with Compressor

The compressor automatically detects same-fill pages:

#![allow(unused)]
fn main() {
use trueno_zram_core::{CompressorBuilder, Algorithm};

let compressor = CompressorBuilder::new()
    .algorithm(Algorithm::Lz4)
    .build()?;

let zero_page = [0u8; PAGE_SIZE];
let compressed = compressor.compress(&zero_page)?;

// Same-fill pages get special encoding
println!("Compressed size: {} bytes", compressed.data.len());
// Output: ~20 bytes (LZ4 minimal encoding for same-fill)
}

Performance

Same-fill detection is extremely fast:

#![allow(unused)]
fn main() {
// SIMD-accelerated comparison
// AVX-512: Check 64 bytes per iteration
// AVX2: Check 32 bytes per iteration
// Typical: <100ns for 4KB page
}

Memory Statistics

On typical systems:

Memory TypeSame-Fill %
Idle desktop60-70%
Active workload35-45%
Database server25-35%
Compilation40-50%