PDA

View Full Version : Flush the Buffer



davide101
Thu 26th Mar '09, 11:14am
I was reviewing the Yahoo Performance blog and came across this interesting post:


Flush the Buffer Early

When users request a page, it can take anywhere from 200 to 500ms for the backend server to stitch together the HTML page. During this time, the browser is idle as it waits for the data to arrive. In PHP you have the function flush() (http://php.net/flush). It 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:

... <!-- css, js -->
</head>
<?php flush(); ?>
<body>
... <!-- content -->

Is this something that vB does or has investigated?

http://developer.yahoo.com/performance/rules.html

Simetrical
Fri 27th Mar '09, 5:43am
I'm pretty sure this would require some refactoring. Currently, it seems like vB scripts end with eval('print_output("' . fetch_template('SOMETEMPLATE') . '");');, with <head> and everything else only being output at the very end, so there's no point where there's been some output but there's still further computaton to do where you could flush. It would probably require some thought to find such a point, and I don't know how early in the script it could be.

You can't output the <title> until you know there are no more possible errors, for instance, as long as errors change the <title>. You might be able to push out some <style>/<link> tags (even if their contents end up going unused), but of course <script>s are supposed to go at the end if possible, so you can't do this with them.

It's interesting to think about, though.