Skip to content

Commit

Permalink
perf: Avoid expensive Plot#getOwner calls in Plot#getOwners (#4418)
Browse files Browse the repository at this point in the history
* Avoid expensive Plot#getOwner calls in Plot#getOwners

* Don't check for the owner beforehand, because it's done in the loop regardless
  • Loading branch information
Vrganj authored May 9, 2024
1 parent a7447c9 commit 83fe761
Showing 1 changed file with 8 additions and 21 deletions.
29 changes: 8 additions & 21 deletions Core/src/main/java/com/plotsquared/core/plot/Plot.java
Original file line number Diff line number Diff line change
Expand Up @@ -643,35 +643,22 @@ public void setOwner(final @NonNull UUID owner) {
}

/**
* Gets a immutable set of owner UUIDs for a plot (supports multi-owner mega-plots).
* Gets an immutable set of owner UUIDs for a plot (supports multi-owner mega-plots).
* <p>
* This method cannot be used to add or remove owners from a plot.
* </p>
*
* @return Immutable view of plot owners
* @return Immutable set of plot owners
*/
public @NonNull Set<UUID> getOwners() {
if (this.getOwner() == null) {
return ImmutableSet.of();
}
if (isMerged()) {
Set<Plot> plots = getConnectedPlots();
Plot[] array = plots.toArray(new Plot[0]);
ImmutableSet.Builder<UUID> owners = ImmutableSet.builder();
UUID last = this.getOwner();
owners.add(this.getOwner());
for (final Plot current : array) {
if (current.getOwner() == null) {
continue;
}
if (last == null || current.getOwner().getMostSignificantBits() != last.getMostSignificantBits()) {
owners.add(current.getOwner());
last = current.getOwner();
}
ImmutableSet.Builder<UUID> owners = ImmutableSet.builder();
for (Plot plot : getConnectedPlots()) {
UUID owner = plot.getOwner();
if (owner != null) {
owners.add(owner);
}
return owners.build();
}
return ImmutableSet.of(this.getOwner());
return owners.build();
}

/**
Expand Down

0 comments on commit 83fe761

Please sign in to comment.