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:
jrandom
2004-06-22 04:29:28 +00:00
committed by zzz
parent b6670ee23a
commit 2f17bfd71c

View File

@ -47,8 +47,19 @@ public class OutboundMessageRegistry {
_log.log(Log.CRIT, buf.toString()); _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) { public List getOriginalMessages(I2NPMessage message) {
HashSet matches = new HashSet(4); ArrayList matches = new ArrayList(2);
long beforeSync = _context.clock().now(); long beforeSync = _context.clock().now();
Map messages = null; Map messages = null;
@ -62,7 +73,7 @@ public class OutboundMessageRegistry {
long afterSync1 = _context.clock().now(); long afterSync1 = _context.clock().now();
ArrayList matchedRemove = new ArrayList(32); ArrayList matchedRemove = null; // new ArrayList(32);
for (Iterator iter = messages.keySet().iterator(); iter.hasNext(); ) { for (Iterator iter = messages.keySet().iterator(); iter.hasNext(); ) {
Long exp = (Long)iter.next(); Long exp = (Long)iter.next();
OutNetMessage msg = (OutNetMessage)messages.get(exp); OutNetMessage msg = (OutNetMessage)messages.get(exp);
@ -82,7 +93,8 @@ public class OutboundMessageRegistry {
if (isMatch) { if (isMatch) {
if (_log.shouldLog(Log.DEBUG)) if (_log.shouldLog(Log.DEBUG))
_log.debug("Selector matches [" + selector); _log.debug("Selector matches [" + selector);
matches.add(msg); if (!matches.contains(msg))
matches.add(msg);
long beforeCon = _context.clock().now(); long beforeCon = _context.clock().now();
boolean continueMatching = selector.continueMatching(); boolean continueMatching = selector.continueMatching();
long afterCon = _context.clock().now(); long afterCon = _context.clock().now();
@ -102,6 +114,8 @@ public class OutboundMessageRegistry {
if (_log.shouldLog(Log.DEBUG)) if (_log.shouldLog(Log.DEBUG))
_log.debug("Stop matching selector " + selector + " for message " _log.debug("Stop matching selector " + selector + " for message "
+ msg.getMessageType()); + msg.getMessageType());
if (matchedRemove == null)
matchedRemove = new ArrayList(4);
matchedRemove.add(exp); matchedRemove.add(exp);
} }
} else { } else {
@ -111,25 +125,8 @@ public class OutboundMessageRegistry {
} }
long afterSearch = _context.clock().now(); long afterSearch = _context.clock().now();
for (Iterator iter = matchedRemove.iterator(); iter.hasNext(); ) { doRemove(matchedRemove);
Long expiration = (Long)iter.next();
OutNetMessage m = null;
long before = _context.clock().now();
synchronized (_pendingMessages) {
m = (OutNetMessage)_pendingMessages.remove(expiration);
}
long diff = _context.clock().now() - before;
if ( (diff > 500) && (_log.shouldLog(Log.WARN)) )
_log.warn("Took too long syncing on remove (" + diff + "ms");
if (m != null) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("Removing message with selector "
+ m.getReplySelector().getClass().getName()
+ " :" + m.getReplySelector().toString());
}
}
long delay = _context.clock().now() - beforeSync; long delay = _context.clock().now() - beforeSync;
long search = afterSearch - afterSync1; long search = afterSearch - afterSync1;
long sync = afterSync1 - beforeSync; long sync = afterSync1 - beforeSync;
@ -141,11 +138,39 @@ public class OutboundMessageRegistry {
_log.log(level, "getMessages took " + delay + "ms with search time of " _log.log(level, "getMessages took " + delay + "ms with search time of "
+ search + "ms (match: " + matchTime + "ms, continue: " + search + "ms (match: " + matchTime + "ms, continue: "
+ continueTime + "ms, #: " + numMessages + ") and sync time of " + continueTime + "ms, #: " + numMessages + ") and sync time of "
+ sync + "ms for " + matchedRemove.size() + " removed, " + sync + "ms for " + (matchedRemove == null ? 0 : matchedRemove.size())
+ matches.size() + " matches"); + " removed, " + matches.size() + " matches");
} }
return new ArrayList(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) {
m = (OutNetMessage)_pendingMessages.remove(expiration);
}
long diff = _context.clock().now() - before;
if ( (diff > 500) && (_log.shouldLog(Log.WARN)) )
_log.warn("Took too long syncing on remove (" + diff + "ms");
if (m != null) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("Removing message with selector "
+ m.getReplySelector().getClass().getName()
+ " :" + m.getReplySelector().toString());
}
}
}
} }
public void registerPending(OutNetMessage msg) { public void registerPending(OutNetMessage msg) {
@ -307,23 +332,28 @@ public class OutboundMessageRegistry {
*/ */
private List removeMessages() { private List removeMessages() {
long now = OutboundMessageRegistry.this._context.clock().now(); long now = OutboundMessageRegistry.this._context.clock().now();
List removedMessages = new ArrayList(8); List removedMessages = new ArrayList(2);
List expirationsToRemove = new ArrayList(8); List expirationsToRemove = null;
synchronized (_pendingMessages) { synchronized (_pendingMessages) {
for (Iterator iter = _pendingMessages.keySet().iterator(); iter.hasNext();) { for (Iterator iter = _pendingMessages.keySet().iterator(); iter.hasNext();) {
Long expiration = (Long)iter.next(); Long expiration = (Long)iter.next();
if (expiration.longValue() < now) { if (expiration.longValue() < now) {
if (expirationsToRemove == null)
expirationsToRemove = new ArrayList(8);
expirationsToRemove.add(expiration); expirationsToRemove.add(expiration);
} else { } else {
// its sorted // its sorted
break; break;
} }
} }
for (int i = 0; i < expirationsToRemove.size(); i++) {
Long expiration = (Long)expirationsToRemove.get(i); if (expirationsToRemove != null) {
OutNetMessage msg = (OutNetMessage)_pendingMessages.remove(expiration); for (int i = 0; i < expirationsToRemove.size(); i++) {
if (msg != null) Long expiration = (Long)expirationsToRemove.get(i);
removedMessages.add(msg); OutNetMessage msg = (OutNetMessage)_pendingMessages.remove(expiration);
if (msg != null)
removedMessages.add(msg);
}
} }
} }
if (_log.shouldLog(Log.DEBUG)) if (_log.shouldLog(Log.DEBUG))