Skip to content

Commit 000679b

Browse files
authored
perf(render): precompute only seated viewer memberships (#79)
Avoid quadratic viewer-membership precomputation for unrelated online spectators while preserving the existing snapshot values. Validated by the protected performance-snapshot A/B gate: PASS_OPTIMIZED.
1 parent bcccf93 commit 000679b

1 file changed

Lines changed: 29 additions & 10 deletions

File tree

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

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,40 @@ public TableRenderSnapshot create(TableRenderSubject session, long version, long
3030
.map(SerializedViewerId::id)
3131
.toList();
3232
Set<UUID> onlineViewerIdSet = new HashSet<>(onlineViewerIds);
33+
SeatWind[] seatWinds = SeatWind.values();
34+
EnumMap<SeatWind, UUID> seatPlayerIds = new EnumMap<>(SeatWind.class);
35+
for (SeatWind wind : seatWinds) {
36+
seatPlayerIds.put(wind, session.playerAt(wind));
37+
}
3338
Map<UUID, String> viewerMembershipSignatures = new HashMap<>();
3439
Map<UUID, List<UUID>> viewerIdsExcluding = new HashMap<>();
35-
viewerMembershipSignatures.put(null, this.viewerMembershipSignature(serializedOnlineViewerIds, null));
36-
viewerIdsExcluding.put(null, List.copyOf(onlineViewerIds));
37-
for (SerializedViewerId viewer : serializedOnlineViewerIds) {
38-
UUID viewerId = viewer.id();
40+
for (UUID playerId : seatPlayerIds.values()) {
41+
if (viewerMembershipSignatures.containsKey(playerId)
42+
|| playerId != null && !onlineViewerIdSet.contains(playerId)) {
43+
continue;
44+
}
3945
viewerMembershipSignatures.put(
40-
viewerId,
41-
this.viewerMembershipSignature(serializedOnlineViewerIds, viewerId)
46+
playerId,
47+
this.viewerMembershipSignature(serializedOnlineViewerIds, playerId)
48+
);
49+
viewerIdsExcluding.put(
50+
playerId,
51+
playerId == null ? List.copyOf(onlineViewerIds) : this.viewerIdsExcluding(onlineViewerIds, playerId)
4252
);
43-
viewerIdsExcluding.put(viewerId, this.viewerIdsExcluding(onlineViewerIds, viewerId));
4453
}
4554
EnumMap<SeatWind, TableSeatRenderSnapshot> seats = new EnumMap<>(SeatWind.class);
46-
for (SeatWind wind : SeatWind.values()) {
47-
seats.put(wind, this.captureSeatSnapshot(session, wind, onlineViewerIdSet, viewerMembershipSignatures, viewerIdsExcluding));
55+
for (SeatWind wind : seatWinds) {
56+
seats.put(
57+
wind,
58+
this.captureSeatSnapshot(
59+
session,
60+
wind,
61+
seatPlayerIds.get(wind),
62+
onlineViewerIdSet,
63+
viewerMembershipSignatures,
64+
viewerIdsExcluding
65+
)
66+
);
4867
}
4968
return new TableRenderSnapshot(
5069
version,
@@ -111,11 +130,11 @@ public TableSeatRenderSnapshot createPrivateHandSeat(TableRenderSubject session,
111130
private TableSeatRenderSnapshot captureSeatSnapshot(
112131
TableRenderSubject session,
113132
SeatWind wind,
133+
UUID playerId,
114134
Set<UUID> onlineViewerIdSet,
115135
Map<UUID, String> viewerMembershipSignatures,
116136
Map<UUID, List<UUID>> viewerIdsExcluding
117137
) {
118-
UUID playerId = session.playerAt(wind);
119138
boolean occupied = playerId != null;
120139
return new TableSeatRenderSnapshot(
121140
wind,

0 commit comments

Comments
 (0)