Add FNV-1a string hash and string intern table #3

Merged
fputs merged 1 commit from fputs/intern into master 2026-11-05 09:58:50 -08:00 AGit
Owner

Adds FNV-1a 64 lfstr_hash, lfstr_hash_cb, and lfstr_match, plus an LfIntern table.

The hash is xor-then-multiply with the standard offset basis and prime, narrowed to size_t. There is no seed. An empty view returns the offset basis and does not read p. lfstr_match follows the hash-map convention: 0 means equal.

LfIntern is a complete struct so the caller can own the handle. map and slabs are private. Each distinct string is its own block (the bytes, a trailing NUL that is not counted in n, and the stored LfStr). A later intern does not move earlier pointers. Duplicates, including the empty string, return the same pointer and do not allocate again. destroy frees the blocks. The caller does not free an interned p.

flint_alloc_fail does not link libflint, so that test also builds string.c and memory.c (the string object references arena_malloc). The armed failure is the second distinct string's malloc. The first string stays readable and the output sentinel is unchanged.

Tested with -DCMAKE_BUILD_TYPE=Debug -DFLINT_SANITIZE=ON -DCMAKE_C_EXTENSIONS=OFF. ctest --label-exclude slow: 26/26 passed.

Adds FNV-1a 64 `lfstr_hash`, `lfstr_hash_cb`, and `lfstr_match`, plus an `LfIntern` table. The hash is xor-then-multiply with the standard offset basis and prime, narrowed to `size_t`. There is no seed. An empty view returns the offset basis and does not read `p`. `lfstr_match` follows the hash-map convention: `0` means equal. `LfIntern` is a complete struct so the caller can own the handle. `map` and `slabs` are private. Each distinct string is its own block (the bytes, a trailing NUL that is not counted in `n`, and the stored `LfStr`). A later intern does not move earlier pointers. Duplicates, including the empty string, return the same pointer and do not allocate again. `destroy` frees the blocks. The caller does not free an interned `p`. `flint_alloc_fail` does not link libflint, so that test also builds `string.c` and `memory.c` (the string object references `arena_malloc`). The armed failure is the second distinct string's `malloc`. The first string stays readable and the output sentinel is unchanged. Tested with `-DCMAKE_BUILD_TYPE=Debug -DFLINT_SANITIZE=ON -DCMAKE_C_EXTENSIONS=OFF`. `ctest --label-exclude slow`: 26/26 passed.
Add FNV-1a string hash and string intern table
All checks were successful
test / test (pull_request) Successful in 12s
test / sanitize (pull_request) Successful in 15s
cde39a5633
fputs force-pushed fputs/intern from cde39a5633
All checks were successful
test / test (pull_request) Successful in 12s
test / sanitize (pull_request) Successful in 15s
to e4f268108c
All checks were successful
test / test (pull_request) Successful in 16s
test / sanitize (pull_request) Successful in 16s
2026-11-05 09:39:14 -08:00
Compare
fputs force-pushed fputs/intern from e4f268108c
All checks were successful
test / test (pull_request) Successful in 16s
test / sanitize (pull_request) Successful in 16s
to af62840717
All checks were successful
test / test (pull_request) Successful in 15s
test / sanitize (pull_request) Successful in 16s
2026-11-05 09:58:17 -08:00
Compare
fputs merged commit 77d3f77074 into master 2026-11-05 09:58:50 -08:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
fputs/libflint!3
No description provided.