Skip to content

Commit

Permalink
fix: reuse grid resources when rebuilding view list
Browse files Browse the repository at this point in the history
This fixes performance issues with gathering
item tooltips.
  • Loading branch information
raoulvdberge committed Aug 18, 2024
1 parent 39e0b11 commit ab15150
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
### Changed

- Optimized memory usage and startup time of cable models. After updating, cables will appear disconnected, but this is only visual. Cause a block update to fix this.
- Optimized performance of searching in the Grid.

### Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public class GridViewImpl implements GridView {
private final GridResourceFactory resourceFactory;
private final Map<ResourceKey, TrackedResource> trackedResources = new HashMap<>();

private List<GridResource> viewList = new ArrayList<>();
private final Map<ResourceKey, GridResource> viewListIndex = new HashMap<>();

private ViewList viewList = new ViewList(new ArrayList<>(), new HashMap<>());
private GridSortingType sortingType;
private GridSortingDirection sortingDirection = GridSortingDirection.ASCENDING;
private BiPredicate<GridView, GridResource> filter = (view, resource) -> true;
Expand Down Expand Up @@ -100,22 +98,35 @@ public long getAmount(final ResourceKey resource) {
@Override
public void sort() {
LOGGER.info("Sorting grid view");
viewList = createViewList();
notifyListener();
}

viewListIndex.clear();

final List<GridResource> newViewList = new ArrayList<>();
private ViewList createViewList() {
final List<GridResource> list = new ArrayList<>();
final Map<ResourceKey, GridResource> index = new HashMap<>();
for (final ResourceKey resource : backingList.getAll()) {
resourceFactory.apply(resource).ifPresent(gridResource -> {
if (filter.test(this, gridResource)) {
newViewList.add(gridResource);
viewListIndex.put(resource, gridResource);
}
});
final GridResource existingGridResource = viewList.index.get(resource);
if (existingGridResource != null) {
tryAddGridResourceIntoViewList(existingGridResource, list, index, resource);
} else {
resourceFactory.apply(resource).ifPresent(
gridResource -> tryAddGridResourceIntoViewList(gridResource, list, index, resource)
);
}
}
newViewList.sort(getComparator());
viewList = newViewList;
list.sort(getComparator());
return new ViewList(list, index);
}

notifyListener();
private void tryAddGridResourceIntoViewList(final GridResource gridResource,
final List<GridResource> list,
final Map<ResourceKey, GridResource> index,
final ResourceKey resource) {
if (filter.test(this, gridResource)) {
list.add(gridResource);
index.put(resource, gridResource);
}
}

@Override
Expand All @@ -126,7 +137,7 @@ public void onChange(final ResourceKey resource,

updateOrRemoveTrackedResource(resource, trackedResource);

final GridResource gridResource = viewListIndex.get(resource);
final GridResource gridResource = viewList.index.get(resource);
if (gridResource != null) {
LOGGER.debug("{} was already found in the view list", resource);
if (gridResource.isZeroed()) {
Expand Down Expand Up @@ -162,12 +173,12 @@ private void reinsertZeroedResourceIntoViewList(final ResourceKey resource,
final GridResource oldGridResource) {
LOGGER.debug("{} was zeroed, unzeroing", resource);
final GridResource newResource = resourceFactory.apply(operationResult.resource()).orElseThrow();
viewListIndex.put(resource, newResource);
viewList.index.put(resource, newResource);
final int index = CoreValidations.validateNotNegative(
viewList.indexOf(oldGridResource),
viewList.list.indexOf(oldGridResource),
"Cannot reinsert previously zeroed resource, it was not found"
);
viewList.set(index, newResource);
viewList.list.set(index, newResource);
}

private void handleChangeForExistingResource(final ResourceKey resource,
Expand All @@ -189,9 +200,9 @@ private void handleChangeForExistingResource(final ResourceKey resource,
private void updateExistingResourceInViewList(final ResourceKey resource,
final GridResource gridResource,
final boolean noLongerAvailable) {
viewList.remove(gridResource);
viewList.list.remove(gridResource);
if (noLongerAvailable) {
viewListIndex.remove(resource);
viewList.index.remove(resource);
notifyListener();
} else {
addIntoView(gridResource);
Expand All @@ -204,19 +215,19 @@ private void handleChangeForNewResource(final ResourceKey resource,
final GridResource gridResource = resourceFactory.apply(operationResult.resource()).orElseThrow();
if (filter.test(this, gridResource)) {
LOGGER.debug("Filter allowed, actually adding {}", resource);
viewListIndex.put(resource, gridResource);
viewList.index.put(resource, gridResource);
addIntoView(gridResource);
notifyListener();
}
}

private void addIntoView(final GridResource resource) {
// Calculate the position according to sorting rules.
final int wouldBePosition = Collections.binarySearch(viewList, resource, getComparator());
final int wouldBePosition = Collections.binarySearch(viewList.list, resource, getComparator());
// Most of the time, the "would be" position is negative, indicating that the resource wasn't found yet in the
// list, comparing with sorting rules. The absolute of this position would be the "real" position if sorted.
if (wouldBePosition < 0) {
viewList.add(-wouldBePosition - 1, resource);
viewList.list.add(-wouldBePosition - 1, resource);
} else {
// If the "would-be" position is positive, this means that the resource is already contained in the list,
// comparing with sorting rules.
Expand All @@ -225,7 +236,7 @@ private void addIntoView(final GridResource resource) {
// For example: a resource with different identity but the same name (in Minecraft: an enchanted book
// with different NBT).
// In that case, just insert it after the "existing" resource.
viewList.add(wouldBePosition + 1, resource);
viewList.list.add(wouldBePosition + 1, resource);
}
}

Expand All @@ -247,7 +258,7 @@ private Comparator<GridResource> getComparator() {

@Override
public List<GridResource> getViewList() {
return viewList;
return viewList.list;
}

@Override
Expand All @@ -258,8 +269,11 @@ public ResourceList copyBackingList() {
@Override
public void clear() {
backingList.clear();
viewListIndex.clear();
viewList.index.clear();
trackedResources.clear();
viewList.clear();
viewList.list.clear();
}

private record ViewList(List<GridResource> list, Map<ResourceKey, GridResource> index) {
}
}

0 comments on commit ab15150

Please sign in to comment.