Skip to content

Plain C API: tld.h is the source, ai module inside, ai_nearest - #5

Merged
TimoLassmann merged 5 commits into
mainfrom
feature/plain-c-api
Nov 29, 2026
Merged

TimoLassmann merged 5 commits into
mainfrom
feature/plain-c-api

Conversation

@TimoLassmann

Copy link
Copy Markdown
Owner

Merges feature/plain-c-api into main.

  • tld.h is the single source: src/ and the amalgamator are removed, tld_ai.h is now the ai module of tld.h.
  • Plain C error handling and a prefix on every public name.
  • ai_nearest is the search for embeddings (about 9 times faster).
  • Example host in the docs is http://localhost:11434.
  • .gitignore lists only files that exist.
  • tld_doc.h regenerated, which also fixes the docs-current and unit-doc failures on main after the history rewrite.

🤖 Generated with Claude Code

TimoLassmann and others added 5 commits November 28, 2026 07:37
No public macro returns, jumps or exits any more. RUN, ASSERT, ERROR_MSG and
the MMALLOC family leave the public API (the implementation keeps them as
TLD__RUN, TLD__ASSERT, ...); tld_error prints and does nothing else, and
tld_galloc is an expression that returns TLD_OK or TLD_FAIL.

OK/FAIL become TLD_OK/TLD_FAIL, the *_MSG macros become tld_log, tld_warn,
tld_debug, tld_section, tld_kv and tld_success, and galloc/gfree become
tld_galloc/tld_gfree.

- names: fails when a public macro returns, jumps or exits, even through a
  macro it uses
- misuse-rejected: an unchecked tld_galloc is rejected with -Wall -Werror
- tests keep their jumping checks as TEST_RUN, TEST_ASSERT and TEST_FAIL in
  tests/test.h
- tools/migrate.py renames the old names and rewrites each jumping macro to
  plain C that keeps the program's ERROR: labels
- tld_doc.h: the rule for names without '_' had nothing left to apply to and
  is removed; the corpus is regenerated (180 records)

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
tld.h was generated from the module files in src/ by tools/amalgamate.py, and
a test checked that the two agreed. It is now the file that is edited. The
code is the same: the preprocessed text of the new tld.h equals that of the
generated one, apart from line numbers.

- tld.h reads top to bottom: the top comment, the declarations of every
  module, then the implementation in the same order, each module under a
  "---- name ----" banner in both halves; one include block per half
- json types, json memory and json are one module, json; stats, quantile and
  auc are one module, stats (17 modules, no symbol changed)
- new MODULES section in the top comment; docs-current fails when it and the
  banners differ
- tld-dev compiles tld.h once and brings its directory as include directory
- unit-hash and regress-sort-cmp define TLD_IMPLEMENTATION themselves
- tools/migrate.py turns an include of a module header into tld.h
- single-header-current is removed (74 tests)

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
One header and one implementation macro: TLD_IMPLEMENTATION compiles
everything, AI_IMPLEMENTATION is gone, and tld_ai.h and docs/tld_ai.md are
removed. The names keep their prefix, ai_ and AI_.

- ai_defaults and ai_fill return TLD_OK or TLD_FAIL (was 0 or -1)
- AI_CHOICE and AI_SCALE define name_name(i) and name_count, so a program no
  longer builds the label-name table itself
- TLD_VERSION "0.5.0" replaces AI_VERSION
- the ai module comes in 13 parts ("ai: embeddings", ...), the sections
  tld_ai.h had; the manual and the corpus show a part as a module of its own
- the top comment of tld.h holds the sections of tld_ai.h's top comment
- docs/tld_card.md: the rules of tld.h on one page, generated from the top
  comment and checked by docs-current
- the declarations for an implementation included after system headers are
  removed with their test: tld.h comes first in its file
- names checks the ai module; its implementation-only macros are #undef'd
- tld_doc.h: the corpus covers one header (177 records)
- doc-examples runs the tld.h examples with the mock server; 73 tests

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Measured on 20,000 random unit vectors, the k-d tree beats comparing every
point up to about 8 dimensions, is slower from 16 and visits every point at
768. A tree does not help with embeddings, and the header now says so.

- kdtree: the module comment says the tree is for points of few dimensions
  and names ai_nearest for embeddings; the code is unchanged
- ai_nearest computes the length of the query once instead of once per row
- ai_cosine and ai_nearest add vectors of 32 or more floats in 32 partial
  sums of floats: one query against 20,000 vectors of 768 floats takes
  1.7 ms (was 16 ms); shorter vectors keep the sum in double
- the values are within 1e-6 of the sums in double they replace, never
  outside [-1, 1], and a score of ai_nearest equals ai_cosine of that row
- new test ai-nearest: both functions against sums in double on 16 vector
  lengths from 1 to 4096 (74 tests)

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@TimoLassmann
TimoLassmann merged commit 163b0c1 into main Nov 29, 2026
4 of 9 checks passed
TimoLassmann added a commit that referenced this pull request Nov 29, 2026
…ers once

Both workflows failed on main after #5.

- misuse-rejected failed on every gcc job: check.sh used -fsyntax-only, and
  gcc reports an unused warn_unused_result value only when it generates
  code, so warn_galloc_unchecked.c compiled. check.sh now compiles each case
  with -c -o /dev/null; tld_galloc itself was right.
- oom-tld timed out under MemorySanitizer: with the ai module in tld.h the
  manual has 177 entries, and tld__doc_entry allocated 4 string buffers per
  entry, so the doc_manual sweep grew from 360 to 1439 allocations, each
  rerunning the whole manual (54 s under MSan locally, was 2.9 s).
  tld__doc_entry now takes work buffers that tld_doc_manual and
  tld__doc_render allocate once: 26 allocations, 0.7 s. The text is
  unchanged (docs-unchanged passes).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TimoLassmann
TimoLassmann deleted the feature/plain-c-api branch November 29, 2026 08:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant