Skip to content

Commit cba64e8

Browse files
authored
perf(render): avoid geometry allocations on proxy reuse (#80)
Compare cached ray-proxy geometry directly and allocate a snapshot only when client proxies need rebuilding. Validated twice by the protected performance-ray-proxy A/B gate, including against the post-#79 dev baseline: PASS_OPTIMIZED.
1 parent 000679b commit cba64e8

1 file changed

Lines changed: 31 additions & 4 deletions

File tree

src/main/java/top/ellan/mahjong/table/render/SparrowRayInteractionProxyCoordinator.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.LinkedHashMap;
55
import java.util.List;
66
import java.util.Map;
7+
import java.util.Objects;
78
import java.util.Set;
89
import java.util.UUID;
910
import java.util.concurrent.atomic.AtomicBoolean;
@@ -75,12 +76,12 @@ synchronized void replace(
7576
if (viewer == null || !viewer.isOnline()) {
7677
continue;
7778
}
78-
List<InteractionGeometry> geometry = interactionGeometry(interactions);
7979
ActiveProxies current = previous.get(viewerId);
80-
if (this.canReuse(viewerId, viewer, current, geometry)) {
80+
if (this.canReuse(viewerId, viewer, current, interactions)) {
8181
next.put(viewerId, current);
8282
continue;
8383
}
84+
List<InteractionGeometry> geometry = interactionGeometry(interactions);
8485
List<ClientProxy> proxies = this.backend.create(viewer, interactions);
8586
if (!proxies.isEmpty()) {
8687
ActiveProxies created = new ActiveProxies(
@@ -135,11 +136,11 @@ private boolean canReuse(
135136
UUID viewerId,
136137
Player viewer,
137138
ActiveProxies active,
138-
List<InteractionGeometry> geometry
139+
List<DisplayInteractionRayRegistry.RayInteraction> interactions
139140
) {
140141
if (active == null
141142
|| active.viewer() != viewer
142-
|| !active.geometry().equals(geometry)
143+
|| !sameGeometry(active.geometry(), interactions)
143144
|| active.proxies().isEmpty()) {
144145
return false;
145146
}
@@ -153,6 +154,21 @@ private boolean canReuse(
153154
return true;
154155
}
155156

157+
private static boolean sameGeometry(
158+
List<InteractionGeometry> geometry,
159+
List<DisplayInteractionRayRegistry.RayInteraction> interactions
160+
) {
161+
if (geometry.size() != interactions.size()) {
162+
return false;
163+
}
164+
for (int index = 0; index < geometry.size(); index++) {
165+
if (!geometry.get(index).matches(interactions.get(index))) {
166+
return false;
167+
}
168+
}
169+
return true;
170+
}
171+
156172
private static List<InteractionGeometry> interactionGeometry(
157173
List<DisplayInteractionRayRegistry.RayInteraction> interactions
158174
) {
@@ -355,6 +371,17 @@ private record InteractionGeometry(
355371
float height,
356372
float depth
357373
) {
374+
private boolean matches(DisplayInteractionRayRegistry.RayInteraction interaction) {
375+
return Objects.equals(this.worldId, interaction.worldId())
376+
&& Double.compare(this.centerX, interaction.centerX()) == 0
377+
&& Double.compare(this.centerY, interaction.centerY()) == 0
378+
&& Double.compare(this.centerZ, interaction.centerZ()) == 0
379+
&& Double.compare(this.acrossX, interaction.acrossX()) == 0
380+
&& Double.compare(this.acrossZ, interaction.acrossZ()) == 0
381+
&& Float.compare(this.width, interaction.width()) == 0
382+
&& Float.compare(this.height, interaction.height()) == 0
383+
&& Float.compare(this.depth, interaction.depth()) == 0;
384+
}
358385
}
359386

360387
private static final class SparrowBackend implements Backend {

0 commit comments

Comments
 (0)