Forum Moderators: coopster

Message Too Old, No Replies

template theory and RAM usage

can anyone clarify

         

jamie

8:43 pm on Nov 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



i just found this by brett about the mods to this board from june 2002, [webmasterworld.com...]

" Removed "template" page generation in favor of inline on-the-fly generation. This reduced amount of memory used per page view by up to 2 megabytes. Not all that important at 10k views a day, but at 100k views, it's critical. "

can anyone explain what the above means? does it mean echoing out data as it is processed instead of storing and spitting out a template?

thanks!

jollymcfats

9:49 pm on Nov 28, 2004 (gmt 0)

10+ Year Member



Brett would know for sure, but I intepret that as meaning that previously layout was controlled by a homegrown template system: i.e. loaded from disk, parsed for variable substitutions and control statements, then those substitutions and control logic were executed by the template system and sent to the browser.

Then the "inline" approach I intepret as embedding the HTML markup and control logic in the "native" site code itself, rather than in a template system. Skipping all the above steps except for the actual execution and output. Possibly echoing it as processing occurs, but not necessarily.

The memory savings is most likely chalked up to skipping the string parsing & execution step. A scripting interpreter (PHP, Perl) is pretty fast and efficient at loading source code and parsing it; implementing an intepreter in an intepreter is not. That's what most template systems do.

For completeness (the subject does read "template theory" :), there is a hybrid approach that combines the above two: pre-compiling a template into executable code. JSP does this (each template page is compiled into a Servlet), some PHP template add-ons do as well I believe.

IanKelley

6:25 am on Nov 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Without reading the post you linked to... The two simplest ways to save memory on page generation are:

1) Don't store or load big chunks of HTML code into variables, instead put them directly into the PHP page or echo them directly from a file.

If you need to parse the HTML for something, do it line by line echoing as you go if possible (as opposed to slurping the entire file into an array or variable).

Ideally there will be no need to parse HTML code because you can switch to PHP mode to display variables.

2) If you have to hold a large chunk of data in a variable, do it for the shortest span of time possible.

That will create faster, less memory intensive, scripts. A lot of people will argue, however, that easy of use, extensibility, readability, etc.. are more important.

There's something to be said for all of that, but as you say, at 100k+ daily page views philosophical programming standards start to seem kinda silly :-)

jamie

8:35 am on Nov 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi both of you, very interesting.

>> That will create faster, less memory intensive, scripts. A lot of people will argue, however, that easy of use, extensibility, readability, etc.. are more important.

i thought as much.

...and because it's a forum, brett can't cache output to disk either - which otherwise might solve some of the ram usage and maintain the usability mentioned above.

many thanks!

mincklerstraat

8:38 am on Nov 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To add to jollymcfats's and IanKelly's excellent thoughts on templating, there's been quite a debate in the PHP community about whether to template or whether not to template. Some, who have a great concern for speed and process use optimization, say that PHP already is a template in some sense - the original intentions of PHP were indeed very template-like, and PHP's original author had no idea PHP would end up being used for all the groovy stuff people use it for today. And that templating 'languages' end up introducing a syntax which, if you are going to end up doing anything really fun, rivals PHP in difficulty, so it's not really going to be making your designers' lives any easier.

Templates are really useful on projects which are big enough that you want your system itself to have security controls for the work that people on the project are doing - e.g., you want your designers to be able to input HTML, and it's not enough to politely ask them to refrain from doing anything except populating some variables.

I'll probably be going down the template route soon enough with one project since that's what I need - even though I rather hate that idea of a whole extra layer of complexity, both for my coding, and for my processor. If php only had some way of natively including files, but specifying in the include statement, 'and don't let the included file do anything except set a few variables' this would already do the trick for me and I wouldn't need to be thinking about this whole extra layer. I suppose I could also use something like

parse_ini_file()
but that, again, is straying from the 'native' format. It'd probably be a whole lot faster than a non-caching template system, but I'm guessing then that a lot of designers would be put off with it, whereas if I use smarty templates, they'll think, 'oh, smarty templates, I need to learn those some time,' and I'm counting on designers when it comes to promoting this thing and getting it to catch on.

