News for hackers. Stuff that matters.
Re:PHP's ob_start() pre-allocates 40k memory per call by ck2 on Thursday December 08, 2011 @03:39AM (#3328290)
I always treated output buffering/capturing like eval

- if you have to use it, you need to question yourself carefully as to "why" and if there is a better way.

Re:PHP's ob_start() pre-allocates 40k memory per call by forgotusername on Thursday December 08, 2011 @04:56AM (#3328471)
As any European user of HN knows too well, when you send tiny chunks separated by a few ms a time up to TCP, the majority of the planet will get a jittery half-rendered experience of your home page lasting seconds for a handful of actual kb downloaded.

The better question might be "if you want to turn it off, the question is why", or something.

Re:PHP's ob_start() pre-allocates 40k memory per call by ars on Thursday December 08, 2011 @05:18AM (#3328519)
Using one ob_start() could make sense for certain cases. But 16 of them? I can't think of any reason to nest ob_start 16 levels deep.
Re:PHP's ob_start() pre-allocates 40k memory per call by nawariata on Thursday December 08, 2011 @08:29AM (#3328958)
This is common approach in MVC frameworks, where every view (template/partial/widget) is rendered independently and then injected into main template.
Re:PHP's ob_start() pre-allocates 40k memory per call by Jach on Thursday December 08, 2011 @06:07AM (#3328644)
Me neither, I'd be interested in the use case for that many. A few years ago I made a PHP-based template system that had one nested buffer to have a poor-man's eval of template (HTML with display-logic PHP) files, it just boiled down to:

    ob_start();
    include $file;
    $contents = ob_get_contents();
    ob_end_clean();
Re:PHP's ob_start() pre-allocates 40k memory per call by lox on Thursday December 08, 2011 @12:04PM (#3329906)
Yup, this is common. If you put this in a function and use extract you can create a clean working space for a template where you can use $vars without worrying about polluting scope.

I'd assume the 16x calls would be because they have a very complicated set of templates/layouts/partials, all doing their own templating.

Re:PHP's ob_start() pre-allocates 40k memory per call by Jach on Thursday December 08, 2011 @12:24PM (#3330013)
That's actually exactly what I was doing in the full context.

I can see 16 calls over the lifetime of the request, but what was confusing me was how they could have 16 nested calls forming some ob_start();ob_start();...ob_end_flush(); Maybe some crazy way to avoid having to return text up the tree through function returns. But I read the post again and it said nothing about nesting them, so it's probably just something like 16 sub-templates or whatever as you suggest being processed at different intervals, not necessarily nested, with the gc not collecting frequently enough.

Re:PHP's ob_start() pre-allocates 40k memory per call by ars on Thursday December 08, 2011 @04:52PM (#3331330)
> with the gc not collecting frequently enough.

PHP is written in C which is not garbage collected.

PHP itself collects garbage, but this buffer is an internal C buffer and would be released immediately.

Re:PHP's ob_start() pre-allocates 40k memory per call by Jach on Thursday December 08, 2011 @08:00PM (#3331984)
...Yeah, not sure what I was thinking with that one, thanks for replying. (My excuse! I've been up over 24 hours already...)
Re:PHP's ob_start() pre-allocates 40k memory per call by masklinn on Thursday December 08, 2011 @05:49AM (#3328605)
I'm thinking various components, each doing its own ob_start() for its output, something like that.
Re:PHP's ob_start() pre-allocates 40k memory per call by ecaron on Thursday December 08, 2011 @08:54AM (#3329037)
I feel like most people use this as a stop-gap for doing gzipping content to reduce bandwidth, but hopefully more people relying on nginx/apache to do the compression delivery will reduce the circa-2004 PHP mentality that started the practice.

Oh Steve Souders, your quest for speed will doom us all:)