Simple Machines Community Forum

Customizing SMF => Tips and Tricks => Topic started by: [Unknown] on March 27, 2005, 05:24:38 PM

Title: Automatically reduce bandwidth consumption (slightly)
Post by: [Unknown] on March 27, 2005, 05:24:38 PM
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]
Title: Re: Automatically reduce bandwidth consumption (slightly)
Post by: Dem0n on March 27, 2005, 05:41:45 PM
Very nice, thanks for sharing.
Title: Re: Automatically reduce bandwidth consumption (slightly)
Post by: Elijah Bliss on March 27, 2005, 06:02:40 PM
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?
Title: Re: Automatically reduce bandwidth consumption (slightly)
Post by: [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]
Title: Re: Automatically reduce bandwidth consumption (slightly)
Post by: Thantos on March 27, 2005, 10:31:43 PM
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
Title: Re: Automatically reduce bandwidth consumption (slightly)
Post by: [Unknown] on March 27, 2005, 11:21:18 PM
I've been pointed to where in the spec it says it's okay, though, I thought...

Must be remembering wrong, then...

-[Unknown]
Title: Re: Automatically reduce bandwidth consumption (slightly)
Post by: Thantos on March 27, 2005, 11:36:53 PM
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 ;)
Title: Re: Automatically reduce bandwidth consumption (slightly)
Post by: DoctorToxn 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
Title: Re: Automatically reduce bandwidth consumption (slightly)
Post by: [Unknown] on March 28, 2005, 12:10:35 AM
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]
Title: Re: Automatically reduce bandwidth consumption (slightly)
Post by: DeadMetal on March 31, 2005, 11:02:33 AM
How about including this into SMF by default, and adding a setting to activate it?
Title: Re: Automatically reduce bandwidth consumption (slightly)
Post by: Tristan Perry on March 31, 2005, 12:08:37 PM
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?
Title: Re: Automatically reduce bandwidth consumption (slightly)
Post by: [Unknown] on April 01, 2005, 01:52:42 AM
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]
Title: Re: Automatically reduce bandwidth consumption (slightly)
Post by: NoRad on April 01, 2005, 02:03:58 AM
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...
Title: Re: Automatically reduce bandwidth consumption (slightly)
Post by: Tristan Perry 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..
Title: Re: Automatically reduce bandwidth consumption (slightly)
Post by: joker on April 20, 2005, 02:44:24 AM
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.
Title: Re: Automatically reduce bandwidth consumption (slightly)
Post by: Dannii on July 22, 2005, 11:28:43 PM
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?
Title: Re: Automatically reduce bandwidth consumption (slightly)
Post by: [Unknown] on July 22, 2005, 11:31:15 PM
Because it is XHTML, this will never be the case.  But, this shouldn't cause that.

-[Unknown]
Title: Re: Automatically reduce bandwidth consumption (slightly)
Post by: Dannii on July 25, 2005, 02:47:55 AM
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.
Title: Re: Automatically reduce bandwidth consumption (slightly)
Post by: [Unknown] on July 25, 2005, 11:42:48 PM
This was mostly just for example, not really for general use.

-[Unknown]
Title: Re: Automatically reduce bandwidth consumption (slightly)
Post by: xcooling on November 21, 2005, 05:06:01 AM
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 ?
Title: Re: Automatically reduce bandwidth consumption (slightly)
Post by: boogiedown on January 29, 2006, 06:46:39 PM
does this work with 1.1 rc2?