🆙 Fix rooms loading

- Layout Cache (eliminates 1 DB query per room load)

Standard room models (model_a, model_b, etc.) are loaded once at startup and cached in memory
RoomLayout gets a new constructor from cached data instead of ResultSet
~99% of rooms use standard models, so this saves a DB round-trip on nearly every room load

- Better Parallel Pipeline (reduced critical path)

Before: layout → [items|rights|wordfilter] → heightmap → [bots|pets|wired]
After:  layout → [items|rights|wordfilter|bots|pets] → [heightmap|wired]

Bots and pets only need layout for positioning, not items - so they now start immediately
Wired only needs items loaded (not heightmap) - so it now runs parallel with heightmap

-  Deferred Promotion Query (faster Room instantiation)

Moved room_promotions DB query from constructor to loadDataInternal() as an async task
Room constructor now only runs bans query (needed for entry check)
Saves ~20ms per Room instantiation for promoted rooms

-  Smart Heightmap (reduced tile iterations by 80-95%)

Instead of updating ALL tiles (1024 for 32x32 room), only updates tiles with items on them
Uses getTilesAt() for correct rotation-aware multi-tile coverage
For a room with 100 items on a 32x32 grid: ~200 tile updates instead of 1024
This commit is contained in:
duckietm
2026-03-26 12:44:12 +01:00
parent b755828d85
commit 95aea934bd
4 changed files with 180 additions and 97 deletions
@@ -221,22 +221,8 @@ public class Room implements Comparable<Room>, ISerialize, Runnable {
this.bannedHabbos = new TIntObjectHashMap<>(); this.bannedHabbos = new TIntObjectHashMap<>();
try (Connection connection = Emulator.getDatabase().getDataSource() try (Connection connection = Emulator.getDatabase().getDataSource().getConnection()) {
.getConnection(); PreparedStatement statement = connection.prepareStatement( // Load bans eagerly (needed for entry check before loadData)
"SELECT * FROM room_promotions WHERE room_id = ? AND end_timestamp > ? LIMIT 1")) {
if (this.promoted) {
statement.setInt(1, this.id);
statement.setInt(2, Emulator.getIntUnixTimestamp());
try (ResultSet promotionSet = statement.executeQuery()) {
this.promoted = false;
if (promotionSet.next()) {
this.promoted = true;
this.promotion = new RoomPromotion(this, promotionSet);
}
}
}
this.loadBans(connection); this.loadBans(connection);
} catch (SQLException e) { } catch (SQLException e) {
LOGGER.error("Caught SQL exception", e); LOGGER.error("Caught SQL exception", e);
@@ -489,7 +475,26 @@ public class Room implements Comparable<Room>, ISerialize, Runnable {
LOGGER.error("Caught exception loading layout", e); LOGGER.error("Caught exception loading layout", e);
} }
// Phase 2: Load items and rights in parallel (independent operations) if (this.promoted) {
CompletableFuture.runAsync(() -> {
try (Connection promoConnection = Emulator.getDatabase().getDataSource().getConnection();
PreparedStatement stmt = promoConnection.prepareStatement(
"SELECT * FROM room_promotions WHERE room_id = ? AND end_timestamp > ? LIMIT 1")) {
stmt.setInt(1, this.id);
stmt.setInt(2, Emulator.getIntUnixTimestamp());
try (ResultSet promoSet = stmt.executeQuery()) {
this.promoted = false;
if (promoSet.next()) {
this.promoted = true;
this.promotion = new RoomPromotion(this, promoSet);
}
}
} catch (Exception e) {
LOGGER.error("Caught exception loading promotion", e);
}
}, Emulator.getThreading().getService());
}
CompletableFuture<Void> itemsFuture = CompletableFuture.runAsync(() -> { CompletableFuture<Void> itemsFuture = CompletableFuture.runAsync(() -> {
try (Connection itemConnection = Emulator.getDatabase().getDataSource().getConnection()) { try (Connection itemConnection = Emulator.getDatabase().getDataSource().getConnection()) {
this.loadItems(itemConnection); this.loadItems(itemConnection);
@@ -514,21 +519,7 @@ public class Room implements Comparable<Room>, ISerialize, Runnable {
} }
}, Emulator.getThreading().getService()); }, Emulator.getThreading().getService());
// Wait for items to be loaded before loading wired data (wired depends on items) // Bots and pets only need layout for positioning - start them now
try {
itemsFuture.join();
} catch (Exception e) {
LOGGER.error("Error waiting for items to load", e);
}
// Phase 3: Load heightmap after items are loaded (depends on items for stack heights)
try {
this.loadHeightmap();
} catch (Exception e) {
LOGGER.error("Caught exception loading heightmap", e);
}
// Phase 4: Load bots, pets, and wired data in parallel (all depend on layout + items)
CompletableFuture<Void> botsFuture = CompletableFuture.runAsync(() -> { CompletableFuture<Void> botsFuture = CompletableFuture.runAsync(() -> {
try (Connection botsConnection = Emulator.getDatabase().getDataSource().getConnection()) { try (Connection botsConnection = Emulator.getDatabase().getDataSource().getConnection()) {
this.loadBots(botsConnection); this.loadBots(botsConnection);
@@ -545,6 +536,22 @@ public class Room implements Comparable<Room>, ISerialize, Runnable {
} }
}, Emulator.getThreading().getService()); }, Emulator.getThreading().getService());
// Wait for items (needed for heightmap + wired)
try {
itemsFuture.join();
} catch (Exception e) {
LOGGER.error("Error waiting for items to load", e);
}
// Phase 3: Heightmap and wired in parallel (both depend on items, not on each other)
CompletableFuture<Void> heightmapFuture = CompletableFuture.runAsync(() -> {
try {
this.loadHeightmap();
} catch (Exception e) {
LOGGER.error("Caught exception loading heightmap", e);
}
}, Emulator.getThreading().getService());
CompletableFuture<Void> wiredFuture = CompletableFuture.runAsync(() -> { CompletableFuture<Void> wiredFuture = CompletableFuture.runAsync(() -> {
try (Connection wiredConnection = Emulator.getDatabase().getDataSource().getConnection()) { try (Connection wiredConnection = Emulator.getDatabase().getDataSource().getConnection()) {
this.loadWiredData(wiredConnection); this.loadWiredData(wiredConnection);
@@ -553,9 +560,9 @@ public class Room implements Comparable<Room>, ISerialize, Runnable {
} }
}, Emulator.getThreading().getService()); }, Emulator.getThreading().getService());
// Wait for all parallel operations to complete // Wait for all remaining operations
try { try {
CompletableFuture.allOf(rightsFuture, wordFilterFuture, botsFuture, petsFuture, wiredFuture).join(); CompletableFuture.allOf(rightsFuture, wordFilterFuture, botsFuture, petsFuture, heightmapFuture, wiredFuture).join();
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error waiting for parallel room data loading", e); LOGGER.error("Error waiting for parallel room data loading", e);
} }
@@ -54,6 +54,25 @@ public class RoomLayout {
} }
} }
public RoomLayout(RoomManager.RoomLayoutData data, Room room) {
this.room = room;
try {
this.name = data.name;
this.doorX = (short) data.doorX;
this.doorY = (short) data.doorY;
this.doorDirection = data.doorDir;
this.heightmap = data.heightmap;
this.parse();
this.pathfinder = new PathfinderImpl(this.room, MAXIMUM_STEP_HEIGHT,
Emulator.getConfig().getBoolean("pathfinder.step.allow.falling", true),
Emulator.getConfig().getBoolean("pathfinder.retro-style.diagonals", false));
} catch (Exception e) {
LOGGER.error("Caught exception", e);
}
}
public static boolean squareInSquare(Rectangle outerSquare, Rectangle innerSquare) { public static boolean squareInSquare(Rectangle outerSquare, Rectangle innerSquare) {
if (outerSquare.x > innerSquare.x) { if (outerSquare.x > innerSquare.x) {
return false; return false;
@@ -72,6 +72,7 @@ public class RoomManager {
public static boolean SHOW_PUBLIC_IN_POPULAR_TAB = false; public static boolean SHOW_PUBLIC_IN_POPULAR_TAB = false;
private final THashMap<Integer, RoomCategory> roomCategories; private final THashMap<Integer, RoomCategory> roomCategories;
private final List<String> mapNames; private final List<String> mapNames;
private final ConcurrentHashMap<String, RoomLayoutData> layoutCache;
private final ConcurrentHashMap<Integer, Room> activeRooms; private final ConcurrentHashMap<Integer, Room> activeRooms;
private final ConcurrentHashMap<Integer, Set<Integer>> roomsByOwner; private final ConcurrentHashMap<Integer, Set<Integer>> roomsByOwner;
private final ArrayList<Class<? extends Game>> gameTypes; private final ArrayList<Class<? extends Game>> gameTypes;
@@ -80,6 +81,7 @@ public class RoomManager {
long millis = System.currentTimeMillis(); long millis = System.currentTimeMillis();
this.roomCategories = new THashMap<>(); this.roomCategories = new THashMap<>();
this.mapNames = new ArrayList<>(); this.mapNames = new ArrayList<>();
this.layoutCache = new ConcurrentHashMap<>();
this.activeRooms = new ConcurrentHashMap<>(); this.activeRooms = new ConcurrentHashMap<>();
this.roomsByOwner = new ConcurrentHashMap<>(); this.roomsByOwner = new ConcurrentHashMap<>();
this.loadRoomCategories(); this.loadRoomCategories();
@@ -114,9 +116,12 @@ public class RoomManager {
public void loadRoomModels() { public void loadRoomModels() {
this.mapNames.clear(); this.mapNames.clear();
this.layoutCache.clear();
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); Statement statement = connection.createStatement(); ResultSet set = statement.executeQuery("SELECT * FROM room_models")) { try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); Statement statement = connection.createStatement(); ResultSet set = statement.executeQuery("SELECT * FROM room_models")) {
while (set.next()) { while (set.next()) {
this.mapNames.add(set.getString("name")); String name = set.getString("name");
this.mapNames.add(name);
this.layoutCache.put(name, new RoomLayoutData(set));
} }
} catch (SQLException e) { } catch (SQLException e) {
LOGGER.error("Caught SQL exception", e); LOGGER.error("Caught SQL exception", e);
@@ -446,6 +451,12 @@ public class RoomManager {
} }
public RoomLayout loadLayout(String name, Room room) { public RoomLayout loadLayout(String name, Room room) {
RoomLayoutData cached = this.layoutCache.get(name);
if (cached != null) {
return new RoomLayout(cached, room);
}
// Fallback to DB if not in cache (should not happen for standard models)
RoomLayout layout = null; RoomLayout layout = null;
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT * FROM room_models WHERE name = ? LIMIT 1")) { try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT * FROM room_models WHERE name = ? LIMIT 1")) {
statement.setString(1, name); statement.setString(1, name);
@@ -1623,4 +1634,24 @@ public class RoomManager {
this.duration = duration; this.duration = duration;
} }
} }
/**
* Cached layout data from room_models to avoid repeated DB queries.
* The raw data is shared; each Room gets its own RoomLayout instance.
*/
static class RoomLayoutData {
final String name;
final int doorX;
final int doorY;
final int doorDir;
final String heightmap;
RoomLayoutData(ResultSet set) throws SQLException {
this.name = set.getString("name");
this.doorX = set.getInt("door_x");
this.doorY = set.getInt("door_y");
this.doorDir = set.getInt("door_dir");
this.heightmap = set.getString("heightmap");
}
}
} }
@@ -517,18 +517,44 @@ public class RoomTileManager {
/** /**
* Loads the heightmap for the room. * Loads the heightmap for the room.
* Only updates tiles that have items on them (+ door tile) instead of all tiles,
* using getTilesAt() to correctly handle rotated multi-tile furniture.
*/ */
public void loadHeightmap() { public void loadHeightmap() {
RoomLayout layout = this.room.getLayout(); RoomLayout layout = this.room.getLayout();
if (layout != null) { if (layout != null) {
for (short x = 0; x < layout.getMapSizeX(); x++) { THashSet<HabboItem> floorItems = this.room.getFloorItems();
for (short y = 0; y < layout.getMapSizeY(); y++) {
RoomTile tile = layout.getTile(x, y); if (floorItems.isEmpty()) {
if (tile != null) { // No items - only update door tile
RoomTile doorTile = layout.getDoorTile();
if (doorTile != null) {
this.updateTile(doorTile);
}
return;
}
// Collect unique tiles occupied by items (handles rotation)
THashSet<RoomTile> tilesToUpdate = new THashSet<>();
for (HabboItem item : floorItems) {
RoomTile baseTile = layout.getTile(item.getX(), item.getY());
if (baseTile != null) {
tilesToUpdate.addAll(layout.getTilesAt(baseTile,
item.getBaseItem().getWidth(),
item.getBaseItem().getLength(),
item.getRotation()));
}
}
// Always include door tile
RoomTile doorTile = layout.getDoorTile();
if (doorTile != null) {
tilesToUpdate.add(doorTile);
}
for (RoomTile tile : tilesToUpdate) {
this.updateTile(tile); this.updateTile(tile);
} }
}
}
} else { } else {
LOGGER.error("Unknown Room Layout for Room (ID: {})", this.room.getId()); LOGGER.error("Unknown Room Layout for Room (ID: {})", this.room.getId());
} }