diff --git a/.changeset/measure-cache-collisions.md b/.changeset/measure-cache-collisions.md new file mode 100644 index 0000000..bf4e6fe --- /dev/null +++ b/.changeset/measure-cache-collisions.md @@ -0,0 +1,5 @@ +--- +"@bomb.sh/tty": patch +--- + +Fixes text that could be measured and drawn as a different string. Two texts whose measurement-cache hashes collide now each keep their own width and wrapping, within one frame and across renders. diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml index d931629..34b838f 100644 --- a/.github/workflows/benchmark.yml +++ b/.github/workflows/benchmark.yml @@ -8,6 +8,7 @@ on: paths: - "src/**" - "clay" # submodule pointer + - "xxhash" # submodule pointer - "patches/**" - "Makefile" - "*.ts" @@ -22,6 +23,7 @@ on: paths: - "src/**" - "clay" # submodule pointer + - "xxhash" # submodule pointer - "patches/**" - "Makefile" - "*.ts" diff --git a/.gitmodules b/.gitmodules index 88c21c4..540bebf 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "clay"] path = clay url = https://github.com/nicbarker/clay.git +[submodule "xxhash"] + path = xxhash + url = https://github.com/Cyan4973/xxHash.git diff --git a/BUILD.md b/BUILD.md index e80e478..abfe74f 100644 --- a/BUILD.md +++ b/BUILD.md @@ -5,7 +5,7 @@ This guide is for maintainers and builders working on `@bomb.sh/tty` itself. It covers: - cloning the repo correctly, -- initializing the `clay` git submodule, +- initializing the `clay` and `xxhash` git submodules, - installing the toolchain needed to compile the C sources to WebAssembly, - building the local development artifacts, and - verifying that the repo is ready for development. @@ -25,7 +25,7 @@ It generates: ## Clone the repo with submodules -The build depends on the `clay` git submodule. +The build depends on the `clay` and `xxhash` git submodules. Preferred fresh clone: @@ -46,8 +46,8 @@ Quick check: git submodule status --recursive ``` -You should also see a populated `clay/` directory. If `clay/` is missing or -empty, fix the submodule state before building. +You should also see populated `clay/` and `xxhash/` directories. If either is +missing or empty, fix the submodule state before building. ## Required tools @@ -198,7 +198,7 @@ make clean && make Re-run `make` when: - you change files under `src/` -- you update the `clay` submodule +- you update the `clay` or `xxhash` submodule - `clayterm.wasm` or `wasm.ts` is missing - generated outputs look stale after switching branches or pulling changes @@ -225,11 +225,12 @@ deno lint ## Troubleshooting -### `clay/` is missing or empty +### `clay/` or `xxhash/` is missing or empty Symptoms may include build failures such as: - `fatal error: '../clay/clay.h' file not found` +- `fatal error: '../xxhash/xxhash.h' file not found` Recovery: diff --git a/deno.json b/deno.json index 0448304..1844cb8 100644 --- a/deno.json +++ b/deno.json @@ -27,11 +27,11 @@ }, "nodeModulesDir": "auto", "fmt": { - "exclude": ["clay", "build", "CHANGELOG.md", ".changeset"] + "exclude": ["clay", "xxhash", "build", "CHANGELOG.md", ".changeset"] }, "lint": { "rules": { "exclude": ["prefer-const"] }, - "exclude": ["clay", "build"], + "exclude": ["clay", "xxhash", "build"], "plugins": ["lint/prefer-let.ts"] } } diff --git a/patches/clay-measure-text-cache-key.patch b/patches/clay-measure-text-cache-key.patch new file mode 100644 index 0000000..d1a0097 --- /dev/null +++ b/patches/clay-measure-text-cache-key.patch @@ -0,0 +1,72 @@ +diff --git a/clay.h b/clay.h +--- a/clay.h ++++ b/clay.h +@@ -1294,13 +1294,24 @@ + + CLAY__ARRAY_DEFINE(Clay__MeasuredWord, Clay__MeasuredWordArray) + ++// XXH3-128 fingerprints the text of each measurement cache entry. ++#define XXH_INLINE_ALL ++#define XXH_NO_STREAM ++#include "../xxhash/xxhash.h" ++ + typedef struct { + Clay_Dimensions unwrappedDimensions; + int32_t measuredWordsStartIndex; + float minWidth; + bool containsNewlines; + // Hash map data +- uint32_t id; ++ // Cache key: a 32-bit id alone collides between distinct texts, so a hit ++ // requires the full 128-bit fingerprint, the length and the text config. ++ uint32_t fingerprint[4]; ++ int32_t length; ++ uint16_t fontId; ++ uint16_t fontSize; ++ uint16_t letterSpacing; + int32_t nextIndex; + uint32_t generation; + } Clay__MeasureTextCacheItem; +@@ -1652,13 +1663,24 @@ + return &Clay__MeasureTextCacheItem_DEFAULT; + } + #endif +- uint32_t id = Clay__HashStringContentsWithConfig(text, config); +- uint32_t hashBucket = id % (context->maxMeasureTextCacheWordCount / 32); ++ // Statically allocated text is identified by its address, as in Clay__HashStringContentsWithConfig ++ const void *keyBytes = text->isStaticallyAllocated ? (const void *)&text->chars : (const void *)text->chars; ++ size_t keyLength = text->isStaticallyAllocated ? sizeof(text->chars) : (size_t)text->length; ++ XXH128_hash_t hash = XXH3_128bits(keyBytes, keyLength); ++ uint32_t fingerprint[4] = { (uint32_t)hash.low64, (uint32_t)(hash.low64 >> 32), (uint32_t)hash.high64, (uint32_t)(hash.high64 >> 32) }; ++ uint32_t hashBucket = fingerprint[0] % (context->maxMeasureTextCacheWordCount / 32); + int32_t elementIndexPrevious = 0; + int32_t elementIndex = context->measureTextHashMap.internalArray[hashBucket]; + while (elementIndex != 0) { + Clay__MeasureTextCacheItem *hashEntry = Clay__MeasureTextCacheItemArray_Get(&context->measureTextHashMapInternal, elementIndex); +- if (hashEntry->id == id) { ++ if (hashEntry->fingerprint[0] == fingerprint[0] && ++ hashEntry->fingerprint[1] == fingerprint[1] && ++ hashEntry->fingerprint[2] == fingerprint[2] && ++ hashEntry->fingerprint[3] == fingerprint[3] && ++ hashEntry->length == text->length && ++ hashEntry->fontId == config->fontId && ++ hashEntry->fontSize == config->fontSize && ++ hashEntry->letterSpacing == config->letterSpacing) { + hashEntry->generation = context->generation; + return hashEntry; + } +@@ -1689,7 +1711,13 @@ + } + + int32_t newItemIndex = 0; +- Clay__MeasureTextCacheItem newCacheItem = { .measuredWordsStartIndex = -1, .id = id, .generation = context->generation }; ++ Clay__MeasureTextCacheItem newCacheItem = { ++ .measuredWordsStartIndex = -1, ++ .fingerprint = { fingerprint[0], fingerprint[1], fingerprint[2], fingerprint[3] }, ++ .length = text->length, ++ .fontId = config->fontId, .fontSize = config->fontSize, .letterSpacing = config->letterSpacing, ++ .generation = context->generation ++ }; + Clay__MeasureTextCacheItem *measured = NULL; + if (context->measureTextHashMapInternalFreeList.length > 0) { + newItemIndex = Clay__int32_tArray_GetValue(&context->measureTextHashMapInternalFreeList, context->measureTextHashMapInternalFreeList.length - 1); diff --git a/src/string.h b/src/string.h new file mode 100644 index 0000000..e61d669 --- /dev/null +++ b/src/string.h @@ -0,0 +1,11 @@ +/* string.h — for freestanding wasm32, as included by xxhash.h */ + +#ifndef CLAYTERM_STRING_H +#define CLAYTERM_STRING_H + +#include "mem.h" + +/* Declared for XXH128_isEqual, which is never called; mem.c has no memcmp. */ +int memcmp(const void *a, const void *b, size_t n); + +#endif diff --git a/test/width.test.ts b/test/width.test.ts index 94a2083..eb3d02c 100644 --- a/test/width.test.ts +++ b/test/width.test.ts @@ -1,10 +1,38 @@ import { describe, expect, it } from "./suite.ts"; import { createTerm } from "../term.ts"; -import { close, grow, open, pack, text } from "../ops.ts"; +import { close, grow, type Op, open, pack, text } from "../ops.ts"; import { createTermNative } from "../term-native.ts"; import { print } from "./print.ts"; const decode = (bytes: Uint8Array) => new TextDecoder().decode(bytes); +const trim = (s: string) => s.split("\n").map((l) => l.trimEnd()).join("\n"); + +// Two distinct strings whose measurement-cache hashes collide under the +// default text config. Each must still measure and draw as itself. +const SHORT = "synthetic sample synthetic sample synthetic sample 022aa2"; +const LONG = + "synthetic sample synthetic sample synthetic sample synthetic sample synthetic sample 01fda4"; + +function boxes(contents: string[]): Op[] { + return [ + open("root", { + layout: { width: grow(), height: grow(), direction: "ttb" }, + }), + ...contents.flatMap((content, i) => [ + open(`box-${i}`), + text(content), + close(), + ]), + close(), + ]; +} + +// The visible picture of boxes(contents) on a w x h grid. +function picture(contents: string[], w: number, h: number): string { + let lines = contents.slice(0, h).map((c) => c.slice(0, w)); + while (lines.length < h) lines.push(""); + return lines.join("\n"); +} function indexOfSeq(haystack: Uint8Array, needle: number[]): number { outer: for (let i = 0; i + needle.length <= haystack.length; i++) { @@ -97,4 +125,33 @@ describe("width", () => { expect(indexOfSeq(out, [0xed, 0xa0, 0x80])).toBe(-1); expect(print(decode(out), 10, 3)).toContain("a�b"); }); + + describe("measurement cache", () => { + it("measures colliding strings as themselves in one frame", async () => { + let term = await createTerm({ width: 100, height: 3 }); + let result = term.render(boxes([SHORT, LONG])); + + expect(SHORT.length).toBe(57); + expect(LONG.length).toBe(91); + expect(result.info.get("box-0")?.bounds.width).toBe(57); + expect(result.info.get("box-1")?.bounds.width).toBe(91); + expect(trim(print(decode(result.output), 100, 3))).toEqual( + [SHORT, LONG, ""].join("\n"), + ); + }); + + it("measures colliding strings as themselves across renders", async () => { + let term = await createTerm({ width: 100, height: 3 }); + let screen = ""; + + for (let content of [SHORT, LONG, SHORT]) { + let result = term.render(boxes([content])); + screen += decode(result.output); + expect(result.info.get("box-0")?.bounds.width).toBe(content.length); + expect(trim(print(screen, 100, 3))).toEqual( + picture([content], 100, 3), + ); + } + }); + }); }); diff --git a/xxhash b/xxhash new file mode 160000 index 0000000..e626a72 --- /dev/null +++ b/xxhash @@ -0,0 +1 @@ +Subproject commit e626a72bc2321cd320e953a0ccf1584cad60f363