From 56b7a4791a0c8f90bc679187b432ae093e7516cf Mon Sep 17 00:00:00 2001 From: Algent Date: Thu, 27 Aug 2026 22:54:44 +0300 Subject: [PATCH] perf: remove multipart traversal copies --- JAVA_MIGRATION_DIVERGENCES.md | 15 +++++++- JAVA_MIGRATION_HANDOFF.md | 25 ++++++------ JAVA_MIGRATION_PROFILE.md | 38 +++++++++++++++---- .../codechicken/multipart/TileMultipart.java | 23 +++++++++-- 4 files changed, 78 insertions(+), 23 deletions(-) diff --git a/JAVA_MIGRATION_DIVERGENCES.md b/JAVA_MIGRATION_DIVERGENCES.md index 76af5f1..2273120 100644 --- a/JAVA_MIGRATION_DIVERGENCES.md +++ b/JAVA_MIGRATION_DIVERGENCES.md @@ -669,7 +669,20 @@ traversal. That read-side cost is not required by the reference and is the next `operate` keeps its `(Lscala/Function1;)V` descriptor and internal callers still go through it, wrapping their action in an `AbstractFunction1`. That preserves both the virtual dispatch, so any trait overriding `operate` still sees -every call, and the reference's per-call allocation. +every call, and the reference's callback-adapter call shape. + +### 2026-08-27 Phase 4 traversal optimization + +The initial Java port implemented every read through `parts()`, which copied the Scala `Seq` into a Java `ArrayList`. +The focused profile measured about 184 allocated bytes for every eight-part `operate` call. `operate` now captures the +published sequence itself and walks the normal immutable Scala `List` through its existing head/tail chain. Other +`Seq` implementations accepted through `partList_$eq` retain an iterator fallback. + +This restores the reference's captured-sequence behavior: a callback-added part is not visited, and an original part +detached before its turn is skipped by the existing null-tile guard. The public member descriptors and virtual call +path are unchanged. The matching profile measured 0 B/call for direct `operate`, 0.05 B/call through `updateEntity`, +and roughly 4.3x higher throughput. Three new plain-JVM tests cover addition, detachment, and the non-list fallback; +the complete 133-test plain-JVM and 28-test Forge server suites pass. ### Preserved Scala-typed descriptors diff --git a/JAVA_MIGRATION_HANDOFF.md b/JAVA_MIGRATION_HANDOFF.md index 6a4648d..932cc01 100644 --- a/JAVA_MIGRATION_HANDOFF.md +++ b/JAVA_MIGRATION_HANDOFF.md @@ -10,9 +10,9 @@ what breaks, and what is left. The other documents hold the reasoning. | `JAVA_MIGRATION_CONSUMER_AUDIT.md` | How every consumer uses FMP at runtime: data, lifecycle, reflection, and generated tiles | | `JAVA_MIGRATION_DIVERGENCES.md` | Every intentional difference from the reference, one entry per port | | `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 pre-optimization baseline, findings, and exact rerun command | +| `JAVA_MIGRATION_PROFILE.md` | The focused baseline, first measured result, findings, and exact rerun command | -Branch: `algent/java`. Base: `master`. 62 commits including the profile-baseline commit. +Branch: `algent/java`. Base: `master`. 64 commits including the first profiled optimization. ## The one rule that matters @@ -220,14 +220,15 @@ The immediate compatibility gate is complete. `ForgeEnvironmentSmokeTest` now fr trait generation, Java-trait rewriting/dispatch, generated-class reuse, and a server-only pass-through interface's overloads, single-implementor rule, copy/rebind behavior, and removal cleanup. -The focused CPU/allocation baseline is complete. It measures about 184 allocated bytes per eight-part -`updateEntity`/`operate` call and 80.5 bytes per generated redstone three-query iteration. JFR attributes the first pair -to `TileMultipart.parts()` collection conversion/copying and the redstone path to Scala iteration, `IntRef`, and -generated closures. Full results and rerun commands are in `JAVA_MIGRATION_PROFILE.md`. +The first profiled optimization is complete. Focused tests freeze `operate` behavior when callbacks add or detach +parts, and its normal immutable-`List` path now traverses the captured list directly without changing the public Scala +`Seq`/`Function1` ABI. The same profile fell from about 184 B/call to 0.05 B/call for `updateEntity` and 0 B/call for +`operate`, with roughly 4.3x higher throughput. Full results and rerun commands are in +`JAVA_MIGRATION_PROFILE.md`. -Next freeze `operate` behavior when callbacks add or remove parts, then remove its measured per-call collection -materialization without changing the Scala `Seq`/`Function1` ABI. Re-run the same profile before starting the complete -`IRedstonePart.scala` unit. +Next characterize and port the complete `IRedstonePart.scala` unit. Re-baseline that phase immediately before the +implementation comparison, then remove its measured Scala iteration, `IntRef`, and generated-closure costs without +splitting `RedstoneInteractions$`. `IRedstonePart.scala` is misleadingly named and is **not** a marker-trait file. It holds six traits plus `RedstoneInteractions`, whose `MODULE$` is load-bearing. After the compatibility gate and an initial profile, port the @@ -255,8 +256,8 @@ all of `multipart/asm/`. The ASM subsystem should be last; freeze generated-clas the packaged-client/manual compatibility run. - Microblock-specific NBT and packet payloads still need characterization immediately before that subsystem changes; the compact core tile/part fixture is complete. -- `TileMultipart`'s `partList` still republishes an immutable Scala `Seq` on every mutation. The Java port's `parts()` - additionally materializes an `ArrayList` and backing array per traversal; the profile confirms it as the first Phase - 4 target. Preserve the reference's captured-immutable-sequence mutation behavior while removing that port artifact. +- `TileMultipart`'s `partList` still republishes an immutable Scala `Seq` on every mutation. `operate` no longer calls + the Java port's copying `parts()` helper, but less frequent read and mutation paths still do. Optimize those only if + a representative profile identifies them; their mutable snapshots are intentional at mutation sites. - Phase 1 never properly started. GTNHLib is currently commented out in `dependencies.gradle`; it had been added at `api` scope to satisfy a test compile rather than because a migration change needed it. diff --git a/JAVA_MIGRATION_PROFILE.md b/JAVA_MIGRATION_PROFILE.md index ba147af..69454cf 100644 --- a/JAVA_MIGRATION_PROFILE.md +++ b/JAVA_MIGRATION_PROFILE.md @@ -1,4 +1,4 @@ -# Java migration — focused performance baseline +# Java migration — focused performance baseline and first result This is the reproducible pre-optimization baseline for Phase 4. It runs inside the real deobfuscated Forge dedicated server, using the same generated tiles as the functional suite. It is intentionally focused rather than a claim about @@ -73,10 +73,34 @@ is the more stable regression metric. ## Decision -The first Phase 4 implementation should target `TileMultipart` traversal, but only after one focused test freezes its -mutation semantics: iteration observes the captured part order, skips a part whose tile was cleared before its turn, -and does not visit a part added during the callback. Then remove the per-call `ArrayList`/array/wrapper materialization -while retaining the public Scala `Seq` and `operate(Function1)` ABI, and re-run this exact profile. +The first Phase 4 implementation targeted `TileMultipart` traversal after focused tests froze its mutation semantics: +iteration observes the captured part order, skips a part whose tile was cleared before its turn, and does not visit a +part added during the callback. The implementation retains the public Scala `Seq` and `operate(Function1)` ABI. -After that comparison, characterize and port the complete `IRedstonePart.scala` unit and eliminate the measured -redstone `IntRef`/closure paths without splitting its load-bearing `RedstoneInteractions$` singleton. +## Traversal result captured 2026-08-27 + +`operate` now captures `partList` directly. The normal immutable Scala `List` path walks its existing head/tail chain, +which creates no iterator, Java wrapper, array, or copied collection. The published setter still accepts any Scala +`Seq`; unusual implementations use the reference-style iterator fallback rather than being forced into the fast-path +representation. + +The retained post-change report is from the same machine, JVM, arguments, warm-up, and 50,000,000-iteration workload: + +| Phase | Baseline elapsed | Result elapsed | Baseline B/op | Result B/op | Throughput change | +| --- | ---: | ---: | ---: | ---: | ---: | +| `updateEntity` | 2.970 s | 0.682 s | 184.0 | 0.05 | 4.36x | +| `operate` | 3.004 s | 0.700 s | 183.9 | 0.0 | 4.29x | +| `redstoneQueries` | 3.689 s | 7.286 s | 80.5 | 80.4 | control only | + +A repeat produced 0.732 s / 0.687 s for `updateEntity` / `operate`, with the same 0.05 / 0.0 B/op. The result therefore +removes effectively all measured traversal allocation and raises throughput from roughly 16.7 million to 68–73 +million calls/s. The post-change JFR hot-method view no longer contains `TileMultipart.parts()`, +`AbstractCollection.toArray`, or the Java-conversion wrappers in these paths. + +The unchanged redstone allocation is the useful control. Its timing was consistently slower in both post-change runs, +but its code did not change and it now starts several seconds earlier because the preceding phases finish faster; do +not attribute that timing difference to this traversal change. Re-baseline the redstone unit immediately before its +own implementation comparison. + +Next characterize and port the complete `IRedstonePart.scala` unit and eliminate the measured redstone +`IntRef`/closure paths without splitting its load-bearing `RedstoneInteractions$` singleton. diff --git a/src/main/scala/codechicken/multipart/TileMultipart.java b/src/main/scala/codechicken/multipart/TileMultipart.java index 03d3016..3b513f0 100644 --- a/src/main/scala/codechicken/multipart/TileMultipart.java +++ b/src/main/scala/codechicken/multipart/TileMultipart.java @@ -87,11 +87,28 @@ public class TileMultipart extends TileEntity implements IChunkLoadTile { return doesTick; } + @SuppressWarnings("unchecked") public void operate(Function1 f) { - for (TMultiPart p : parts()) { - if (p.tile() != null) { - f.apply(p); + Seq current = partList; + if (!(current instanceof scala.collection.immutable.List)) { + scala.collection.Iterator iterator = current.iterator(); + while (iterator.hasNext()) { + applyIfBound(f, iterator.next()); } + return; + } + + scala.collection.immutable.List list = (scala.collection.immutable.List) current; + while (!list.isEmpty()) { + TMultiPart p = list.head(); + list = (scala.collection.immutable.List) list.tail(); + applyIfBound(f, p); + } + } + + private static void applyIfBound(Function1 f, TMultiPart p) { + if (p.tile() != null) { + f.apply(p); } }