Automatically reduce bandwidth consumption (slightly)

Started by [Unknown], March 27, 2005, 05:24:38 PM

Previous topic - Next topic

[Unknown]

In Sources/Subs.php, find this:
// Start up the session URL fixer.
ob_start('ob_sessrewrite');


Add below it:

function ob_reduce_bandwidth($buffer)
{
// We can't run on 4.1.x because PHP will crash sometimes.
if (@version_compare(PHP_VERSION, '4.2.0') == -1 || isset($_GET['debug']))
return $buffer;

$css = create_function('$x', 'return preg_replace(\'~([a-z_\-]+?):\s*(\d[^;]+?);~s\', \'$1:$2;\', $x);');

$buffer = preg_replace('~([\t \r\n])+~s', '$1', $buffer);
$buffer = preg_replace('~style="(.+?)"~se', '\'style="\' . $css("$1") . \'"\'', $buffer);
$buffer = strtr($buffer, array('" />' => '"/>'));

return $buffer;
}
ob_start('ob_reduce_bandwidth');


Now, this isn't perfect - more could be done, like changing:

<table width="100%" cellpadding="0" cellspacing="0" border="0">

To:

<table width="100%" cellpadding=0 cellspacing=0 border=0>

(my mistake, always thought it shouldn't be allowed so that's what I get for being wrong.)

But it's a start.  It's still not a large change (in bytes), though, mind you... but it could save a kilobyte, maybe, per page view.

-[Unknown]


Elijah Bliss

Quote from: [Unknown] on March 27, 2005, 05:24:38 PM
<table width="100%" cellpadding=0 cellspacing=0 border=0>

-[Unknown]

Can't you run in to rendering problems if quotations are absent?

[Unknown]

No... quotations around numeric values are, technically, not required... and I've never seen a browser cough at them being missing.

Still, imho it just looks better and makes more sense to quote all attributes.

-[Unknown]

Thantos

Quote from: [Unknown] on March 27, 2005, 06:05:41 PM
No... quotations around numeric values are, technically, not required... and I've never seen a browser cough at them being missing.

Still, imho it just looks better and makes more sense to quote all attributes.

-[Unknown]
Actually standard's speaking they are required.

Example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><head>
<title>test</title>
</head>
<body>
<table border=1 width=100% cellpadding=0 cellspacing=0>
<tr>
<td>Hello</td>
</tr>
</table>
</body>
</html>

Produced 5 errors all of "an attribute value specification must be an attribute value literal unless SHORTTAG YES is specified"
Putting quotes around the 100% removed 2 and putting quotes around the other 3 removed the rest.
All of this tested with W3C's checker

[Unknown]

I've been pointed to where in the spec it says it's okay, though, I thought...

Must be remembering wrong, then...

-[Unknown]

Thantos

IIRC its ok in HTML just not in XHTML.  I'd have to go review the spec to be sure though.  I've just been bitten by that error enough time to know it's there ;)

DoctorToxn

I really don't see how removing all the " will help in reducing the bandwidth.  Ok maybe a little but, is it worth saving like 0.01 seconds?

BuilderDan

[Unknown]

Quote from: BuilderDan on March 28, 2005, 12:04:22 AM
I really don't see how removing all the " will help in reducing the bandwidth.  Ok maybe a little but, is it worth saving like 0.01 seconds?

BuilderDan

Gigabytes, not seconds - we're measuring consumption (server cost) here not speed.

-[Unknown]

DeadMetal

How about including this into SMF by default, and adding a setting to activate it?

Tristan Perry

#10
10.29 KB (10533 bytes) -> 9.96 KB (10202 bytes)

Small change, but should still help, thanks  :) As DeadMetal says, why not include this in SMF by default?

EDIT: I've seen many sites 'scrunch' up the source code like this, how does it reduce the file size? I thought that the file size was the amount of characters and whitespace wasn't included?

[Unknown]

#11
No, whitespace definately matters.  And 300 bytes per request may be near nothing, but... it is 3%.

Meaning, this could decrease your bandwidth by 3%.  If we used it here, that'd mean a decrease of like a gig or so.

I dunno, personally I really dislike it when the HTML's formatting is screwy.  Anyway, for a tiny bit more:


function ob_reduce_bandwidth($buffer)
{
global $settings;

// We can't run on 4.1.x because PHP will crash sometimes.
if (@version_compare(PHP_VERSION, '4.2.0') == -1 || isset($_GET['debug']))
return $buffer;

// This basically changes "padding: 2ex;" to the uglier "padding:2ex"...
$css = create_function('$x', 'return preg_replace(array(\'~([a-z_\-]+?):\s*([\d#][^;]+?);~s\', \'~;$~\'), array(\'$1:$2;\', \'\'), $x);');

// Take off the commenting of JavaScript/CSS code....
$buffer = strtr($buffer, array('<script language="JavaScript" type="text/javascript"><!--' => '<script language="JavaScript" type="text/javascript">', '// --></script>' => '</script>', '<style type="text/css"><!--' => '<style type="text/css">', '--></style>' => '</style>'));
// Remove any JavaScript comments, if used.
$buffer = preg_replace('~^[\t ]+//.+?$~s', ' ', $buffer);
// Remove any whitespace and strip it down...
$buffer = preg_replace('~([\t \r\n])+~s', '$1', $buffer);
// Match any style attributes and clean them up.
$buffer = preg_replace('~style="(.+?)"~se', '\'style="\' . $css("$1") . \'"\'', $buffer);
// Remove the space at the end of empty tags...
$buffer = strtr($buffer, array('" />' => '"/>', '<br />' => '<br/>'));

// You only need this for the Classic theme anyway.
if ($settings['theme_id'] != 2)
$buffer = strtr($buffer, array(' class="check"' => ''));

return $buffer;
}
ob_start('ob_reduce_bandwidth');


-[Unknown]

NoRad

I think most forum users would benefit more from optimizing their header images and/or reducing quality of user uploaded avatars. Unless everybody cache's the images. Nevermind...

Tristan Perry

I applied this to my forums, although I've recently found out that this removes all excess blank lines from a post when editing it.. This bug happens with both codes you posted Unknown..

joker

Quote from: Tau Online on April 01, 2005, 12:44:36 PM
I applied this to my forums, although I've recently found out that this removes all excess blank lines from a post when editing it.. This bug happens with both codes you posted Unknown..
Even worth: No users can register! The 'register-button keeps being grey colored. Some javascript error is shown. After removing the mod it became black again.
joker ;-)

Dannii

Interesting...

Quote"an attribute value specification must be an attribute value literal unless SHORTTAG YES is specified"
How do we specify SHORTTAG YES? If that was specified it would still be standards-compliant, and much could be saved by removing unneeded quotes, yes?
"Never imagine yourself not to be otherwise than what it might appear to others that what you were or might have been was not otherwise than what you had been would have appeared to them to be otherwise."

[Unknown]

Because it is XHTML, this will never be the case.  But, this shouldn't cause that.

-[Unknown]

Dannii

I tried it and it did reduce bandwidth by around 3% which is pretty nice, except I've removed it now because it no longer validates. I think that's because it did something nasty to the comments.
"Never imagine yourself not to be otherwise than what it might appear to others that what you were or might have been was not otherwise than what you had been would have appeared to them to be otherwise."

[Unknown]

This was mostly just for example, not really for general use.

-[Unknown]

xcooling

Any updates on this ?
Like making  it XHTML compatible, also has this been included in 1.1rc 1 ?

Also did you test the consumption with mod_gzip / zlib enabled ?

Advertisement: