Avoid allocations on Map::putIfAbsent usage

Map::computeIfAbsent only runs the passed constructors when it needs to
This commit is contained in:
AnimeGitB 2022-08-19 12:44:55 +09:30
parent 0cb75aeb5f
commit 7d523efd41
2 changed files with 4 additions and 3 deletions

View File

@ -106,8 +106,9 @@ public class TowerManager extends BasePlayerManager {
if (!hasNextLevel()) {
// set up the next floor
recordMap.putIfAbsent(getNextFloorId(), new TowerLevelRecord(getNextFloorId()));
player.getSession().send(new PacketTowerCurLevelRecordChangeNotify(getNextFloorId(), 1));
var nextFloorId = this.getNextFloorId();
recordMap.computeIfAbsent(nextFloorId, TowerLevelRecord::new);
player.getSession().send(new PacketTowerCurLevelRecordChangeNotify(nextFloorId, 1));
}else {
player.getSession().send(new PacketTowerCurLevelRecordChangeNotify(currentFloorId, getCurrentLevel()));
}

View File

@ -42,7 +42,7 @@ public class WorldDataSystem extends BaseGameSystem {
DataLoader.loadList("ChestReward.json", ChestReward.class)
.forEach(reward ->
reward.getObjNames().forEach(name ->
chestInteractHandlerMap.putIfAbsent(name, new NormalChestInteractHandler(reward))));
chestInteractHandlerMap.computeIfAbsent(name, x -> new NormalChestInteractHandler(reward))));
} catch (Exception e) {
Grasscutter.getLogger().error("Unable to load chest reward config.", e);
}