The first thing I detected is that a AttrCompare is created for every call to sortAttributes. The AttrCompare doesn't have any fields but it consumes 16 byte anyhow. So we generated 16 bytes per output node of unneeded garbage(not too ecological). So I have refactor the class so the AttrCompore now is a constant so it is only allocate once. Big save for the garbage collector but not too big for the heap.
Other easy refactor is found in the c14n excl where a lot of string buffers are generated to transform prefix to xmlns:prefix (that's what we were storing in the mapping table), refactor the store procedure so it stores prefix instead of xmlns:prefix (getLocalName function instead of getName) and voila not StringBufffer allocate when handling c14n excl. A small speed up but a good reduction to the heap/garbage collector.
They are not a big thing but are very small changes without too much impact. I include the patch bellow,
Regards, Raul
p.s- for the next time ,What's better: send a patch to the mailing list or open a bug report?
p.s- is it important to check if the namespace definition is relative in the c14n? I ask because the jmp think is the bigger atomic time waster. I take this with a grain of salt because the other profilers don't blame this function a lot, but...
Index: org/apache/xml/security/c14n/helper/C14nHelper.java
===================================================================
RCS file: /home/cvspublic/xml-security/src/org/apache/xml/security/c14n/helper/C14nHelper.java,v
retrieving revision 1.12
diff -u -r1.12 C14nHelper.java
--- org/apache/xml/security/c14n/helper/C14nHelper.java 8 Feb 2004 06:10:13 -0000 1.12
+++ org/apache/xml/security/c14n/helper/C14nHelper.java 19 May 2004 20:44:08 -0000
@@ -32,7 +32,8 @@
*/
public class C14nHelper {
- /**
+ private static final AttrCompare ATR_COMPARE = new AttrCompare();
+/**
* Constructor C14nHelper
*
*/
@@ -45,7 +46,7 @@
* Sorts a list of attributes according to the c14n spec. The list can contain
* both namespaces and regular attributes.
*
- * @param attributes
+ * @param attributes NOTE this array will be sorted.
*
*/
public static final Object[] sortAttributes(Object[] attributes) {
@@ -53,15 +54,15 @@
if (attributes == null) {
return new Attr[0];
}
-
+ /*
Object[] result = new Object[attributes.length];
for (int i=0; i<attributes.length; i++) {
result[i] = attributes[i];
- }
+ }*/
- java.util.Arrays.sort(result, new AttrCompare()); + java.util.Arrays.sort(attributes/*result*/, ATR_COMPARE);
- return result; + return attributes; }
/**
Index: org/apache/xml/security/c14n/implementations/Canonicalizer20010315.java
===================================================================
RCS file: /home/cvspublic/xml-security/src/org/apache/xml/security/c14n/implementations/Canonicalizer20010315.java,v
retrieving revision 1.31
diff -u -r1.31 Canonicalizer20010315.java
--- org/apache/xml/security/c14n/implementations/Canonicalizer20010315.java 12 May 2004 12:00:44 -0000 1.31
+++ org/apache/xml/security/c14n/implementations/Canonicalizer20010315.java 19 May 2004 20:44:09 -0000
@@ -77,7 +77,7 @@
for (int i = 0; i < attrsLength; i++) {
Attr N = (Attr) attrs.item(i);
- String NName=N.getName();
+ String NName=N.getLocalName();
String NValue=N.getValue();
String NUri =N.getNamespaceURI();
@@ -93,7 +93,7 @@
"c14n.Canonicalizer.RelativeNamespace", exArgs);
}
- if ("xml".equals(N.getLocalName())
+ if ("xml".equals(NName)
&& Constants.XML_LANG_SPACE_SpecNS.equals(NValue)) {
//The default mapping for xml must not be output.
continue;
@@ -190,7 +190,7 @@
for (int i = 0; i < attrsLength; i++) {
Attr N = (Attr) attrs.item(i);
- String NName=N.getName();
+ String NName=N.getLocalName();
String NValue=N.getValue();
String NUri =N.getNamespaceURI();
@@ -210,7 +210,7 @@
"c14n.Canonicalizer.RelativeNamespace", exArgs);
}
- if ("xml".equals(N.getLocalName())
+ if ("xml".equals(NName)
&& Constants.XML_LANG_SPACE_SpecNS.equals(NValue)) {
/* except omit namespace node with local name xml, which defines
* the xml prefix, if its string value is http://www.w3.org/XML/1998/namespace.
Index: org/apache/xml/security/c14n/implementations/Canonicalizer20010315Excl.java
===================================================================
RCS file: /home/cvspublic/xml-security/src/org/apache/xml/security/c14n/implementations/Canonicalizer20010315Excl.java,v
retrieving revision 1.14
diff -u -r1.14 Canonicalizer20010315Excl.java
--- org/apache/xml/security/c14n/implementations/Canonicalizer20010315Excl.java 12 May 2004 12:00:44 -0000 1.14
+++ org/apache/xml/security/c14n/implementations/Canonicalizer20010315Excl.java 19 May 2004 20:44:09 -0000
@@ -153,7 +153,7 @@
boolean xmlnsDef=false;
for (int i = 0; i < attrsLength; i++) {
Attr N = (Attr) attrs.item(i);
- String NName=N.getName();
+ String NName=N.getLocalName();
String NNodeValue=N.getNodeValue();
if (!Constants.NamespaceSpecNS.equals(N.getNamespaceURI())) {
@@ -161,7 +161,7 @@
//The Element is output element, add his prefix(if used) to visibyUtilized
String prefix = N.getPrefix();
if ( (prefix != null) && (!prefix.equals("xml") && !prefix.equals("xmlns")) ) {
- visiblyUtilized.add("xmlns:" + prefix);
+ visiblyUtilized.add(/*"xmlns:" +*/ prefix);
} //Add to the result.
result.add(N); @@ -186,7 +186,7 @@
if ((prefix == null) || (prefix.length() == 0)) {
visiblyUtilized.add("xmlns");
} else {
- visiblyUtilized.add("xmlns:" + prefix);
+ visiblyUtilized.add(/*"xmlns:" +*/ prefix);
}
} else {
visiblyUtilized.add("xmlns");
@@ -255,7 +255,7 @@
boolean xmlnsDef=false;
for (int i = 0; i < attrsLength; i++) {
Attr N = (Attr) attrs.item(i);
- String NName=N.getName();
+ String NName=N.getLocalName(); String NNodeValue=N.getNodeValue();
if ( !this._xpathNodeSet.contains(N) ) {
//The node is not in the nodeset(if there is a nodeset)
@@ -268,7 +268,7 @@
//The Element is output element, add his prefix(if used) to visibyUtilized
String prefix = N.getPrefix();
if ((prefix != null) && (!prefix.equals("xml") && !prefix.equals("xmlns")) ){
- visiblyUtilized.add("xmlns:" + prefix);
+ visiblyUtilized.add(/*"xmlns:" +*/ prefix);
} //Add to the result.
result.add(N);
@@ -298,7 +298,7 @@
if ((prefix == null) || (prefix.length() == 0)) {
visiblyUtilized.add("xmlns");
} else {
- visiblyUtilized.add("xmlns:" + prefix);
+ visiblyUtilized.add(/*"xmlns:" +*/ prefix);
}
} else {
visiblyUtilized.add("xmlns");
Index: org/apache/xml/security/c14n/implementations/CanonicalizerBase.java
===================================================================
RCS file: /home/cvspublic/xml-security/src/org/apache/xml/security/c14n/implementations/CanonicalizerBase.java,v
retrieving revision 1.6
diff -u -r1.6 CanonicalizerBase.java
--- org/apache/xml/security/c14n/implementations/CanonicalizerBase.java 12 May 2004 12:00:44 -0000 1.6
+++ org/apache/xml/security/c14n/implementations/CanonicalizerBase.java 19 May 2004 20:44:12 -0000
@@ -520,7 +520,7 @@
&& Constants.XML_LANG_SPACE_SpecNS.equals(N.getNodeValue())) {
continue;
} - ns.addMapping(N.getName(),N.getValue(),N); + ns.addMapping(N.getLocalName(),N.getValue(),N); } }
}
@@ -566,7 +566,7 @@
case '"' :
this._writer.write(""");
break;
-
+ case 0x09 : // '\t'
this._writer.write("	");
break;
Index: org/apache/xml/security/transforms/params/InclusiveNamespaces.java
===================================================================
RCS file: /home/cvspublic/xml-security/src/org/apache/xml/security/transforms/params/InclusiveNamespaces.java,v
retrieving revision 1.5
diff -u -r1.5 InclusiveNamespaces.java
--- org/apache/xml/security/transforms/params/InclusiveNamespaces.java 8 Feb 2004 06:11:35 -0000 1.5
+++ org/apache/xml/security/transforms/params/InclusiveNamespaces.java 19 May 2004 20:44:15 -0000
@@ -153,9 +153,9 @@
String prefix = st.nextToken();
if (prefix.equals("#default")) { - prefixes.add("xmlns" + ""); + prefixes.add("xmlns" /*+""*/); } else { - prefixes.add("xmlns:" + prefix); + prefixes.add(/*"xmlns:" +*/ prefix); } }