Skip to content

Commit 14c53c2

Browse files
committed
Switching to synchronizing around the itemsReceived list over copyonwrite, cause we probably write more than read
1 parent 50ee476 commit 14c53c2

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/main/java/dev/koifysh/archipelago/ItemManager.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class ItemManager {
1818
private final Client client;
1919
private WebSocket webSocket;
2020

21-
private List<NetworkItem> receivedItems = new CopyOnWriteArrayList<>();
21+
private List<NetworkItem> receivedItems = new ArrayList<>();
2222

2323
private final AtomicInteger index = new AtomicInteger();
2424

@@ -28,10 +28,12 @@ public ItemManager(Client client) {
2828

2929
public void receiveItems(List<NetworkItem> ids, int index) {
3030
if (index == 0) {
31-
receivedItems = new CopyOnWriteArrayList<>();
31+
receivedItems = new ArrayList<>();
3232
}
3333
if (receivedItems.size() == index) {
34-
receivedItems.addAll(ids);
34+
synchronized (this) {
35+
receivedItems.addAll(ids);
36+
}
3537
DataPackage dp = client.getDataPackage();
3638
int myTeam = client.getTeam();
3739
for (int i = this.index.get(); i < receivedItems.size(); i++) {
@@ -53,7 +55,7 @@ public void receiveItems(List<NetworkItem> ids, int index) {
5355
}
5456

5557
public void writeFromSave(List<NetworkItem> receivedItems, int index) {
56-
this.receivedItems = new CopyOnWriteArrayList<>(receivedItems);
58+
this.receivedItems = new ArrayList<>(receivedItems);
5759
this.index.set(index);
5860
}
5961

@@ -66,13 +68,17 @@ public int getIndex() {
6668
}
6769

6870
public List<NetworkItem> getReceivedItems() {
69-
return receivedItems;
71+
synchronized (this) {
72+
return new ArrayList<>(receivedItems);
73+
}
7074
}
7175

7276
public List<Long> getReceivedItemIDs() {
7377
List<Long> ids = new ArrayList<>();
74-
for (NetworkItem receivedItem : receivedItems) {
75-
ids.add(receivedItem.itemID);
78+
synchronized (this) {
79+
for (NetworkItem receivedItem : receivedItems) {
80+
ids.add(receivedItem.itemID);
81+
}
7682
}
7783
return ids;
7884
}

0 commit comments

Comments
 (0)