Skip to content

Commit

Permalink
optimize out intermediary highlight list from getter
Browse files Browse the repository at this point in the history
  • Loading branch information
rfresh2 committed Dec 5, 2024
1 parent 2a1bfa2 commit 317c715
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,13 @@ public void setWindow(int regionX, int regionZ, int regionSize) {

private void loadHighlightsInWindow() {
executorService.execute(() -> {
final List<ChunkHighlightData> chunks = database.getHighlightsInWindow(
synchronized (this.chunks) {
database.getHighlightsInWindow(
dimension,
windowRegionX - windowRegionSize, windowRegionX + windowRegionSize,
windowRegionZ - windowRegionSize, windowRegionZ + windowRegionSize
);
synchronized (this.chunks) {
for (int i = 0; i < chunks.size(); i++) {
final ChunkHighlightData chunk = chunks.get(i);
this.chunks.put(chunkPosToLong(chunk.x(), chunk.z()), chunk.foundTime());
}
windowRegionZ - windowRegionSize, windowRegionZ + windowRegionSize,
(x, y, time) -> this.chunks.put(chunkPosToLong(x, y), time)
);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public List<ChunkHighlightData> getHighlightsInWindow(
chunks.add(new ChunkHighlightData(
resultSet.getInt("x"),
resultSet.getInt("z"),
resultSet.getInt("foundTime")));
resultSet.getLong("foundTime")));
}
return chunks;
}
Expand All @@ -123,6 +123,37 @@ public List<ChunkHighlightData> getHighlightsInWindow(
return Collections.emptyList();
}

@FunctionalInterface
public interface HighlightConsumer {
void accept(int x, int z, long foundTime);
}

// avoids instantiating the intermediary list
public void getHighlightsInWindow(
final ResourceKey<Level> dimension,
final int regionXMin, final int regionXMax,
final int regionZMin, final int regionZMax,
HighlightConsumer consumer
) {
try (var statement = connection.createStatement()) {
try (ResultSet resultSet = statement.executeQuery(
"SELECT * FROM \"" + getTableName(dimension) + "\" "
+ "WHERE x >= " + regionCoordToChunkCoord(regionXMin) + " AND x <= " + regionCoordToChunkCoord(regionXMax)
+ " AND z >= " + regionCoordToChunkCoord(regionZMin) + " AND z <= " + regionCoordToChunkCoord(regionZMax))) {
while (resultSet.next()) {
consumer.accept(
resultSet.getInt("x"),
resultSet.getInt("z"),
resultSet.getLong("foundTime")
);
}
}
} catch (final Exception e) {
XaeroPlus.LOGGER.error("Error getting chunks from {} database in dimension: {}, window: {}-{}, {}-{}", databaseName, dimension.location(), regionXMin, regionXMax, regionZMin, regionZMax, e);
// fall through
}
}

public void removeHighlight(final int x, final int z, final ResourceKey<Level> dimension) {
try (var statement = connection.createStatement()) {
statement.executeUpdate("DELETE FROM \"" + getTableName(dimension) + "\" WHERE x = " + x + " AND z = " + z);
Expand Down

0 comments on commit 317c715

Please sign in to comment.