writers need buffering too
"Pieter Schoenmakers" <[email protected]> Mon, 17 Nov 2003 00:27:12 +0100
| Newsgroups | gmane.comp.java.enhydra.xmlc |
|---|---|
| Organization | Aspiratie Webdevelopment Holding |
| Message-ID | <003a01c3ac99$29d62660$11001fac@todo> |
Hi,
While investigating the speed of an application that uses XMLC 2.2.3, I
encountered a serious performance bug in DOMFormatter. The DOMFormatter
is used to write out the HTML (or XML) representation of a DOM tree. It
uses a Writer onto a buffered output stream, but the Writer is itself
not wrapped in a BufferedWriter. Without the BufferedWriter, every
invocation of one of the Writer's write() methods causes the allocation
of one object, presumably a byte array. Invoking the
single-character-write method is one object worse, presumably it
requires a character array. This includes the frequent invocations to
the write methods to emit a '<', '&', and '>' characters.
I didn't perform any serious measurements targeting this particular
memory consumer. I can relate that this fix was part of an effort to
reduce memory consumption of a page that allocated approximately 260000
objects to display. After some playing with a profiler, the memory
consumption was reduced by 50%, a significant part of which comes from
this XMLC fix. Find the diff attached below. I'm interested to hear
about the real performance-gain figures.
Regards,
Pieter Schoenmakers
diff -r -u
xmlc-src-2.2.3-/xmlc/modules/xmlc/src/org/enhydra/xml/io/DOMFormatter.ja
va
xmlc-src-2.2.3/xmlc/modules/xmlc/src/org/enhydra/xml/io/DOMFormatter.jav
a
---
xmlc-src-2.2.3-/xmlc/modules/xmlc/src/org/enhydra/xml/io/DOMFormatter.ja
va 2003-03-10 10:36:16.000000000 +0100
+++
xmlc-src-2.2.3/xmlc/modules/xmlc/src/org/enhydra/xml/io/DOMFormatter.jav
a 2003-11-13 12:05:37.000000000 +0100
@@ -26,6 +26,7 @@
import org.enhydra.xml.dom.DOMOps;
import org.enhydra.xml.dom.DOMAccess;
import java.io.Writer;
+import java.io.BufferedWriter;
import java.io.StringWriter;
import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
@@ -199,8 +200,8 @@
public void write(Node node,
OutputStream out) throws IOException {
Formatter formatter = getFormatter(node, fOptions, false);
- Writer writer = new OutputStreamWriter(out,
-
formatter.getMIMEEncoding());
+ Writer writer = new BufferedWriter (new OutputStreamWriter(out,
+
formatter.getMIMEEncoding()));
formatter.write(node, writer);
writer.flush();
}