So if it's just you and a very small group of people you can really trust who are working on the site, and it's likely to stay this way, don't worry about templates; if you later want to allow people you trust less to be able to add or change the HTML styling without needing to check out their work, go templates.

BTW: in July, John Lim updated his article a HOW TO on optimizing PHP [phplens.com]. I've only just come across the new version. He says, amongst other things, that

$var = "this $kind of $doing vars";
no longer has any significant speed advantage over
$var = 'this '.$kind.' of '.$doing.' vars';
since 4.3.x, though I'll probably stick to the latter for code clarity and old rusty habit. He also points out the surprisingly large speed benefits of using
ob_start()
(not even with
ob_gz_handler()
) and why just using a buffer is even faster than either spitting out output line by line, or concatenating all content into one long $content variable. This is probably the best PHP speed-optimization article I've come across.

PS: If I've just overlooked something, and there is a way of natively letting an included file include some variables, but not execute any code, please let me know!

jamie

2:38 pm on Nov 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



mincklerstraat, that's a great post.

john lim's article is one of my favourites on php - i'll have to check out the new version.

i actually use a template system with my site. using eval() and variable substition like {$this}.

the reason i found brett's post so interesting was that i am always looking for ways however to lighten the load, cos we max out our server in our peak season. however as i realised above, brett can't afford to cache any output, we can though - i've just been playing with cache_lite and the scalability improvement is phenomenal.

thanks!

<added>

i've just read some of the changes in the article.

is there a limit on using output buffers during the generation of one page? can one use as many as 10, or even more?

cheers

jollymcfats

4:35 pm on Nov 29, 2004 (gmt 0)

10+ Year Member



In this thread [webmasterworld.com] about forum performance, Brett suggests that much output here is cached to flat files. Caching to the filesystem is definitely the way to go if you can do it. Serving those cached files from Apache is even better, and serving them from Tux better yet.

Regarding variable-substituting templates, they can be sped up with the "compilation" template style- use Perl or whatnot to transform

{$var}
to
<?= $var?>
. But keep the eval() code so that you can test your templates directly without a compilation step during development. The templates can be batch-compiled before you send the code off to production.

jamie

5:08 pm on Nov 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



jollymcfats,

that's a really interesting post you refer to - (mostly chinese to me of course ;-)

re: the variable substitution.

i have already been looking at other ways to do this to, and one was for every instance of a template, to use output buffering instead of eval():

ob_start()
include ('html_template.inc');
$data = ob_get_contents();
ob_end_clean();

the templates then become a standard mixture of <?php echo $template_var;?> and html code, like you suggest.

that was the reason i asked about the amount of output buffers per page - could be as many as 8 templates / buffers per page (for formatting the content, sidebars, adds, news articles, etc).

i have not tested the speed differences over eval() yet, but in your opinion does this sound like a more scalable use of templates.

many thanks!

jollymcfats

5:14 pm on Nov 29, 2004 (gmt 0)

10+ Year Member



ob_start()
include ('html_template.inc');
$data = ob_get_contents();
ob_end_clean();

This is pretty much exactly what I do, with the same usage pattern you're envisioning for your site. No problems whatsoever. Plus I have some templates that include other templates in the same way, so there are some nested ob_starts() in addition. Still no problems.

jamie

6:52 pm on Nov 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



that's great news!

thanks very much for the advice.

freeflight2

7:11 pm on Nov 29, 2004 (gmt 0)

10+ Year Member



don't waste your time over optimizing things - focus on writing well written software and templates/language files are (currently) the nicest way to do it - use MMCache or an other php compiler / shared memory cache - makes here 1M+ highly dynamic templated page views for phpbb, gallery etc. per Dual Xeon box.
By using caches in front of web servers it's possible to generate 2-3M+ page views/server per day while still delivering highly dynamic pages to the user.

High memory usage is often related to something completely different - most high traffic sites are usually disk IO bound: php needs data from disk, disk is too slow => more and more processes pile up and consume more and more RAM.

jamie

