Skip to content

Commit

Permalink
perf(death): avoid creating intermediate list (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
iProdigy authored Apr 14, 2023
1 parent a9ee802 commit 7a0b379
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- Minor: Rescale screenshots to comply with Discord's 8MB size limit. (#200)
- Minor: Include combat level in skill notifications. (#203)
- Dev: Update grade wrapper to v8.1 minor version. (#209)
- Dev: Refactor killer identification in death notifier. (#197)
- Dev: Refactor killer identification in death notifier. (#197, #210)

## 1.3.2

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dinkplugin/notifiers/DeathNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ private Actor identifyKiller() {

// find another player interacting with us (that is preferably not a friend or clan member)
if (pvpEnabled) {
Optional<Player> pker = client.getPlayers().stream()
Optional<Player> pker = Arrays.stream(client.getCachedPlayers())
.filter(interacting)
.min(PK_COMPARATOR.apply(localPlayer)); // O(n)
if (pker.isPresent())
Expand Down
7 changes: 4 additions & 3 deletions src/test/java/dinkplugin/notifiers/DeathNotifierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected void setUp() {

// init client mocks
when(client.getVarbitValue(Varbits.IN_WILDERNESS)).thenReturn(1);
when(client.getPlayers()).thenReturn(Collections.emptyList());
when(client.getCachedPlayers()).thenReturn(new Player[0]);
when(client.getCachedNPCs()).thenReturn(new NPC[0]);
WorldPoint location = new WorldPoint(0, 0, 0);
when(localPlayer.getWorldLocation()).thenReturn(location);
Expand Down Expand Up @@ -151,7 +151,8 @@ void testNotifyPk() {
Player other = mock(Player.class);
when(other.getName()).thenReturn(pker);
when(other.getInteracting()).thenReturn(localPlayer);
when(client.getPlayers()).thenReturn(Arrays.asList(mock(Player.class), mock(Player.class), other, mock(Player.class)));
Player[] candidates = { mock(Player.class), mock(Player.class), other, mock(Player.class) };
when(client.getCachedPlayers()).thenReturn(candidates);

// fire event
plugin.onActorDeath(new ActorDeath(localPlayer));
Expand Down Expand Up @@ -201,7 +202,7 @@ void testNotifyNotPk() {
Player other = mock(Player.class);
when(other.getName()).thenReturn("Rasmus");
when(other.getInteracting()).thenReturn(localPlayer);
when(client.getPlayers()).thenReturn(Collections.singletonList(other));
when(client.getCachedPlayers()).thenReturn(new Player[] { other });
when(client.getVarbitValue(Varbits.IN_WILDERNESS)).thenReturn(0);

// fire event
Expand Down

0 comments on commit 7a0b379

Please sign in to comment.