mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-02 17:09:41 +08:00
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.
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user