From 6a86b81417f8ce126d377b18914b5cd692b7c0e2 Mon Sep 17 00:00:00 2001 From: Mike Primm Date: Thu, 8 Dec 2011 15:56:35 +0800 Subject: [PATCH] Start optimize of update event handling --- src/main/java/org/dynmap/MapManager.java | 16 ++--- src/main/java/org/dynmap/UpdateQueue.java | 72 +++++++++++------------ 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/src/main/java/org/dynmap/MapManager.java b/src/main/java/org/dynmap/MapManager.java index 3d017c17..31c2111d 100644 --- a/src/main/java/org/dynmap/MapManager.java +++ b/src/main/java/org/dynmap/MapManager.java @@ -1078,30 +1078,30 @@ public class MapManager { return new File(worldTileDirectory, tile.getFilename()); } - public void pushUpdate(Object update) { - for(DynmapWorld world : getWorlds()) { - world.updates.pushUpdate(update); + public void pushUpdate(Client.Update update) { + int sz = worlds.size(); + for(int i = 0; i < sz; i++) { + worlds.get(i).updates.pushUpdate(update); } } - public void pushUpdate(World world, Object update) { + public void pushUpdate(World world, Client.Update update) { pushUpdate(world.getName(), update); } - public void pushUpdate(String worldName, Object update) { + public void pushUpdate(String worldName, Client.Update update) { DynmapWorld world = getWorld(worldName); if(world != null) world.updates.pushUpdate(update); } - public Object[] getWorldUpdates(String worldName, long since) { + public Client.Update[] getWorldUpdates(String worldName, long since) { DynmapWorld world = getWorld(worldName); if (world == null) - return new Object[0]; + return new Client.Update[0]; return world.updates.getUpdatedObjects(since); } - private static boolean use_legacy = false; /** * Render processor helper - used by code running on render threads to request chunk snapshot cache from server/sync thread */ diff --git a/src/main/java/org/dynmap/UpdateQueue.java b/src/main/java/org/dynmap/UpdateQueue.java index 3dfb244a..96870707 100644 --- a/src/main/java/org/dynmap/UpdateQueue.java +++ b/src/main/java/org/dynmap/UpdateQueue.java @@ -7,65 +7,65 @@ import java.util.ListIterator; public class UpdateQueue { public Object lock = new Object(); - private LinkedList updateQueue = new LinkedList(); + private LinkedList updateQueue = new LinkedList(); - private static final int maxUpdateAge = 120000; + private static final long maxUpdateAge = 120000; + private static final long ageOutPeriod = 5000; + + private long lastageout = 0; - public void pushUpdate(Object obj) { + private void doAgeOut(long now) { + /* If we're due */ + if((now < lastageout) || (now > (lastageout + ageOutPeriod))) { + lastageout = now; + long deadline = now - maxUpdateAge; + ListIterator i = updateQueue.listIterator(0); + while (i.hasNext()) { + Client.Update u = i.next(); + if (u.timestamp < deadline) + i.remove(); + else + break; + } + } + } + + public void pushUpdate(Client.Update obj) { synchronized (lock) { /* Do inside lock - prevent delay between time and actual work */ long now = System.currentTimeMillis(); - long deadline = now - maxUpdateAge; - ListIterator i = updateQueue.listIterator(0); - while (i.hasNext()) { - Update u = i.next(); - if (u.time < deadline || u.obj == obj) - i.remove(); - } - updateQueue.addLast(new Update(now, obj)); + doAgeOut(now); /* Consider age out */ + updateQueue.addLast(obj); } } - private ArrayList tmpupdates = new ArrayList(); + private ArrayList tmpupdates = new ArrayList(); - public Object[] getUpdatedObjects(long since) { - Object[] updates; + public Client.Update[] getUpdatedObjects(long since) { + Client.Update[] updates; synchronized (lock) { long now = System.currentTimeMillis(); - long deadline = now - maxUpdateAge; + doAgeOut(now); /* Consider age out */ + tmpupdates.clear(); - Iterator it = updateQueue.descendingIterator(); + Iterator it = updateQueue.descendingIterator(); while (it.hasNext()) { - Update u = it.next(); - if (u.time >= since) { + Client.Update u = it.next(); + if (u.timestamp >= since) { // Tile is new. - tmpupdates.add(u.obj); - } else if (u.time < deadline) { - // Tile is too old, removing this one (will eventually decrease). - it.remove(); - break; - } else { - // Tile is old, but not old enough for removal. + tmpupdates.add(u); + } + else { break; } } // Reverse output. - updates = new Object[tmpupdates.size()]; + updates = new Client.Update[tmpupdates.size()]; for (int i = 0; i < updates.length; i++) { updates[i] = tmpupdates.get(updates.length-1-i); } } return updates; } - - public static class Update { - public long time; - public Object obj; - - public Update(long time, Object obj) { - this.time = time; - this.obj = obj; - } - } }