Page 1 of 1

Flush the Buffer Early - speed tip

Posted: Sat Aug 02, 2008 9:34 pm
by JeremyBASS
A users request a page, which can take anywhere from 200 to 500ms for the backend server to stitch the HTML page. The browser is idle during this time, waiting for the data. PHP has the function flush().  Yea of us :) function flush allows you to send your partially ready HTML response to the browser so that the browser can start fetching components while your backend is busy with the rest of the HTML page. The benefit is mainly seen on busy backends or light frontends.

A good place to consider flushing is right after the HEAD because the HTML for the head is usually easier to produce and it allows you to include any CSS and JavaScript files for the browser to start fetching in parallel while the backend is still processing.

Example:

      ...
   
   
   
      ...

Now for us all we have to do is make a UDT, lets say {flush}, containing,

Code: Select all

//needed air to present this
//

?><?php flush(); 

//
//

You could put this any where you want to but this is the best IMHO place to do it...

{flush}


As always, use at your on risk, though it should cause no harm, it's your site and your responsibility.

jeremyBass

Re: Flush the Buffer Early - speed tip

Posted: Tue Sep 16, 2008 9:39 pm
by Gregor
JeremyBass,

I think there is a small problem with the udt and the php code. This one errors when making a udt.

Gregor

Re: Flush the Buffer Early - speed tip

Posted: Tue Sep 16, 2008 9:41 pm
by calguy1000
The UDT should just consist of:

Code: Select all

flush();
one line.... nothing else.... that's all you need.

Re: Flush the Buffer Early - speed tip

Posted: Tue Sep 16, 2008 9:42 pm
by calguy1000
Though I don't think it'll have the effect that you're thinking.

CMS already does output buffering, and capturing of the buffers, etc.... this tag won't change that much.

Re: Flush the Buffer Early - speed tip

Posted: Tue Sep 16, 2008 10:23 pm
by Gregor
Thanks for your respond Calguy. I'll give it a little check.

Re: Flush the Buffer Early - speed tip

Posted: Thu Sep 18, 2008 12:21 am
by JeremyBASS
Gregor wrote: JeremyBass,

I think there is a small problem with the udt and the php code. This one errors when making a udt.

Gregor
Shoot there I forgot to get back on this... yes indeed  ?><?php  is not needed... it works in most cases but still not needed...

All this does is flush the header information... I needed to fight a host that was deadly slow... this is only one trick... I did see a difference in time but it was not a great gain ~300ms... but it was a gain... the full answer is much bigger as far as speeding up your site... I was going to write something up but just have not had the time yet.... but in short I took a site that was loading in about ~10secs down to ~1, reduced the size from ~500kb to ~250kb (dead download with gobs of pics and flash) freed CMSMS from the db, and got it where it is
was only making one 1-2 header requests on every pages with a cached load of ~8kb pre page... I'll be writing about it in a bit here

anyways... this is a very last nitty gritty step... and if it's already been place in the core script then this is not needed any more... hope that helps....
jeremyBass