From 302897c5e8d61db9107505ba929f9abfc197896d Mon Sep 17 00:00:00 2001 From: Lukasz Kasprzak Date: Wed, 29 Jul 2026 00:49:47 +0200 Subject: perf: DEFLATE (puff) + -no-pie -> OF binary 879 KB -> 662 KB (-25%) Two size wins: - Replace the home-grown LZSS (2.0x) with DEFLATE: mktext gzips the text and strips the gzip container to a raw DEFLATE stream; clectio decompresses it with puff, Mark Adler's public-domain reference inflate (~200 lines, verified byte-identical to gunzip on the OF/EF text, repetitive, random and empty inputs). Text blob 671 KB -> 492 KB (OF), 164 KB -> 132 KB (EF), 2.7x. - Build with -no-pie: clectio is a local CLI reading static embedded data with no attack surface, so it needs no PIE/ASLR. The linker then bakes the string- table pointers in at link time instead of emitting ~57 KB of runtime relocations. Net: OF 879 -> 662 KB, EF 266 -> 214 KB. Verified byte-identical to lectio. The default build still needs only a compiler + libc; a CORPUS= rebuild now also needs gzip. lz.h removed. Claude-Session: https://claude.ai/code/session_01S1CD1u3tgSS4xNu6WHfp5a --- mktext.c | 48 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 13 deletions(-) (limited to 'mktext.c') diff --git a/mktext.c b/mktext.c index 1cfcf4b..17d7705 100644 --- a/mktext.c +++ b/mktext.c @@ -8,8 +8,6 @@ * usage: mktext verses.keys corpus.tsv out.lz * out.lz = [uint32 LE rawlen][LZSS-packed "chap:verse text\n" lines]. */ -#define LZ_PACK -#include "lz.h" #include #include #include @@ -121,17 +119,41 @@ int main(int argc, char **argv) { if (missing) fprintf(stderr, "mktext: warning: %zu of %zu verses not in corpus (blank)\n", missing, nkeys); - size_t plen; - unsigned char *packed = lz_pack((unsigned char *)out, olen, &plen); + /* Pack: gzip the raw text, then strip the gzip container to the bare DEFLATE + * stream (10-byte header + 8-byte trailer) that clectio's puff() decodes. + * out.lz = [uint32 LE raw length][raw DEFLATE]. */ + { + char rawpath[4096], gzpath[4200], cmd[4300]; + FILE *rf, *of; + char *gz; + size_t gzn, deflen; + unsigned char hdr[4]; - FILE *of = fopen(argv[3], "wb"); - if (!of) { fprintf(stderr, "mktext: cannot write %s\n", argv[3]); return 1; } - unsigned char hdr[4] = { (unsigned char)olen, (unsigned char)(olen >> 8), - (unsigned char)(olen >> 16), (unsigned char)(olen >> 24) }; - fwrite(hdr, 1, 4, of); - fwrite(packed, 1, plen, of); - fclose(of); - fprintf(stderr, "mktext: %zu verses, %zu -> %zu bytes (%.1fx)\n", - nkeys, olen, plen, olen ? (double)olen / (double)plen : 0); + snprintf(rawpath, sizeof rawpath, "%s.raw", argv[3]); + rf = fopen(rawpath, "wb"); + if (!rf) { fprintf(stderr, "mktext: cannot write %s\n", rawpath); return 1; } + fwrite(out, 1, olen, rf); + fclose(rf); + + snprintf(cmd, sizeof cmd, "gzip -9nf '%s'", rawpath); + if (system(cmd) != 0) { fprintf(stderr, "mktext: gzip failed (is gzip installed?)\n"); return 1; } + snprintf(gzpath, sizeof gzpath, "%s.gz", rawpath); + gz = slurp(gzpath, &gzn); + remove(gzpath); + if (gzn < 18) { fprintf(stderr, "mktext: unexpected gzip output\n"); return 1; } + deflen = gzn - 18; /* drop 10-byte gzip header + 8-byte trailer */ + + of = fopen(argv[3], "wb"); + if (!of) { fprintf(stderr, "mktext: cannot write %s\n", argv[3]); return 1; } + hdr[0] = (unsigned char)olen; + hdr[1] = (unsigned char)(olen >> 8); + hdr[2] = (unsigned char)(olen >> 16); + hdr[3] = (unsigned char)(olen >> 24); + fwrite(hdr, 1, 4, of); + fwrite(gz + 10, 1, deflen, of); + fclose(of); + fprintf(stderr, "mktext: %zu verses, %zu -> %zu bytes (%.1fx)\n", + nkeys, olen, deflen + 4, olen ? (double)olen / (double)(deflen + 4) : 0); + } return 0; } -- cgit v1.3