Repository navigation
perf(udf): keep one counting table in entropy instead of one per row - #45
Merged
Merged
Conversation
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>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What / why
The built-in
entropyUDF was slow in the static build: 200,000 rows took 23 seconds, about 8,600 rows a second, whererot13takes 3.7 seconds over the same input. The time went intomake-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.
entropy, v0.1.2 releaseentropy, this branchrot13, for referenceResults do not change, to the last digit. The sum is taken in the order
maphashvisits 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:clrhashkeeps 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.unwind-protect, so a row that fails cannot leave counts behind for the next one.Checked:
mainon macOS with ECL 26.5.5 and on Ubuntu 24.04 with ECL 21.2.1.builtin-udfsgains 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) andmake smoke; the two values printed forclickhouseandq7x9z2j4k8w1.evil.exampleare 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 testandmake smokepasssite/updated if behavior or docs changed (samples insite/src/samples.tsstay verbatim) (n/a: outputs are unchanged)🤖 Generated with Claude Code