PDA

View Full Version : Save alittle bandwidth and load time on each page


Andy
19-11-08, 19:10
This mod is not for the faint hearted.

It's a simple mod but in a very delicate place. If you don't really need to save a few kb per page, then don't bother.

But if bandwidth is at a premium and you need to save as much as possible, then this mod might help.

Look at the source code of your page output (view->source)

Those line breaks are not empty, they are actual characters kicked out by your server:
on a Linux server: \n
on a windows server: \r
on a mac: \r\n

So, if we can get rid of those line breaks, we can obviously save some bandwidth. Probably not alot, but it all adds up.

Here's the mod:


open: smarty/libs/Smarty.class.php

in the unedited file, on (or around) line 1296, you should see this code:
if ($display) {
if (isset($_smarty_results)) { echo $_smarty_results; }
if ($this->debugging) {
// capture time for debugging info
$_params = array();
require_once(SMARTY_CORE_DIR . 'core.get_microtime.php');
$this->_smarty_debug_info[$_included_tpls_idx]['exec_time'] = (smarty_core_get_microtime($_params, $this) - $_debug_start_time);
require_once(SMARTY_CORE_DIR . 'core.display_debug_console.php');
echo smarty_core_display_debug_console($_params, $this);
}
error_reporting($_smarty_old_error_level);
return;
} else {
error_reporting($_smarty_old_error_level);
if (isset($_smarty_results)) { return $_smarty_results; }
}We only need to make one small edit to this, and it's on the second line of the above.

Change:if (isset($_smarty_results)) { echo $_smarty_results; }To: if (isset($_smarty_results)) { echo preg_replace("/\n|\r\n|\r$/", "", $_smarty_results); }
So whats it doing?

preg_replace is just looking for any \n,\r or \r\n and replacing it with nothing. simple eh?

How much do you save?

my tests on /productlist.php?rb=4-5

without mod: 35,107 bytes (34.284Kb)
with mod: 33,032 bytes (32.257Kb)
Saving: 2075 bytes (2.026Kb)

1000 page views on /productlist.php?rb=4-5 with the Mod would save me 1.978Mb (2026Kb - 2074624bytes)



Disclaimer:
Ive tested this mod for performance on my server with my code but not on your server with your code.
I have now removed my mod until Im finished developing my site. This mod removes all newlines from the source html, which makes it difficult to read.
I have my capabilities, not your capabilities.
If something goes wrong, I am not to blame, you are.
Save EVERYTHING. backup EVERYTHING. Blame NO-ONE.

Page size tests done here: http://www.prioritysubmit.com/seotools-page-size-extractor.html

If you use this mod, run some tests at the link above and post your results.

If you find a bug in this code, please post below. Similarly, if you know of an improvement, post below.

Andy :D

IntroSites
19-11-08, 19:20
Ive used a similar mod to achieve the same on my V1 SW for many many months without problem

Good Mod - Thanks for sharing

Amoochi
19-11-08, 23:06
Nice one, thanks for sharing this. :)

And definately not wanting to be detracting from your efforts, but this is the sort of thing that most definately should be part of the package by default. Just as it should all be spider friendly links by default.

Andy
17-02-09, 15:31
Just seen these replies.

If the mod was added by default, then no-one could read their own source code. It would require some sort of config switch to turn it on and of.

Mod rewrite by default is an "OK" idea, but it's not exactly competitive to have everyone with the same style of mod rewrite. And whos to say what is the best style of modrewrite? Unless it was OFF by default and configurable using a wordpress style system (which would require a database)
Im quite glad its not a default option tbh, gives me a reason to hack these files up!