Skip to content

perf(udf): keep one counting table in entropy instead of one per row - #45

Merged
juliojimenez merged 1 commit into
mainfrom
julio/entropy-reuse-table
Nov 18, 2026
Merged

juliojimenez merged 1 commit into
mainfrom
julio/entropy-reuse-table

Conversation

@juliojimenez

Copy link
Copy Markdown
Member

What / why

The built-in entropy UDF was slow in the static build: 200,000 rows took 23 seconds, about 8,600 rows a second, where rot13 takes 3.7 seconds over the same input. The time went into make-hash-table. ECL's default table has 1024 buckets, the function made one for every row, and in the musl build that costs about 100 microseconds, roughly five times the cost of the row loop itself. The glibc build pays far less for it, which is why it only showed in the release binary.

The function now keeps one table and clears it after each row.

200,000 rows, static aarch64 binary Time
entropy, v0.1.2 release 23.2 s
entropy, this branch 5.7 s
rot13, for reference 3.7 s

Results do not change, to the last digit. The sum is taken in the order maphash visits the counts, and that order matters: a version that summed in order of first appearance changed the last digit for 57,165 of 160,007 test strings, including the second value the README quotes. The kept table starts every row empty and at the default size, exactly as a fresh one would, so the order is the same. Two details make that hold:

  • clrhash keeps a table that grew at its new size. A string with several hundred distinct characters grows it, and in that case the table is replaced with a fresh one before the next row.
  • The clearing is in an unwind-protect, so a row that fails cannot leave counts behind for the next one.

Checked:

  • The output of this branch is byte for byte the output of the v0.1.2 release binary over 160,007 strings (dictionary-word domains, random labels, random printable ASCII up to 120 characters). The same holds against main on macOS with ECL 26.5.5 and on Ubuntu 24.04 with ECL 21.2.1.
  • builtin-udfs gains six assertions that compare the function with a fresh-table reference over a sequence that includes a 2,000-character string to force the growth. Removing the replacement of a grown table, or the clearing, makes them fail.
  • make test (231 assertions) and make smoke; the two values printed for clickhouse and q7x9z2j4k8w1.evil.example are unchanged.

The timings are from Docker on an Apple Silicon Mac, with the binaries built by scripts/build-static.sh.

One thing to know: the function now keeps state between calls, so it is not safe to call from two threads at once. The UDF loop is single-threaded.

Checklist

  • make test and make smoke pass
  • README.md and site/ updated if behavior or docs changed (samples in site/src/samples.ts stay verbatim) (n/a: outputs are unchanged)
  • Tests added/updated for compiler, rules, or UDF changes
  • Code stays portable to ECL 21.2.1 (suite run on 21.2.1, outputs compared there too)

🤖 Generated with Claude Code

The built-in entropy UDF made a hash table for every row. ECL's default
table has 1024 buckets, and in the static (musl) build allocating one
costs about 100 microseconds, so 200,000 rows took 23 seconds. One table
is now kept and cleared after each row: 5.7 seconds for the same input.

Results are unchanged to the last digit. The kept table starts every row
empty and at its default size, like a fresh one, so maphash visits the
counts in the same order; a table that grew is replaced, and the
clearing sits in an unwind-protect.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@juliojimenez juliojimenez self-assigned this Nov 18, 2026
@juliojimenez juliojimenez added the chore Chore label Nov 18, 2026
@juliojimenez
juliojimenez merged commit 61b45da into main Nov 18, 2026
6 checks passed
@juliojimenez
juliojimenez deleted the julio/entropy-reuse-table branch November 18, 2026 04:22
@juliojimenez juliojimenez mentioned this pull request Nov 18, 2026
4 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

chore Chore

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant