cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bruno Dumon <br...@outerthought.org>
Subject Re: Unnecessary array allocation and copying
Date Mon, 26 Apr 2004 21:19:59 GMT
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
bruno@outerthought.org                          bruno@apache.org


Mime
View raw message