Repository navigation
fix(snapshot): pick a new Set.of and Map.of iteration salt on every start from a boot snapshot, as HotSpot does - #118
Conversation
…tart from a boot snapshot, as HotSpot does
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 17 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
There was a problem hiding this comment.
Extracts System.nanoTime into a shared helper used by both the native and the snapshot restore, and reseeds ImmutableCollections.SALT32L and REVERSE from nanoTime() on every snapshot restore so Set.of and Map.of iteration order varies between starts like HotSpot. Adds a SetOrder test that boots twice to take a snapshot, restores four times, and asserts more than one iteration order appears.
review-bot, model nvidia/nemotron-3.5-super-vl:free, verdict approve
There was a problem hiding this comment.
Exports a shared nanoTime helper from src/natives/lang.ts and wires it into System.nanoTime(). src/snapshot.ts adds reseedImmutableCollections, invoked from restoreVM, which rewrites ImmutableCollections.SALT32L and REVERSE from nanoTime() so each restored start gets a fresh iteration salt as HotSpot does. Adds test/snapshot/SetOrder.java and a snapshot test that boots twice, restores four times, and requires more than one Set.of iteration order.
review-bot, model nvidia/nemotron-3.5-super-vl:free, verdict approve
There was a problem hiding this comment.
The PR reseeds ImmutableCollections.SALT32L and REVERSE from System.nanoTime() on every snapshot restore, matching HotSpot's per-start iteration salt. It extracts nanoTime as a shared helper used by both the System.nanoTime() native and the restore path, and adds a test that boots SetOrder twice, snapshots, restores four times, and asserts the iteration order changes.
review-bot, model nvidia/nemotron-3.5-super-vl:free, verdict approve
What this changes
The OpenJDK test
java/util/Collection/RandomizedIteration.javafailed on #116's CI run ("iteration order not randomized!"). It starts five VMs, each writing the iteration order of aSet.ofand aMap.of, and requires the orders to differ.ImmutableCollectionspicks the salt behind that order fromSystem.nanoTime()in its<clinit>, which HotSpot runs on every start. java-web's boot snapshot froze the salt: only the first two of the five starts booted, and the other three restored the second one's salt. Two samples give the same order about 1.6% of the time with that test's 63 words, so the test failed now and then.Restoring a snapshot now sets
ImmutableCollections.SALT32LandREVERSEthe way<clinit>does, fromSystem.nanoTime(). The salt only picks where iteration starts and in which direction;SetNandMapNplace and find elements byhashCode()alone, so the sets and maps already in the snapshot stay valid. HotSpot does the same with its archived module graph: its archived sets are iterated with the salt of the current run.nanoTimeis now one helper that the native and the restore share.Tests
test/snapshot.test.ts: bootSetOrdertwice to take a snapshot, restore it four times (checked by the snapshot file's mtime and inode), and require more than one iteration order of a 20-elementSet.of. It passes with the change and fails without it ("one order from every start").test/snapshot.test.ts(8 tests),test/warm-snapshot.test.tsandtest/snapshot-depth.test.tspass.pnpm typecheckpasses.RandomizedIteration.java, since it changessrc/snapshot.ts.