Repository navigation
Fix text measurement cache collisions #173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
taras
wants to merge
1
commit into
bombshell-dev:main
Choose a base branch
from
taras:codex/text-measure-cache-collisions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| diff --git a/clay.h b/clay.h | ||
| index 6452cd6..1a29854 100644 | ||
| --- a/clay.h | ||
| +++ b/clay.h | ||
| @@ -1299,6 +1299,11 @@ typedef struct { | ||
| int32_t measuredWordsStartIndex; | ||
| float minWidth; | ||
| bool containsNewlines; | ||
| + // Cache key: the hash alone can collide, so lookups also compare these | ||
| + Clay_String cacheText; | ||
| + uint16_t cacheFontId; | ||
| + uint16_t cacheFontSize; | ||
| + uint16_t cacheLetterSpacing; | ||
| // Hash map data | ||
| uint32_t id; | ||
| int32_t nextIndex; | ||
| @@ -1638,6 +1643,12 @@ Clay__MeasuredWord *Clay__AddMeasuredWord(Clay__MeasuredWord word, Clay__Measure | ||
| } | ||
| } | ||
|
|
||
| +bool Clay__MemCmp(const char *s1, const char *s2, int32_t length); | ||
| + | ||
| +// Returns storage holding a copy of the key that stays readable for as long as | ||
| +// the published cache entry, given the owner from errorHandler.userData. | ||
| +const char *clayterm_copy_text_key(void *userData, const char *chars, int32_t length); | ||
| + | ||
| Clay__MeasureTextCacheItem *Clay__MeasureTextCached(Clay_String *text, Clay_TextElementConfig *config) { | ||
| Clay_Context* context = Clay_GetCurrentContext(); | ||
| #ifndef CLAY_WASM | ||
| @@ -1658,7 +1669,12 @@ Clay__MeasureTextCacheItem *Clay__MeasureTextCached(Clay_String *text, Clay_Text | ||
| 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->id == id && | ||
| + hashEntry->cacheText.length == text->length && | ||
| + hashEntry->cacheFontId == config->fontId && | ||
| + hashEntry->cacheFontSize == config->fontSize && | ||
| + hashEntry->cacheLetterSpacing == config->letterSpacing && | ||
| + Clay__MemCmp(hashEntry->cacheText.chars, text->chars, text->length)) { | ||
| hashEntry->generation = context->generation; | ||
| return hashEntry; | ||
| } | ||
| @@ -1689,7 +1705,12 @@ Clay__MeasureTextCacheItem *Clay__MeasureTextCached(Clay_String *text, Clay_Text | ||
| } | ||
|
|
||
| int32_t newItemIndex = 0; | ||
| - Clay__MeasureTextCacheItem newCacheItem = { .measuredWordsStartIndex = -1, .id = id, .generation = context->generation }; | ||
| + Clay__MeasureTextCacheItem newCacheItem = { | ||
| + .measuredWordsStartIndex = -1, .id = id, .generation = context->generation, | ||
| + .cacheText = { .length = text->length, .chars = NULL }, | ||
| + .cacheFontId = config->fontId, .cacheFontSize = config->fontSize, | ||
| + .cacheLetterSpacing = config->letterSpacing | ||
| + }; | ||
| Clay__MeasureTextCacheItem *measured = NULL; | ||
| if (context->measureTextHashMapInternalFreeList.length > 0) { | ||
| newItemIndex = Clay__int32_tArray_GetValue(&context->measureTextHashMapInternalFreeList, context->measureTextHashMapInternalFreeList.length - 1); | ||
| @@ -1771,6 +1792,9 @@ Clay__MeasureTextCacheItem *Clay__MeasureTextCached(Clay_String *text, Clay_Text | ||
| measured->unwrappedDimensions.width = measuredWidth; | ||
| measured->unwrappedDimensions.height = measuredHeight; | ||
|
|
||
| + // Only an entry linked below can be found again, so only it keeps a key. | ||
| + measured->cacheText.chars = clayterm_copy_text_key(context->errorHandler.userData, text->chars, text->length); | ||
| + | ||
| if (elementIndexPrevious != 0) { | ||
| Clay__MeasureTextCacheItemArray_Get(&context->measureTextHashMapInternal, elementIndexPrevious)->nextIndex = newItemIndex; | ||
| } else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1256,6 +1256,15 @@ counted as separate cells in layout. Measurement and rendering MUST agree: if | |
| the measurer ignores a combining mark for width purposes, the renderer MUST | ||
| still attach and emit it. | ||
|
|
||
| **Measurement cache.** Clay caches each text's measurement under a hash of its | ||
| content and text config. A local Clay patch reuses a cached measurement only | ||
| when the text bytes and config also match, so strings whose hashes collide are | ||
| each measured as themselves. A frame's text bytes do not outlive its render, so | ||
| each renderer instance copies the keys of cached measurements into a fixed | ||
| region of its own state, clearing Clay's cache before any layout whose text | ||
| might not fit there. A frame too large for that region keys its measurements on | ||
| its own bytes, and the cache is cleared once that frame is presented. | ||
|
|
||
|
Comment on lines
+1259
to
+1267
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I can tell, this is all a private implementation detail and so should not be in the render-spec |
||
| **Cell representation.** Each cell in the buffer stores a grapheme cluster — a | ||
| base Unicode codepoint plus up to 8 combining-mark codepoints — together with a | ||
| foreground color (packed ARGB with attribute flags in the high byte) and a | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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