Skip to content

[Core] Preserve text measurements across 32-bit hash collisions #664

Open

Description

@taras

Two distinct, immutable strings can receive the same text width when their 32-bit measurement-cache IDs collide. This reproduces on e6cc36941ab2af5d81107617039d6f527a1c660b (current main) with SIMD disabled and a measurement callback that returns the slice length. Both source buffers stay valid and unchanged throughout the test.

For the ASCII strings below, the cache returns widths 57, 57. After resetting it, the longer string correctly measures 91. Both cache IDs are 4113376897.

Reproduce

Save this as repro.c beside clay.h. It calls the internal cache function directly to keep the example small.

#define CLAY_IMPLEMENTATION
#define CLAY_DISABLE_SIMD
#include "clay.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static Clay_Dimensions measure(Clay_StringSlice text,
                               Clay_TextElementConfig *config, void *userData) {
    (void)config;
    (void)userData;
    return (Clay_Dimensions){.width = (float)text.length, .height = 1};
}

int main(void) {
    uint32_t bytes = Clay_MinMemorySize();
    void *memory = malloc(bytes);
    if (!memory) return 2;
    Clay_Initialize(Clay_CreateArenaWithCapacityAndMemory(bytes, memory),
                    (Clay_Dimensions){200, 40}, (Clay_ErrorHandler){0});
    Clay_SetMeasureTextFunction(measure, NULL);
    const char *shortText = "synthetic sample synthetic sample synthetic sample 022aa2";
    const char *longText = "synthetic sample synthetic sample synthetic sample synthetic sample synthetic sample 01fda4";
    Clay_String a = {.length = (int32_t)strlen(shortText), .chars = shortText};
    Clay_String b = {.length = (int32_t)strlen(longText), .chars = longText};
    Clay_TextElementConfig config = {.fontSize = 1};
    uint32_t hashA = Clay__HashStringContentsWithConfig(&a, &config);
    uint32_t hashB = Clay__HashStringContentsWithConfig(&b, &config);
    float first = Clay__MeasureTextCached(&a, &config)->unwrappedDimensions.width;
    float cached = Clay__MeasureTextCached(&b, &config)->unwrappedDimensions.width;
    Clay_ResetMeasureTextCache();
    float fresh = Clay__MeasureTextCached(&b, &config)->unwrappedDimensions.width;
    printf("hashes: %u %u\nwidths: %.0f %.0f; after reset: %.0f\n",
            hashA, hashB, first, cached, fresh);
    int reproduced = hashA == hashB && first == 57 && cached == 57 && fresh == 91;
    free(memory);
    return reproduced ? 0 : 1;
}
curl -L https://raw.githubusercontent.com/nicbarker/clay/e6cc36941ab2af5d81107617039d6f527a1c660b/clay.h -o clay.h
clang -O2 repro.c -o repro
./repro

Observed on macOS arm64:

hashes: 4113376897 4113376897
widths: 57 57; after reset: 91

Exit zero means this program reproduced the defect. Expected cache results are 57, 91, without needing a reset.

Cache identity

Clay__MeasureTextCached accepts an entry solely when its 32-bit ID matches. There is no collision check against the text. The hash can select a bucket, but matching it does not establish text identity. Any retained comparison key also needs a lifetime that remains valid across the frames where it is cached.

The same public string pair reproduces through @bomb.sh/tty rendering, causing incorrect fit widths and lost text tails. A focused tty fix with owned, bounded key storage and regression coverage is being prepared. The scalar repro above keeps the report independent of that integration and of SIMD hashing concerns such as #468.

A consumer-side fix with synthetic renderer regressions is proposed in bombshell-dev/tty#173. It keeps Clay’s pinned revision and uses tty-owned bounded key storage; it does not resolve this generic core report.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions