Start optimize of update event handling

This commit is contained in:
Mike Primm 2011-12-08 15:56:35 +08:00 committed by mikeprimm
parent e8189c09e0
commit f5563e6be8
2 changed files with 44 additions and 44 deletions

View file

@ -1078,30 +1078,30 @@ public class MapManager {
return new File(worldTileDirectory, tile.getFilename()); return new File(worldTileDirectory, tile.getFilename());
} }
public void pushUpdate(Object update) { public void pushUpdate(Client.Update update) {
for(DynmapWorld world : getWorlds()) { int sz = worlds.size();
world.updates.pushUpdate(update); 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); pushUpdate(world.getName(), update);
} }
public void pushUpdate(String worldName, Object update) { public void pushUpdate(String worldName, Client.Update update) {
DynmapWorld world = getWorld(worldName); DynmapWorld world = getWorld(worldName);
if(world != null) if(world != null)
world.updates.pushUpdate(update); world.updates.pushUpdate(update);
} }
public Object[] getWorldUpdates(String worldName, long since) { public Client.Update[] getWorldUpdates(String worldName, long since) {
DynmapWorld world = getWorld(worldName); DynmapWorld world = getWorld(worldName);
if (world == null) if (world == null)
return new Object[0]; return new Client.Update[0];
return world.updates.getUpdatedObjects(since); 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 * Render processor helper - used by code running on render threads to request chunk snapshot cache from server/sync thread
*/ */

View file

@ -7,65 +7,65 @@ import java.util.ListIterator;
public class UpdateQueue { public class UpdateQueue {
public Object lock = new Object(); public Object lock = new Object();
private LinkedList<Update> updateQueue = new LinkedList<Update>(); private LinkedList<Client.Update> updateQueue = new LinkedList<Client.Update>();
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<Client.Update> 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) { synchronized (lock) {
/* Do inside lock - prevent delay between time and actual work */ /* Do inside lock - prevent delay between time and actual work */
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
long deadline = now - maxUpdateAge; doAgeOut(now); /* Consider age out */
ListIterator<Update> i = updateQueue.listIterator(0); updateQueue.addLast(obj);
while (i.hasNext()) {
Update u = i.next();
if (u.time < deadline || u.obj == obj)
i.remove();
}
updateQueue.addLast(new Update(now, obj));
} }
} }
private ArrayList<Object> tmpupdates = new ArrayList<Object>(); private ArrayList<Client.Update> tmpupdates = new ArrayList<Client.Update>();
public Object[] getUpdatedObjects(long since) { public Client.Update[] getUpdatedObjects(long since) {
Object[] updates; Client.Update[] updates;
synchronized (lock) { synchronized (lock) {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
long deadline = now - maxUpdateAge; doAgeOut(now); /* Consider age out */
tmpupdates.clear(); tmpupdates.clear();
Iterator<Update> it = updateQueue.descendingIterator(); Iterator<Client.Update> it = updateQueue.descendingIterator();
while (it.hasNext()) { while (it.hasNext()) {
Update u = it.next(); Client.Update u = it.next();
if (u.time >= since) { if (u.timestamp >= since) {
// Tile is new. // Tile is new.
tmpupdates.add(u.obj); tmpupdates.add(u);
} else if (u.time < deadline) { }
// Tile is too old, removing this one (will eventually decrease). else {
it.remove();
break;
} else {
// Tile is old, but not old enough for removal.
break; break;
} }
} }
// Reverse output. // Reverse output.
updates = new Object[tmpupdates.size()]; updates = new Client.Update[tmpupdates.size()];
for (int i = 0; i < updates.length; i++) { for (int i = 0; i < updates.length; i++) {
updates[i] = tmpupdates.get(updates.length-1-i); updates[i] = tmpupdates.get(updates.length-1-i);
} }
} }
return updates; return updates;
} }
public static class Update {
public long time;
public Object obj;
public Update(long time, Object obj) {
this.time = time;
this.obj = obj;
}
}
} }