Closed adakitesystems closed 6 years ago
This is what I meant:
// private variable:
private Map<Player, List<PlayerUnit>> testMap;
// populate inside a method executed before onFrame():
this.testMap = this.units.values().stream()
.filter(u -> u instanceof PlayerUnit)
.map(PlayerUnit.class::cast)
.collect(Collectors.groupingBy(PlayerUnit::getPlayer));
Or if we wanted to keep them stored as Units:
// private variable:
private Map<Player, List<Unit>> testMap;
// populate inside a method executed before onFrame():
this.testMap = allUnits().stream()
.filter(u -> u instanceof PlayerUnit)
.collect(Collectors.groupingBy(u -> ((PlayerUnit)u).getPlayer()));
Supposedly, this allows us to rewrite getUnits(Player)
as:
public List<PlayerUnit> getUnits(Player) {
return this.testMap.get(player);
}
(please note this is untested and should be checked for mutability and possible NPEs as well)
The mere fact of doing this and populating the map before onFrame()
should probably be enough to speed up multiple calls of getUnits(Player)
.
Closed with Pull Request https://github.com/OpenBW/BWAPI4J/pull/46
Inspired by comments made by @MasterFocus and @dgant on Discord, I would like to discuss caching all possible "computed" objects/lists such as BW#getUnits(Player).
Example idea from @dgant's PurpleWave bot repo: https://github.com/dgant/PurpleWave/blob/master/src/Performance/Cache.scala
Possible Java solution (untested):
In the specific case of
BW#getUnits(Player)
, @MasterFocus suggested the following:@dgant suggested to remove
int currentFrameCount
from the parameter list. To do this, we may need aFrameCount
class that could be passed around? Something whereFrameCount
is an interface instantiated as the subclassFrameCountSetter
used by whichever class does the updating (e.g. BW.java) and store theFrameCount
type in all relevantCache
objects?