6:21 am on Nov 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi freeflight,

>> most high traffic sites are usually disk IO bound

what does this mean? that the bottleneck on high traffic sites is the disk seek speed?

cheers

Timotheos

8:14 am on Nov 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A while ago I started on a new project and decided I needed to use a template system. So I sat down and starting reading the Smarty docs since it's so popular. Good documentation on it but by the time I got done I was wondering why on earth would I want to learn another syntax just to create a template. So I did a little searching and turned out that I asked the right question. There's much to say about templating systems but I finally ended up using Savant [wiki.ciaweb.net] and I think it's great. Their's an article here [phppatterns.com] by Harry Fueck's that got me going in this direction.

Tim

jamie

8:42 am on Nov 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



interesting article tim, especially the heated comments ;-)

we are lucky enough to just have trusted designers working on the html, so the use of <?php?> tags in included html files works fine. a further layer of security such as provided by smarty is not necessary - i also really balk at the idea of learning the smarty syntax!

something i read regularly is that one shouldn't really bother over-optimising (just like freeflight2 said), but that the real bottleneck on dynamic sites is the database - outpout caching is an order of magnitude quicker and more scalable than any code optimisation.

my favourite analogy (read on some blog somewhere) likened most php code optimisation and decisions such as whether to use foreach() or while() to building a bridge and trying to decide whether to use milwaukee or stanley tools ... lol

mincklerstraat

9:35 am on Nov 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, freeflight2 is very right about over-optimization; many types of optimization are a thing of the past, and using single quotes instead of double quotes for optimization purposes has apparently become one of them with 4.3. It's the basics that are to be kept in mind - doing any calculations inside loops that only need to be calculated once? Are you making your code into one huge file to do everything, instead of making the code used for the majority of your pageviews nice, trim, and memory-friendly, with those oddball occasionally-used page functions in separate files? As far as PHP coding goes, for me it's one of the values to be considered, but not always close to the top. For files which are used in the production of most actual page views, optimization is a big concern - for admin functions, I don't mind if I go all out creating lots of functions which are only used once, or big classes which keep the logic clear, though they could probably be cleaned up a lot in terms of memory usage, or huge long files that do everything adminwise. For pages I can cache a fairly long time, I don't worry so much; for search results, I worry a lot.

I like the idea of having all HTML output for normal pageviews (not including admin or forms) being at least nicely localized in a separate file or directory, so it's all there in one place to be easily changed. This doesn't necessarily mean all the extras of a whole extra templating layer.

Savant looks interesting indeed; this is the kind of thing I do with many of my scripts, which I guess is like having an ad hoc templating system, a sort of informal convention which keeps your HTML in separate pages away from your program's 'business logic.' The kicker is, though, you'll have to look through each of these templates yourself if you don't have a design team you implicitly trust - you won't be able to blindly allow company x to have whoever they want to modify the templates, unless you're willing to deal with the possible consequences - errors due to neglect or purpose, stolen code, etc. A truly separate layer of interpreted template code will do a lot more to help you seal up your ap security-wise if you have to play with other parties whose skill or will is an undetermined quantity. It's time concerns here - does this situation happen often enough that you're willing to deal with the consequences? If it's a script just used by three or four sites, probably not worth going to the hassle of adding that whole extra layer - if it's something you're using on a broader scale, then it's probably worth it.

IanKelley

8:10 pm on Nov 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



and using single quotes instead of double quotes for optimization purposes has apparently become one of them with 4.3

I don't see how that's possible. Regardless of how the PHP engine has changed, single quotes still means don't parse for variable substitution.

It might not be a huge speed difference but there has to be a difference.

Code Sentinel

8:46 pm on Nov 30, 2004 (gmt 0)

10+ Year Member



I like the single quotes with concats simply because of the tag coloring in dreamweaver or any other editor which support php tags.. makes for easier reading.

I agree with the over-optimization, you get to a point where it doesn't really matter UNTIL you get some huge surge of traffic. When that time comes it may be cheaper to throw more hardware at the problem or you can just use a simple caching script(server side or client side or both).