[stringtemplate-interest] huge memory improvement for sites using ST
Terence Parr
parrt at cs.usfca.edu
Thu Oct 5 10:16:26 PDT 2006
Hi, my former business partner, Tom Burns, is building
www.schoolloop.com and just discovered that having ST write straight
to the output stream is much better than writing to a string and then
writing out. He says:
> http://schoolloop.com gets about 1 million page views a day. We were
> having some performance issues, so I was looking around for some
> "big win" improvements. I definitely found one.
>
> SchoolLoop uses StringTemplate for all of its page generation. I made
> a simple change to the core method -- generatePage -- and the sites
> behavior at peak times changed drastically. We went from having about
> 300M out of 3G free to having about 2G out of 3G free. Obviously,
> this is
> just one sample, but it gives you a good idea of the change in
> behavior we
> have observed. The time between full garbage collections went from
> about
> 20 minutes to about 5 or 6 hours.
>
> All I did was change this:
>
> public void generatePage(PrintWriter pageOut) throws Exception
> {
> StringTemplate page = generatePage();
> pageOut.write(page.toString());
> }
>
>
> to this:
>
> public void generatePage(PrintWriter pageOut) throws Exception
> {
> StringTemplate page = generatePage();
> NoIndentWriter writer = new NoIndentWriter(pageOut);
> page.write(writer);
> }
>
> The problem with toString() is that it uses a StringBuffer to store
> the intermediate strings -- and it has no idea how big the buffer
> will ultimately be. So it makes very bad guesses in the worst case
> (on
> schoolloop, some pages are over 500k) and it has to iteratively
> allocate
> new, larger buffers and copy the existing buffer to the new one.
>
> Thomas E. Burns
> Co-Founder/CTO, Schoolloop.com
More information about the stringtemplate-interest
mailing list