minor refactoring. i hate how large that method is, but beyond the essential stuff, its pretty much just logging and benchmarking.
plus, yeah, this method still takes too long in some situations. working on identifying why...
This commit is contained in:
@ -47,8 +47,19 @@ public class OutboundMessageRegistry {
|
||||
_log.log(Log.CRIT, buf.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all messages that are waiting for the specified message. In
|
||||
* addition, those matches may include instructions to either continue or not
|
||||
* continue waiting for further replies - if it should continue, the matched
|
||||
* message remains in the registry, but if it shouldn't continue, the matched
|
||||
* message is removed from the registry.
|
||||
*
|
||||
* @param message Payload received that may be a reply to something we sent
|
||||
* @return List of OutNetMessage describing messages that were waiting for
|
||||
* the payload
|
||||
*/
|
||||
public List getOriginalMessages(I2NPMessage message) {
|
||||
HashSet matches = new HashSet(4);
|
||||
ArrayList matches = new ArrayList(2);
|
||||
long beforeSync = _context.clock().now();
|
||||
|
||||
Map messages = null;
|
||||
@ -62,7 +73,7 @@ public class OutboundMessageRegistry {
|
||||
|
||||
long afterSync1 = _context.clock().now();
|
||||
|
||||
ArrayList matchedRemove = new ArrayList(32);
|
||||
ArrayList matchedRemove = null; // new ArrayList(32);
|
||||
for (Iterator iter = messages.keySet().iterator(); iter.hasNext(); ) {
|
||||
Long exp = (Long)iter.next();
|
||||
OutNetMessage msg = (OutNetMessage)messages.get(exp);
|
||||
@ -82,6 +93,7 @@ public class OutboundMessageRegistry {
|
||||
if (isMatch) {
|
||||
if (_log.shouldLog(Log.DEBUG))
|
||||
_log.debug("Selector matches [" + selector);
|
||||
if (!matches.contains(msg))
|
||||
matches.add(msg);
|
||||
long beforeCon = _context.clock().now();
|
||||
boolean continueMatching = selector.continueMatching();
|
||||
@ -102,6 +114,8 @@ public class OutboundMessageRegistry {
|
||||
if (_log.shouldLog(Log.DEBUG))
|
||||
_log.debug("Stop matching selector " + selector + " for message "
|
||||
+ msg.getMessageType());
|
||||
if (matchedRemove == null)
|
||||
matchedRemove = new ArrayList(4);
|
||||
matchedRemove.add(exp);
|
||||
}
|
||||
} else {
|
||||
@ -111,8 +125,35 @@ public class OutboundMessageRegistry {
|
||||
}
|
||||
long afterSearch = _context.clock().now();
|
||||
|
||||
for (Iterator iter = matchedRemove.iterator(); iter.hasNext(); ) {
|
||||
Long expiration = (Long)iter.next();
|
||||
doRemove(matchedRemove);
|
||||
|
||||
long delay = _context.clock().now() - beforeSync;
|
||||
long search = afterSearch - afterSync1;
|
||||
long sync = afterSync1 - beforeSync;
|
||||
|
||||
int level = Log.DEBUG;
|
||||
if (delay > 1000)
|
||||
level = Log.ERROR;
|
||||
if (_log.shouldLog(level)) {
|
||||
_log.log(level, "getMessages took " + delay + "ms with search time of "
|
||||
+ search + "ms (match: " + matchTime + "ms, continue: "
|
||||
+ continueTime + "ms, #: " + numMessages + ") and sync time of "
|
||||
+ sync + "ms for " + (matchedRemove == null ? 0 : matchedRemove.size())
|
||||
+ " removed, " + matches.size() + " matches");
|
||||
}
|
||||
|
||||
return matches;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified messages from the pending list
|
||||
*
|
||||
* @param matchedRemove expiration (Long) of the pending message to remove
|
||||
*/
|
||||
private void doRemove(List matchedRemove) {
|
||||
if (matchedRemove != null) {
|
||||
for (int i = 0; i < matchedRemove.size(); i++) {
|
||||
Long expiration = (Long)matchedRemove.get(i);
|
||||
OutNetMessage m = null;
|
||||
long before = _context.clock().now();
|
||||
synchronized (_pendingMessages) {
|
||||
@ -129,23 +170,7 @@ public class OutboundMessageRegistry {
|
||||
+ " :" + m.getReplySelector().toString());
|
||||
}
|
||||
}
|
||||
|
||||
long delay = _context.clock().now() - beforeSync;
|
||||
long search = afterSearch - afterSync1;
|
||||
long sync = afterSync1 - beforeSync;
|
||||
|
||||
int level = Log.DEBUG;
|
||||
if (delay > 1000)
|
||||
level = Log.ERROR;
|
||||
if (_log.shouldLog(level)) {
|
||||
_log.log(level, "getMessages took " + delay + "ms with search time of "
|
||||
+ search + "ms (match: " + matchTime + "ms, continue: "
|
||||
+ continueTime + "ms, #: " + numMessages + ") and sync time of "
|
||||
+ sync + "ms for " + matchedRemove.size() + " removed, "
|
||||
+ matches.size() + " matches");
|
||||
}
|
||||
|
||||
return new ArrayList(matches);
|
||||
}
|
||||
|
||||
public void registerPending(OutNetMessage msg) {
|
||||
@ -307,18 +332,22 @@ public class OutboundMessageRegistry {
|
||||
*/
|
||||
private List removeMessages() {
|
||||
long now = OutboundMessageRegistry.this._context.clock().now();
|
||||
List removedMessages = new ArrayList(8);
|
||||
List expirationsToRemove = new ArrayList(8);
|
||||
List removedMessages = new ArrayList(2);
|
||||
List expirationsToRemove = null;
|
||||
synchronized (_pendingMessages) {
|
||||
for (Iterator iter = _pendingMessages.keySet().iterator(); iter.hasNext();) {
|
||||
Long expiration = (Long)iter.next();
|
||||
if (expiration.longValue() < now) {
|
||||
if (expirationsToRemove == null)
|
||||
expirationsToRemove = new ArrayList(8);
|
||||
expirationsToRemove.add(expiration);
|
||||
} else {
|
||||
// its sorted
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (expirationsToRemove != null) {
|
||||
for (int i = 0; i < expirationsToRemove.size(); i++) {
|
||||
Long expiration = (Long)expirationsToRemove.get(i);
|
||||
OutNetMessage msg = (OutNetMessage)_pendingMessages.remove(expiration);
|
||||
@ -326,6 +355,7 @@ public class OutboundMessageRegistry {
|
||||
removedMessages.add(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (_log.shouldLog(Log.DEBUG))
|
||||
_log.debug("Removed " + removedMessages.size() + " messages");
|
||||
return removedMessages;
|
||||
|
Reference in New Issue
Block a user