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:
Tomas Slusny
2018-03-26 16:13:18 +02:00
committed by Adam
parent 5272e2d9b6
commit 9dfd671121
3 changed files with 17 additions and 28 deletions

View File

@@ -33,7 +33,6 @@ import net.runelite.api.coords.WorldPoint;
@Builder
class GroundItem
{
private int index;
private int id;
private int itemId;
private String name;

View File

@@ -99,7 +99,7 @@ public class GroundItemsOverlay extends Overlay
offsetMap.clear();
final LocalPoint localLocation = player.getLocalLocation();
plugin.getCollectedGroundItems().forEach(item ->
plugin.getCollectedGroundItems().values().forEach(item ->
{
final LocalPoint groundPoint = LocalPoint.fromWorld(client, item.getLocation());

View File

@@ -28,6 +28,7 @@ import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.Lists;
import com.google.common.eventbus.Subscribe;
import com.google.inject.Provides;
import java.awt.Color;
@@ -35,6 +36,7 @@ import java.awt.Rectangle;
import static java.lang.Boolean.TRUE;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@@ -89,20 +91,6 @@ public class GroundItemsPlugin extends Plugin
private static final float HIGH_ALCHEMY_CONSTANT = 0.6f;
// ItemID for coins
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)
private final Map<Rectangle, String> hiddenBoxes = new HashMap<>();
@@ -139,11 +127,23 @@ public class GroundItemsPlugin extends Plugin
private GroundItemsOverlay overlay;
@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 LoadingCache<String, Boolean> highlightedItems;
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
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);
groundItems.clear();
int index = 0;
for (int x = lowerX; x <= upperX; ++x)
{
@@ -254,10 +253,6 @@ public class GroundItemsPlugin extends Plugin
if (groundItem != null)
{
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
collectedGroundItems.clear();
groundItems.stream()
.collect(GROUND_ITEM_MAP_COLLECTOR)
.values()
.stream()
.sorted((left, right) -> Integer.compare(right.getIndex(), left.getIndex()))
.collect(Collectors.toCollection(() -> collectedGroundItems));
Lists.reverse(groundItems).stream().collect(groundItemMapCollector);
}
@Nullable