From ed876d7f794b1daa2f57cdfd78c321f53f196940 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Mon, 30 Nov 2026 11:58:02 +0000 Subject: [PATCH] kernel: make kwdiff take a far match only when it is clearly longer, so the SMP patch compresses better A match more than 4 KiB away from where the current one continues now has to be 32 bytes longer instead of 8. New code otherwise became tens of thousands of short copies from unrelated places, each a seek a compressor cannot predict. The SMP patch with the Kubernetes options goes from 1.51 to 1.30 MB gzip; the format, the loader and the time to make it are unchanged. --- .agents/js.md | 4 ++-- kernel/tools/kwdiff.mjs | 11 ++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/.agents/js.md b/.agents/js.md index 27ca87a..ab752c4 100644 --- a/.agents/js.md +++ b/.agents/js.md @@ -891,8 +891,8 @@ parts that are easy to get wrong. `unkernel.kernel.smp.kwpatch` last, a patch from its UP kernel to the SMP one (`kernel/tools/kwdiff.mjs --remap`: the base's function, global and type indices and data addresses renumbered to the target's by - `js/core/kremap.ts`, then a bsdiff-style byte diff; 1.37 MB raw, 0.76 - MB gzip, 0.67 MB brotli, 2026-11-17). With more than one CPU asked + `js/core/kremap.ts`, then a bsdiff-style byte diff that takes a far match only when it is + clearly longer; 2.14 MB raw, 0.93 MB gzip, 2026-11-30). With more than one CPU asked (`cpus`, CLI `--cpus N`), `loadKernelModule({ smp: true })` (and the CLI) reads the bundle's bytes (no streaming compile), applies the patch (`js/core/kpatch.ts` `smpKernelFromBundle()`: base length and SHA-256, diff --git a/kernel/tools/kwdiff.mjs b/kernel/tools/kwdiff.mjs index 3e7db05..022d7e2 100644 --- a/kernel/tools/kwdiff.mjs +++ b/kernel/tools/kwdiff.mjs @@ -107,6 +107,15 @@ function search(sa, a, b, bi, lo, hi) { return x > y ? [x, sa[lo]] : [y, sa[hi]]; } +// A new match ends the current one when it is longer by 8 bytes (bsdiff's +// rule), or by FAR_GAIN when it is more than FAR bytes away from where the +// current one continues: new code otherwise becomes tens of thousands of +// short copies from unrelated places, each a seek that a compressor cannot +// predict, where the same bytes as new bytes compress better (the SMP +// patch with the Kubernetes options: 1.51 MB gzip with 8, 1.30 with 32; +// 2026-11-30). +const FAR = 4096, FAR_GAIN = 32; + /** bsdiff-style diff: { ctrl: [add, copy, seek][], diff: Uint8Array, extra: Uint8Array }. */ export function diff(a, b) { const sa = suffixArray(a); @@ -122,7 +131,7 @@ export function diff(a, b) { [len, pos] = search(sa, a, b, scan, 0, n); for (; scsc < scan + len; scsc++) if (scsc + lastoffset < n && a[scsc + lastoffset] === b[scsc]) oldscore++; - if ((len === oldscore && len !== 0) || len > oldscore + 8) break; + if ((len === oldscore && len !== 0) || len > oldscore + (Math.abs(pos - scan - lastoffset) <= FAR ? 8 : FAR_GAIN)) break; if (scan + lastoffset < n && a[scan + lastoffset] === b[scan]) oldscore--; } if (len !== oldscore || scan === m) {