Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Item List Sort: Ensure access to shared private field is atomic #584

Merged
merged 2 commits into from
Jan 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions src/main/java/codechicken/nei/ItemList.java
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
@@ -44,7 +45,7 @@ public class ItemList {

private static final HashSet<Item> erroredItems = new HashSet<>();
private static final HashSet<String> stackTraces = new HashSet<>();
private static final HashMap<ItemStack, Integer> ordering = new HashMap<>();
private static HashMap<ItemStack, Integer> ordering = new HashMap<>();
/**
* Unlike {@link LayoutManager#itemsLoaded}, this indicates whether item loading is actually finished or not.
*/
@@ -234,10 +235,10 @@ private String getTooltip(ItemStack stack) {
}

private void updateOrdering(List<ItemStack> items) {
ItemList.ordering.clear();

ItemSorter.sort(items);

HashMap<ItemStack, Integer> newOrdering = new HashMap<>();

if (!CollapsibleItems.isEmpty()) {
HashMap<Integer, Integer> groups = new HashMap<>();
int orderIndex = 0;
@@ -246,23 +247,25 @@ private void updateOrdering(List<ItemStack> items) {
final int groupIndex = CollapsibleItems.getGroupIndex(stack);

if (groupIndex == -1) {
ItemList.ordering.put(stack, orderIndex++);
newOrdering.put(stack, orderIndex++);
} else {

if (!groups.containsKey(groupIndex)) {
groups.put(groupIndex, orderIndex++);
}

ItemList.ordering.put(stack, groups.get(groupIndex));
newOrdering.put(stack, groups.get(groupIndex));
}
}
} else {
int orderIndex = 0;

for (ItemStack stack : items) {
ItemList.ordering.put(stack, orderIndex++);
newOrdering.put(stack, orderIndex++);
}
}

ItemList.ordering = newOrdering;
}

@Override
@@ -346,12 +349,9 @@ public ForkJoinWorkerThread newThread(ForkJoinPool pool) {

public static final RestartableTask updateFilter = new RestartableTask("NEI Item Filtering") {

private int compare(ItemStack o1, ItemStack o2) {
return Integer.compare(ItemList.ordering.get(o1), ItemList.ordering.get(o2));
}

@Override
public void execute() {

if (!loadFinished) return;
ItemFilter filter = getItemListFilter();
ArrayList<ItemStack> filtered;
@@ -368,8 +368,12 @@ public void execute() {
}

if (interrupted()) return;
filtered.sort(this::compare);

Comparator<ItemStack> comparator = Comparator.comparingInt(ItemList.ordering::get);
filtered.sort(comparator::compare);

if (interrupted()) return;

ItemPanel.updateItemList(filtered);
}
};