Repository navigation
Integer.sqrt: Karatsuba square root; String#hash: SipHash-1-3 for long ASCII strings - #202
Conversation
…2**106 Newton's iteration from the double's root (#190) made several full-width BigInteger divisions per call, and .NET 11's division is slow. - 2**62..2**106: the double's root of the value as a UInt128 is off by a few units at most; it is corrected to the exact floor with UInt128 squares. No BigInteger division at all. - Above: Zimmermann's Karatsuba square root with remainder (Brent and Zimmermann, Modern Computer Arithmetic, algorithm 1.12; GMP's mpz_sqrtrem). Each level takes the root of the top half and gets the next k bits in one division of half the size, and the remainder in one squaring; the split keeps the top part >= B**2/4, so at most one correction follows. SqrtSeed and its test go; Integer_SqrtRem1 checks s*s + r == n and 0 <= r <= 2s from 1 to 600 bits (random, k*k and its neighbours, powers of two) and up to a million bits. Part of #89. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…UTF-8 bytes - Long (128+) ASCII strings hashed their bytes with Marvin (#195). They now use SipHash-1-3 under a random 128-bit key drawn once per process, as CRuby does: about 1.5 times as fast per byte, and as hard to flood without the key. Short strings and non-ASCII characters keep String.GetHashCode (Marvin), so short Hash keys are unchanged. The even-length ASCII string that collided with the UTF-16 string its bytes spell, whatever the seed, no longer does: the two hashes are now different keyed functions. - A non-ASCII UTF-8 string held as bytes validated them three times (the escaping encoding's count and decode each check them again) before hashing the characters. It now decodes once with Utf8.ToUtf16 and falls back to the escaping decoder only for invalid bytes. Same hash, since the escaping decoder hands valid UTF-8 to the standard one. MutableString_SipHash1 checks the implementation against a plain one with the round counts as parameters, which gives the SipHash paper's 2-4 vectors. Part of #89. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Brings in #202 (Integer.sqrt, String#hash) and #204 (exception fixes), which change ruby4.rb and the code the startup library compiles. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Part of #89: the
Integer.sqrton large Integers item and theString#hashon long strings item.1.
Integer.sqrt: Karatsuba square root, and no BigInteger at all below 2**106#190 started Newton's iteration from the double's square root. That still meant several full-width BigInteger divisions per call, and on .NET 11.0.0 BigInteger division is 3–6× slower than on .NET 10.
UInt128) is off by a few units at most, and it is corrected to the exact floor withUInt128squares. No BigInteger arithmetic.Integer.SqrtRem, Zimmermann's Karatsuba square root with remainder. This is Brent and Zimmermann, Modern Computer Arithmetic, algorithm 1.12, the same algorithm as GMP'smpz_sqrtrem.SqrtSeed(#89: Integer#[], Integer.sqrt, pow(b, m), Kernel#Integer parsing and tail calls, Data.define #190) and its test are gone. Nothing else used it.2.
String#hash: SipHash-1-3 for long ASCII strings, and one-pass UTF-8 decodingRandomNumberGenerator). This is CRuby's string hash, and Python's and Rust's.String.GetHashCode(Marvin), so short Hash keys are unchanged.EscapingEncoding's count and its decode each check again).Utf8.ToUtf16. Only invalid bytes fall back to the escaping decoder.("é" * 500).hashtakes about half as long as before (it is held as bytes).XxHash3: not in the box (a NuGet dependency), and it makes no claim to resist hash flooding.#hash. .NET validates non-ASCII UTF-8 at ~0.6 ns/byte, so a byte-held one could not get much below ~3× CRuby anyway.Measurements
Release build. Thread CPU time,
tasksetto one core, ≥0.6 s warm-up per case, best of 5. Three interleaved rounds per build: CRuby, then master and branch for net10.0, then the same for net11.0. The table shows the range of the three bests, in ns unless marked µs.The box was heavily loaded by other jobs during these runs (load average 7–25), so the spread is wide. Read the low ends.
Integer.sqrt(2**70)Integer.sqrt(2**100 + c)Integer.sqrt(2**2000 + 1)Integer.sqrt20k bitsInteger.sqrt100k bitshash1 KB ASCIIhash1 KB ASCII, BINARYhash1 KB UTF-8 ("é" * 500)hash100 KB ASCIIhash100 KB ASCII, BINARYhash1 MB ASCIICRuby's figures include ~14 ns of
whileloop, IronRuby's ~2 ns.Short keys were measured separately, in four interleaved rounds on another core. They are unchanged:
"abcdefghijklmnop".hash.hashh["key_500"](1000 keys)On a quiet run, sqrt by size (steady state, net10.0 / net11.0, ns): 120 bits 734 / 767, 250 bits 1447 / 1574, 1000 bits 2569 / 2843, 2000 bits 4090 / 4201, 8000 bits 9604 / 7478. CRuby on the same run: 806, 1555, 3932, 6549, 24406.
What is left above ~10k bits is .NET's BigInteger multiply and divide against GMP, with net11.0's slower division on top.
Verification
Encoding.listentry × 8 ways to hold them, plus CLR strings: 16,102 strings and 2,440,776eql?pairs, with 0 hash mismatches on net10.0 and on net11.0.encodeandencodeswitched to characters.Integer_SqrtRem1: s·s + r == n and 0 ≤ r ≤ 2s for 1–600 bits (random values, powers of two ± 1, k², k² ± 1, k² + 2k, k² + 2k + 1) and up to 1M bits. It replacesInteger_SqrtSeedHuge1.MutableString_SipHash1: checks the SipHash-1-3 implementation against a plain one with the round counts as parameters. That plain one reproduces the SipHash paper's 2-4 test vectors.Integer_SqrtLarge1,MutableString_GetHashCode2/3unchanged and passing.try_convert"into"/"to" wording errors (2 in integer, 1 in string, 1 in hash).[RubyMethod]signature changed, so the initializers need no regeneration.Review
I ran the repo's
reviewskill as an independent pass. It found no Blockers, no Should-fixes and no Notes. Beyond the checks above, it compared sqrt against CRuby on 26,255 more values (every size 60–1200 bits, with s² neighbours) on both runtimes. It also checkedeql?/hash on CLR strings, BOMs, astral characters, offset slices and invalid UTF-8.One problem came up along the way, before the PR was opened: my edits had turned six CRLF files into LF. I restored CRLF, so the diff is only the real change (~270 lines).
🤖 Generated with Claude Code


Confidence Score: 5/5
The PR appears safe to merge; no actionable issues were found.Summary
This PR speeds up large integer square roots and string hashes.
No actionable issues were found. This review checked source code and existing tests; it did not run the test suite.
Reviews (1) · Last reviewed commit: "String#hash: SipHash-1-3 for long ASCII ..." · Reviewed by Greptile
Summary by CodeRabbit