jobsonp / sipdroid

Automatically exported from code.google.com/p/sipdroid
GNU General Public License v3.0
0 stars 0 forks source link

org.zoolu.sip.provider.SipParser.indexOfHeader has very poor performance #929

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Profile an incoming call
2. indexOfHeader takes a large amount of CPU time.

Suggested patch;

-  public int indexOfHeader(String hname) {
-    if (str.startsWith(hname, index))
-      return index;
-    String[] target = { '\n' + hname, '\r' + hname };
-    SipParser par = new SipParser(str, index);
-    // par.goTo(target);
-    par.goToIgnoreCase(target);
-    if (par.hasMore())
-      par.skipChar();
-    return par.getPos();
-  }

+  public int indexOfHeader(String hname) {
+    Pattern p = Pattern.compile("^" + hname + ": ",
+        Pattern.CASE_INSENSITIVE
+        | Pattern.MULTILINE);
+    Matcher m = p.matcher(str);
+    if (m.find(index))
+      return m.start();
+    return str.length();
+  }

Slightly better performance could be gained by building a single static Pattern 
per caller.

Original issue reported on code.google.com by jer...@lakeman.me on 10 Jun 2011 at 4:32

GoogleCodeExporter commented 8 years ago

Original comment by pmerl...@googlemail.com on 11 Jun 2011 at 1:48

GoogleCodeExporter commented 8 years ago

Original comment by pmerl...@googlemail.com on 24 Jun 2011 at 10:02