Flush the Buffer Early - speed tip

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
JeremyBASS

Flush the Buffer Early - speed tip

Post 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
User avatar
Gregor
Power Poster
Power Poster
Posts: 1874
Joined: Thu Mar 23, 2006 9:25 am

Re: Flush the Buffer Early - speed tip

Post 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
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: Flush the Buffer Early - speed tip

Post by calguy1000 »

The UDT should just consist of:

Code: Select all

flush();
one line.... nothing else.... that's all you need.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: Flush the Buffer Early - speed tip

Post 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.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
User avatar
Gregor
Power Poster
Power Poster
Posts: 1874
Joined: Thu Mar 23, 2006 9:25 am

Re: Flush the Buffer Early - speed tip

Post by Gregor »

Thanks for your respond Calguy. I'll give it a little check.
JeremyBASS

Re: Flush the Buffer Early - speed tip

Post 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
Post Reply

Return to “Tips and Tricks”