Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/measure-cache-collisions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changeset needs revision.

Changeset package scope does not match the affected packages confidently. Changeset is not sufficiently consumer-focused.

View the proposed replacement
---
'@bomb.sh/tty': patch
---

TODO: Write changeset message (inference failed: HTTP 401: {"error":{"message":"User not found.","code":401}})

"@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.
2 changes: 2 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
paths:
- "src/**"
- "clay" # submodule pointer
- "xxhash" # submodule pointer
- "patches/**"
- "Makefile"
- "*.ts"
Expand All @@ -22,6 +23,7 @@ on:
paths:
- "src/**"
- "clay" # submodule pointer
- "xxhash" # submodule pointer
- "patches/**"
- "Makefile"
- "*.ts"
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -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
13 changes: 7 additions & 6 deletions BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:

Expand All @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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:

Expand Down
4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
}
72 changes: 72 additions & 0 deletions patches/clay-measure-text-cache-key.patch
Original file line number Diff line number Diff line change
@@ -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);
11 changes: 11 additions & 0 deletions src/string.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* string.h — <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
59 changes: 58 additions & 1 deletion test/width.test.ts
Original file line number Diff line number Diff line change
@@ -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++) {
Expand Down Expand Up @@ -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),
);
}
});
});
});
1 change: 1 addition & 0 deletions xxhash
Submodule xxhash added at e626a7
Loading