Reduce the amount of temporary objects in items
- Reduce the amount of GroundItem objects created by mutating objects instead of creating merged copies - Reduce the amount of stream transformations required by using LinkedHashMap and sorting the ground items before collection Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
This commit is contained in:
@@ -33,7 +33,6 @@ import net.runelite.api.coords.WorldPoint;
|
|||||||
@Builder
|
@Builder
|
||||||
class GroundItem
|
class GroundItem
|
||||||
{
|
{
|
||||||
private int index;
|
|
||||||
private int id;
|
private int id;
|
||||||
private int itemId;
|
private int itemId;
|
||||||
private String name;
|
private String name;
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ public class GroundItemsOverlay extends Overlay
|
|||||||
offsetMap.clear();
|
offsetMap.clear();
|
||||||
final LocalPoint localLocation = player.getLocalLocation();
|
final LocalPoint localLocation = player.getLocalLocation();
|
||||||
|
|
||||||
plugin.getCollectedGroundItems().forEach(item ->
|
plugin.getCollectedGroundItems().values().forEach(item ->
|
||||||
{
|
{
|
||||||
final LocalPoint groundPoint = LocalPoint.fromWorld(client, item.getLocation());
|
final LocalPoint groundPoint = LocalPoint.fromWorld(client, item.getLocation());
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ import com.google.common.base.Joiner;
|
|||||||
import com.google.common.base.Splitter;
|
import com.google.common.base.Splitter;
|
||||||
import com.google.common.cache.CacheBuilder;
|
import com.google.common.cache.CacheBuilder;
|
||||||
import com.google.common.cache.LoadingCache;
|
import com.google.common.cache.LoadingCache;
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
import com.google.common.eventbus.Subscribe;
|
import com.google.common.eventbus.Subscribe;
|
||||||
import com.google.inject.Provides;
|
import com.google.inject.Provides;
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
@@ -35,6 +36,7 @@ import java.awt.Rectangle;
|
|||||||
import static java.lang.Boolean.TRUE;
|
import static java.lang.Boolean.TRUE;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
@@ -89,20 +91,6 @@ public class GroundItemsPlugin extends Plugin
|
|||||||
private static final float HIGH_ALCHEMY_CONSTANT = 0.6f;
|
private static final float HIGH_ALCHEMY_CONSTANT = 0.6f;
|
||||||
// ItemID for coins
|
// ItemID for coins
|
||||||
private static final int COINS = ItemID.COINS_995;
|
private static final int COINS = ItemID.COINS_995;
|
||||||
// Collects similar ground items
|
|
||||||
private static final Collector<GroundItem, ?, Map<GroundItem.GroundItemKey, GroundItem>> GROUND_ITEM_MAP_COLLECTOR = Collectors
|
|
||||||
.toMap
|
|
||||||
((item) -> new GroundItem.GroundItemKey(item.getItemId(), item.getLocation()), Function.identity(), ((a, b) ->
|
|
||||||
GroundItem.builder()
|
|
||||||
.index(b.getIndex())
|
|
||||||
.id(b.getId())
|
|
||||||
.itemId(b.getItemId())
|
|
||||||
.name(b.getName())
|
|
||||||
.location(b.getLocation())
|
|
||||||
.haPrice(a.getHaPrice() + b.getHaPrice())
|
|
||||||
.gePrice(a.getGePrice() + b.getGePrice())
|
|
||||||
.quantity(a.getQuantity() + b.getQuantity())
|
|
||||||
.build()));
|
|
||||||
|
|
||||||
@Getter(AccessLevel.PACKAGE)
|
@Getter(AccessLevel.PACKAGE)
|
||||||
private final Map<Rectangle, String> hiddenBoxes = new HashMap<>();
|
private final Map<Rectangle, String> hiddenBoxes = new HashMap<>();
|
||||||
@@ -139,11 +127,23 @@ public class GroundItemsPlugin extends Plugin
|
|||||||
private GroundItemsOverlay overlay;
|
private GroundItemsOverlay overlay;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private final List<GroundItem> collectedGroundItems = new ArrayList<>();
|
private final Map<GroundItem.GroundItemKey, GroundItem> collectedGroundItems = new LinkedHashMap<>();
|
||||||
private final List<GroundItem> groundItems = new ArrayList<>();
|
private final List<GroundItem> groundItems = new ArrayList<>();
|
||||||
private LoadingCache<String, Boolean> highlightedItems;
|
private LoadingCache<String, Boolean> highlightedItems;
|
||||||
private LoadingCache<String, Boolean> hiddenItems;
|
private LoadingCache<String, Boolean> hiddenItems;
|
||||||
|
|
||||||
|
// Collects similar ground items
|
||||||
|
private final Collector<GroundItem, ?, Map<GroundItem.GroundItemKey, GroundItem>> groundItemMapCollector = Collectors
|
||||||
|
.toMap
|
||||||
|
((item) -> new GroundItem.GroundItemKey(item.getItemId(), item.getLocation()), Function.identity(), (a, b) ->
|
||||||
|
{
|
||||||
|
b.setHaPrice(a.getHaPrice() + b.getHaPrice());
|
||||||
|
b.setGePrice(a.getGePrice() + b.getGePrice());
|
||||||
|
b.setQuantity(a.getQuantity() + b.getQuantity());
|
||||||
|
return b;
|
||||||
|
},
|
||||||
|
() -> collectedGroundItems);
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
GroundItemsConfig provideConfig(ConfigManager configManager)
|
GroundItemsConfig provideConfig(ConfigManager configManager)
|
||||||
{
|
{
|
||||||
@@ -220,7 +220,6 @@ public class GroundItemsPlugin extends Plugin
|
|||||||
final int upperY = Math.min(from.getRegionY() + MAX_RANGE, REGION_SIZE - 1);
|
final int upperY = Math.min(from.getRegionY() + MAX_RANGE, REGION_SIZE - 1);
|
||||||
|
|
||||||
groundItems.clear();
|
groundItems.clear();
|
||||||
int index = 0;
|
|
||||||
|
|
||||||
for (int x = lowerX; x <= upperX; ++x)
|
for (int x = lowerX; x <= upperX; ++x)
|
||||||
{
|
{
|
||||||
@@ -254,10 +253,6 @@ public class GroundItemsPlugin extends Plugin
|
|||||||
if (groundItem != null)
|
if (groundItem != null)
|
||||||
{
|
{
|
||||||
groundItems.add(groundItem);
|
groundItems.add(groundItem);
|
||||||
|
|
||||||
// Required for preserving the correct order later
|
|
||||||
groundItem.setIndex(index);
|
|
||||||
index++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -265,12 +260,7 @@ public class GroundItemsPlugin extends Plugin
|
|||||||
|
|
||||||
// Group ground items together and sort them properly
|
// Group ground items together and sort them properly
|
||||||
collectedGroundItems.clear();
|
collectedGroundItems.clear();
|
||||||
groundItems.stream()
|
Lists.reverse(groundItems).stream().collect(groundItemMapCollector);
|
||||||
.collect(GROUND_ITEM_MAP_COLLECTOR)
|
|
||||||
.values()
|
|
||||||
.stream()
|
|
||||||
.sorted((left, right) -> Integer.compare(right.getIndex(), left.getIndex()))
|
|
||||||
.collect(Collectors.toCollection(() -> collectedGroundItems));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
|||||||
Reference in New Issue
Block a user