From 625abf9aee7a1f511341fe5c9d3776d40cdc0ace Mon Sep 17 00:00:00 2001 From: Milan Schmittner Date: Tue, 1 Aug 2017 11:31:16 +0200 Subject: [PATCH 1/2] Cache buffer occupancy Otherwise, we have to recalclate everytime we want to get the currently free space. If we carry many messages, the current approach (iterarting over all messages) is highly inefficient. --- src/routing/MessageRouter.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/routing/MessageRouter.java b/src/routing/MessageRouter.java index 4d21233a1..239848c1e 100644 --- a/src/routing/MessageRouter.java +++ b/src/routing/MessageRouter.java @@ -95,6 +95,8 @@ public abstract class MessageRouter { private DTNHost host; /** size of the buffer */ private long bufferSize; + /** current occupancy of the buffer */ + private long bufferOccupancy; /** TTL for all messages */ protected int msgTtl; /** Queue mode for sending messages */ @@ -157,6 +159,7 @@ public void init(DTNHost host, List mListeners) { this.blacklistedMessages = new HashMap(); this.mListeners = mListeners; this.host = host; + this.bufferOccupancy = 0; } /** @@ -274,17 +277,11 @@ public long getBufferSize() { * size isn't defined) */ public long getFreeBufferSize() { - long occupancy = 0; - if (this.getBufferSize() == Integer.MAX_VALUE) { return Integer.MAX_VALUE; } - for (Message m : getMessageCollection()) { - occupancy += m.getSize(); - } - - return this.getBufferSize() - occupancy; + return this.getBufferSize() - this.bufferOccupancy; } /** @@ -438,6 +435,7 @@ protected boolean isIncomingMessage(String id) { */ protected void addToMessages(Message m, boolean newMessage) { this.messages.put(m.getId(), m); + this.bufferOccupancy += m.getSize(); if (newMessage) { for (MessageListener ml : this.mListeners) { @@ -453,6 +451,7 @@ protected void addToMessages(Message m, boolean newMessage) { */ protected Message removeFromMessages(String id) { Message m = this.messages.remove(id); + this.bufferOccupancy -= m.getSize(); return m; } From b45e6ff5c6c36fee5ea9df53e910c8972d3c8382 Mon Sep 17 00:00:00 2001 From: Milan Schmittner Date: Tue, 1 Aug 2017 11:33:18 +0200 Subject: [PATCH 2/2] DTNHost tracks active connections Again, the current appraoch (iterating over all interfaces) is highly inefficient. In the default scenario, this patch resulted in a 20% speed-up on my test hardware. --- src/core/DTNHost.java | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/core/DTNHost.java b/src/core/DTNHost.java index 258be0450..502c5b48b 100644 --- a/src/core/DTNHost.java +++ b/src/core/DTNHost.java @@ -6,6 +6,7 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.List; import movement.MovementModel; @@ -34,6 +35,7 @@ public class DTNHost implements Comparable { private List msgListeners; private List movListeners; private List net; + private List allConnections; private ModuleCommunicationBus comBus; static { @@ -60,6 +62,7 @@ public DTNHost(List msgLs, this.address = getNextAddress(); this.name = groupId+address; this.net = new ArrayList(); + this.allConnections = new ArrayList(); for (NetworkInterface i : interf) { NetworkInterface ni = i.replicate(); @@ -165,25 +168,21 @@ public ModuleCommunicationBus getComBus() { * @param con The connection object whose state changed */ public void connectionUp(Connection con) { + this.allConnections.add(con); this.router.changedConnection(con); } public void connectionDown(Connection con) { + this.allConnections.remove(con); this.router.changedConnection(con); } /** - * Returns a copy of the list of connections this host has with other hosts - * @return a copy of the list of connections this host has with other hosts + * Returns an immutable list of connections this host has with other hosts + * @return an immutable list of connections this host has with other hosts */ public List getConnections() { - List lc = new ArrayList(); - - for (NetworkInterface i : net) { - lc.addAll(i.getConnections()); - } - - return lc; + return Collections.unmodifiableList(allConnections); } /**