#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::indexing_slicing,
clippy::panic
)]
use std::alloc::{GlobalAlloc, Layout, System};
use std::sync::atomic::{AtomicUsize, Ordering};
use delta_kit::{apply_delta, apply_delta_lenient};
static ALLOCATIONS: AtomicUsize = AtomicUsize::new(0);
struct Counting;
unsafe impl GlobalAlloc for Counting {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
ALLOCATIONS.fetch_add(1, Ordering::Relaxed);
unsafe { System.alloc(layout) }
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
unsafe { System.dealloc(ptr, layout) }
}
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
ALLOCATIONS.fetch_add(1, Ordering::Relaxed);
unsafe { System.alloc_zeroed(layout) }
}
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
ALLOCATIONS.fetch_add(1, Ordering::Relaxed);
unsafe { System.realloc(ptr, layout, new_size) }
}
}
#[global_allocator]
static GLOBAL: Counting = Counting;
fn allocations() -> usize {
ALLOCATIONS.load(Ordering::Relaxed)
}
fn base_data(size: usize) -> Vec<u8> {
(0..size)
.map(|i| b"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i % 62])
.collect()
}
fn copy_only_delta(base_len: usize, target_len: usize, instrs: usize) -> Vec<u8> {
assert_eq!(instrs * 256, target_len, "each Copy covers 256 bytes");
let mut delta = Vec::with_capacity(13 + instrs * 13);
delta.push(0x02); delta.extend_from_slice(&(target_len as u64).to_le_bytes());
delta.extend_from_slice(&(instrs as u32).to_le_bytes());
for i in 0..instrs {
let offset = (i * 256) % base_len.min(i * 256 + 1).max(256);
let offset = if offset + 256 <= base_len { offset } else { 0 };
delta.push(0x01); delta.extend_from_slice(&(offset as u64).to_le_bytes());
delta.extend_from_slice(&256u32.to_le_bytes());
}
delta
}
#[test]
fn apply_delta_allocation_bounds() {
let base = base_data(1024 * 1024);
let delta_10k = copy_only_delta(base.len(), 2_560_000, 10_000);
let before = allocations();
let out = apply_delta(&base, &delta_10k).expect("delta must apply");
let allocs_10k = allocations() - before;
assert_eq!(out.len(), 2_560_000);
assert!(
allocs_10k <= 8,
"apply of a 10k-instruction delta must allocate O(1) buffers \
(got {allocs_10k}; the target_len header is the declared bound)"
);
let delta_40k = copy_only_delta(base.len(), 10_240_000, 40_000);
let before = allocations();
let out = apply_delta(&base, &delta_40k).expect("delta must apply");
let allocs_40k = allocations() - before;
assert_eq!(out.len(), 10_240_000);
assert!(
allocs_40k <= 8,
"apply of a 40k-instruction delta must stay in the same O(1) budget \
(got {allocs_40k})"
);
let before = allocations();
let out = apply_delta_lenient(&base, &delta_10k);
let allocs_lenient = allocations() - before;
assert_eq!(out.len(), 2_560_000);
assert!(
allocs_lenient <= 8,
"lenient apply must respect the same O(1) buffer bound (got {allocs_lenient})"
);
let before = allocations();
drop(copy_only_delta(base.len(), 2_560_000, 10_000));
assert!(
allocations() > before,
"delta assembly must allocate — counter sanity check"
);
}