From 47a4700bee6d91836a85e23c24033ab3f7cdfafd Mon Sep 17 00:00:00 2001 From: Algent Date: Wed, 2 Sep 2026 16:51:21 +0200 Subject: [PATCH] refactor: port scratch bit set to Java --- JAVA_MIGRATION.md | 9 +++++ JAVA_MIGRATION_DIVERGENCES.md | 39 +++++++++++++++++++ JAVA_MIGRATION_HANDOFF.md | 24 +++++++----- .../multipart/asm/ScratchBitSet$class.java | 29 ++++++++++++++ .../multipart/asm/ScratchBitSet.java | 16 ++++++++ .../multipart/asm/ScratchBitSet.scala | 20 ---------- 6 files changed, 108 insertions(+), 29 deletions(-) create mode 100644 src/main/scala/codechicken/multipart/asm/ScratchBitSet$class.java create mode 100644 src/main/scala/codechicken/multipart/asm/ScratchBitSet.java delete mode 100644 src/main/scala/codechicken/multipart/asm/ScratchBitSet.scala diff --git a/JAVA_MIGRATION.md b/JAVA_MIGRATION.md index 8419c7c..8525fb2 100644 --- a/JAVA_MIGRATION.md +++ b/JAVA_MIGRATION.md @@ -1221,3 +1221,12 @@ generated-trait compatibility work. Scala-runtime removal remains a later projec - A clean build passes all 262 plain-JVM tests and the Java 8 Forge server passes all 116 tests. Both supported public surfaces match the reference. `multipart/asm/ScratchBitSet.scala` is the next isolated support target; the generated microblock traits still require the documented abstract-Java-mixin and side-only-member prerequisites. +- Characterized `ScratchBitSet` against untouched Scala. Four plain-JVM cases freeze the exact interface/helper + surface, lazy allocation, repeated accessor calls, owner/thread isolation, bit preservation/clearing, storage + replacement/reinitialization and `freshBitSet` dispatch through an overridden `getBitSet`. +- Ported the interface and `$class` helper directly to Java without changing either generator. All seven callable + methods, their abstract/static modifiers and both binary names remain exact; no new API or default methods are + introduced. Neither downstream audit contains a reference to this support trait. +- A clean build passes all 266 plain-JVM tests and the Java 8 Forge server passes all 116 tests. The two-type ABI and + both generator companions' disassembly match the reference. `multipart/asm/ByteCodecs.scala` is next as an isolated + codec port, leaving signature parsing and trait compilation for separate targets. diff --git a/JAVA_MIGRATION_DIVERGENCES.md b/JAVA_MIGRATION_DIVERGENCES.md index 0c1b765..bbe8bc9 100644 --- a/JAVA_MIGRATION_DIVERGENCES.md +++ b/JAVA_MIGRATION_DIVERGENCES.md @@ -3093,3 +3093,42 @@ a `LinkedHashSet`; part iteration uses the existing Scala iterator without extra - Java 8 Forge dedicated-server suite: 116 tests, 0 failures, 0 errors. - Raw `javap` comparison confirms identical callable public names/descriptors and the expected six-closure removal. - Real client conversion packets/render rebinding and GuideNH/Schematica previews remain manual checkpoints. + +## 2026-09-02 — Scratch bit-set Java port + +`ScratchBitSet` and `ScratchBitSet$class` are now Java. The multipart and microblock generator companions continue +using the same interface, storage accessors and helper calls without any source changes. + +### Observable behavior + +No known runtime divergence. Initialization installs a new ordinary `ThreadLocal` through the public setter; the +first read on each thread lazily allocates a `BitSet`, and subsequent reads reuse it without clearing. Each owner and +thread has independent storage. `freshBitSet` clears and returns the same set via the owner's virtual `getBitSet`, +preserving overrides instead of directly calling the helper's implementation. + +The lazy-allocation path still reads the storage accessor twice. Public setter replacement and repeated `$init$` +calls still replace storage, without eagerly allocating a set or clearing the previous storage's contents. + +### ABI and source compatibility + +The interface retains its four abstract methods, including the exact mangled getter and raw `ThreadLocal` setter. +The abstract helper class retains all three static methods and their descriptors. Neither type acquires fields, +and both generator companions have unchanged `javap -p -c -s` output. Neither downstream audit identifies a direct +consumer of this trait, so no new frozen Scala consumer is needed. + +Accepted classfile differences are removed Scala marker/signature attributes and a private Java helper constructor +(the original helper had no constructor). Recompiled Scala can no longer inherit implementations merely by mixing +in this Java interface: it must implement the methods and initialize storage explicitly, as the existing Java +generator companions do. Previously compiled helper calls retain their binary entry points. + +### Compiler artifacts + +None. The reference and port jars contain the same two `ScratchBitSet` classes. + +### Validation + +- `ScratchBitSetCharacterizationTest`: 4 plain-JVM tests, unchanged from the Scala baseline. +- Clean complete plain-JVM suite: 266 tests, 0 failures, 0 errors. +- Java 8 Forge dedicated-server suite: 116 tests, 0 failures, 0 errors, including both generator families. +- Raw `javap` comparison confirms the same public names, descriptors, modifiers and class list. +- No new manual checkpoint; existing generator/client integration checks remain on the checklist. diff --git a/JAVA_MIGRATION_HANDOFF.md b/JAVA_MIGRATION_HANDOFF.md index 5ddd497..a582ca9 100644 --- a/JAVA_MIGRATION_HANDOFF.md +++ b/JAVA_MIGRATION_HANDOFF.md @@ -12,8 +12,8 @@ what breaks, and what is left. The other documents hold the reasoning. | `JAVA_MIGRATION_MANUAL_CHECKS.md` | What no automated test can cover, and must be checked by hand in a client | | `JAVA_MIGRATION_PROFILE.md` | The focused baseline, first measured result, findings, and exact rerun command | -Branch: `algent/java`. Base: `master`. 150 commits including the API-cleanup plan and separate characterization and -port commits through the multipart generator. +Branch: `algent/java`. Base: `master`. 152 commits including the API-cleanup plan and separate characterization and +port commits through the scratch bit-set helper. ## The one rule that matters @@ -67,7 +67,7 @@ awk -F'"' '/()); + } +} diff --git a/src/main/scala/codechicken/multipart/asm/ScratchBitSet.java b/src/main/scala/codechicken/multipart/asm/ScratchBitSet.java new file mode 100644 index 0000000..908bb44 --- /dev/null +++ b/src/main/scala/codechicken/multipart/asm/ScratchBitSet.java @@ -0,0 +1,16 @@ +package codechicken.multipart.asm; + +import java.util.BitSet; + +/** Per-owner, per-thread scratch storage used by both generators. */ +public interface ScratchBitSet { + + void codechicken$multipart$asm$ScratchBitSet$_setter_$codechicken$multipart$asm$ScratchBitSet$$bitSets_$eq( + ThreadLocal value); + + ThreadLocal codechicken$multipart$asm$ScratchBitSet$$bitSets(); + + BitSet getBitSet(); + + BitSet freshBitSet(); +} diff --git a/src/main/scala/codechicken/multipart/asm/ScratchBitSet.scala b/src/main/scala/codechicken/multipart/asm/ScratchBitSet.scala deleted file mode 100644 index 670ecfa..0000000 --- a/src/main/scala/codechicken/multipart/asm/ScratchBitSet.scala +++ /dev/null @@ -1,20 +0,0 @@ -package codechicken.multipart.asm -import java.util.BitSet - -trait ScratchBitSet { - private val bitSets = new ThreadLocal[BitSet] - - def getBitSet = bitSets.get match { - case null => - val bitset = new BitSet - bitSets.set(bitset) - bitset - case bitset => bitset - } - - def freshBitSet = { - val bitset = getBitSet - bitset.clear() - bitset - } -}