Skip to content

Commit f60e9bf

Browse files
authored
perf(render): avoid sorting production region queues (#67)
Validated independently by paired GitHub JMH A/B run 29478306639 (PASS_OPTIMIZED; time +43.5207%, allocation +3762.7374%, 8/8 wins).
1 parent 149e9c5 commit f60e9bf

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ public final class TableRegionDisplayCoordinator {
3939
private static final int PRIORITY_TURN_STATE = 240;
4040
private static final int PRIORITY_BOARD = 160;
4141
private static final int PRIORITY_BACKGROUND = 80;
42+
private static final int[] APPLY_PRIORITY_ORDER = {
43+
PRIORITY_REACTION_PROMPT,
44+
PRIORITY_HAND,
45+
PRIORITY_TURN_STATE,
46+
PRIORITY_BOARD,
47+
PRIORITY_BACKGROUND
48+
};
4249

4350
private final TableSessionContext session;
4451
private final TableRegionFingerprintService fingerprintService;
@@ -275,6 +282,43 @@ public boolean hasStaleDisplayRegions() {
275282
}
276283

277284
private QueueExecution applyQueue(List<QueuedRegionUpdate> updates) {
285+
if (!this.hasProductionQueueOrder(updates)) {
286+
return this.applySortedQueue(updates);
287+
}
288+
int processed = 0;
289+
for (int priority : APPLY_PRIORITY_ORDER) {
290+
for (QueuedRegionUpdate update : updates) {
291+
if (update.priority() != priority) {
292+
continue;
293+
}
294+
if (!update.action().apply()) {
295+
return new QueueExecution(true, processed);
296+
}
297+
processed++;
298+
}
299+
}
300+
return new QueueExecution(false, processed);
301+
}
302+
303+
private boolean hasProductionQueueOrder(List<QueuedRegionUpdate> updates) {
304+
long previousSequence = Long.MIN_VALUE;
305+
for (QueuedRegionUpdate update : updates) {
306+
if (update.sequence() < previousSequence || !this.isProductionPriority(update.priority())) {
307+
return false;
308+
}
309+
previousSequence = update.sequence();
310+
}
311+
return true;
312+
}
313+
314+
private boolean isProductionPriority(int priority) {
315+
return switch (priority) {
316+
case PRIORITY_REACTION_PROMPT, PRIORITY_HAND, PRIORITY_TURN_STATE, PRIORITY_BOARD, PRIORITY_BACKGROUND -> true;
317+
default -> false;
318+
};
319+
}
320+
321+
private QueueExecution applySortedQueue(List<QueuedRegionUpdate> updates) {
278322
updates.sort(
279323
Comparator.comparingInt(QueuedRegionUpdate::priority).reversed()
280324
.thenComparingLong(QueuedRegionUpdate::sequence)

0 commit comments

Comments
 (0)