Re: Unnecessary array allocation and copying
Ugo Cei wrote: While browsing our sources, I came across this snippet from o.a.c..components.pipeline.AbstractProcessingPipeline: // execute the pipeline: this.generator.generate(); byte[] data = os.toByteArray(); environment.setContentLength(data.length); environment.getOutputStream(0).write(data); But from the javadocs of java.io.ByteArrayOutputStream#toByteArray, I read: "Creates a newly allocated byte array. Its size is the current size of this output stream and the valid contents of the buffer have been copied into it." Thus I wonder: why are we doing this copy instead of doing: // execute the pipeline: this.generator.generate(); environment.setContentLength(os.size()); os.writeTo(environment.getOutputStream(0)); If I'm not mistaken, this would avoid allocating and copying an array as large as the serializer's output. Or am I missing something subtle here? I guess you just found a more efficient way to achieve exactly the same thing ;-) It seems to me we can safely avoid this array allocation. Sylvain -- Sylvain Wallez Anyware Technologies http://www.apache.org/~sylvain http://www.anyware-tech.com { XML, Java, Cocoon, OpenSource }*{ Training, Consulting, Projects }
Re: Unnecessary array allocation and copying
Il giorno 26/apr/04, alle 23:19, Bruno Dumon ha scritto: The same pattern occurs at other locations also, e.g. AbstractCachingProcessingPipeline line 246 Yes, there are four occurrences between AbstractProcessingPipeline and AbstractCachingProcessingPipeline. I've optimized all four locally and tested by running random samples. So far, so good. Unfortunately, our test suite does not compile ATM (gr), so I'll probably commit and see if someone complains. Ugo
Re: Unnecessary array allocation and copying
On Mon, 2004-04-26 at 22:39, Ugo Cei wrote: > While browsing our sources, I came across this snippet from > o.a.c..components.pipeline.AbstractProcessingPipeline: > > // execute the pipeline: > this.generator.generate(); > byte[] data = os.toByteArray(); > environment.setContentLength(data.length); > environment.getOutputStream(0).write(data); > > But from the javadocs of java.io.ByteArrayOutputStream#toByteArray, I > read: > > "Creates a newly allocated byte array. Its size is the current size of > this output stream and the valid contents of the buffer have been > copied into it." > > Thus I wonder: why are we doing this copy instead of doing: > > // execute the pipeline: > this.generator.generate(); > environment.setContentLength(os.size()); > os.writeTo(environment.getOutputStream(0)); > > If I'm not mistaken, this would avoid allocating and copying an array > as large as the serializer's output. Or am I missing something subtle > here? I don't think so, this seems like a good optimalisation. The same pattern occurs at other locations also, e.g. AbstractCachingProcessingPipeline line 246 -- Bruno Dumon http://outerthought.org/ Outerthought - Open Source, Java & XML Competence Support Center [EMAIL PROTECTED] [EMAIL PROTECTED]
Unnecessary array allocation and copying
While browsing our sources, I came across this snippet from o.a.c..components.pipeline.AbstractProcessingPipeline: // execute the pipeline: this.generator.generate(); byte[] data = os.toByteArray(); environment.setContentLength(data.length); environment.getOutputStream(0).write(data); But from the javadocs of java.io.ByteArrayOutputStream#toByteArray, I read: "Creates a newly allocated byte array. Its size is the current size of this output stream and the valid contents of the buffer have been copied into it." Thus I wonder: why are we doing this copy instead of doing: // execute the pipeline: this.generator.generate(); environment.setContentLength(os.size()); os.writeTo(environment.getOutputStream(0)); If I'm not mistaken, this would avoid allocating and copying an array as large as the serializer's output. Or am I missing something subtle here? Ugo