Advertisement:

Author Topic: Automatically reduce bandwidth consumption (slightly)  (Read 35856 times)

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Automatically reduce bandwidth consumption (slightly)
« on: March 27, 2005, 05:24:38 PM »
In Sources/Subs.php, find this:
Code: [Select]
// Start up the session URL fixer.
ob_start('ob_sessrewrite');

Add below it:
Code: [Select]
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]
« Last Edit: April 01, 2005, 01:53:46 AM by [Unknown] »

Offline Dem0n

  • Full Member
  • ***
  • Posts: 520
  • Gender: Male
  • Word Lyfe!
    • TheTVStop
Re: Automatically reduce bandwidth consumption (slightly)
« Reply #1 on: March 27, 2005, 05:41:45 PM »
Very nice, thanks for sharing.

Offline Elijah Bliss

  • Sophist Member
  • *****
  • Posts: 1,340
Re: Automatically reduce bandwidth consumption (slightly)
« Reply #2 on: March 27, 2005, 06:02:40 PM »
<table width="100%" cellpadding=0 cellspacing=0 border=0>

-[Unknown]

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

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re: Automatically reduce bandwidth consumption (slightly)
« Reply #3 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]

Offline Thantos

  • SMF Friend
  • SMF Super Hero
  • *
  • Posts: 16,163
  • Gender: Male
Re: Automatically reduce bandwidth consumption (slightly)
« Reply #4 on: March 27, 2005, 10:31:43 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:
Code: [Select]
<!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

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re: Automatically reduce bandwidth consumption (slightly)
« Reply #5 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]

Offline Thantos

  • SMF Friend
  • SMF Super Hero
  • *
  • Posts: 16,163
  • Gender: Male
Re: Automatically reduce bandwidth consumption (slightly)
« Reply #6 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 ;)

Offline DoctorToxn

  • Semi-Newbie
  • *
  • Posts: 56
    • Dtronix
Re: Automatically reduce bandwidth consumption (slightly)
« Reply #7 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

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re: Automatically reduce bandwidth consumption (slightly)
« Reply #8 on: March 28, 2005, 12:10:35 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]

Offline DeadMetal

  • Jr. Member
  • **
  • Posts: 122
  • Gender: Male
Re: Automatically reduce bandwidth consumption (slightly)
« Reply #9 on: March 31, 2005, 11:02:33 AM »
How about including this into SMF by default, and adding a setting to activate it?

Offline Tristan Perry

  • SMF Hero
  • ******
  • Posts: 2,498
  • Gender: Male
    • Tristan Perry
Re: Automatically reduce bandwidth consumption (slightly)
« Reply #10 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?
« Last Edit: March 31, 2005, 12:13:03 PM by Tau Online »

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re: Automatically reduce bandwidth consumption (slightly)
« Reply #11 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:

Code: [Select]
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]
« Last Edit: April 01, 2005, 02:03:51 AM by [Unknown] »

Offline Radianation

  • Sophist Member
  • *****
  • Posts: 1,269
  • Gender: Male
  • Why not believe in me?
Re: Automatically reduce bandwidth consumption (slightly)
« Reply #12 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...

Offline Tristan Perry

  • SMF Hero
  • ******
  • Posts: 2,498
  • Gender: Male
    • Tristan Perry
Re: Automatically reduce bandwidth consumption (slightly)
« Reply #13 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..

Offline joker

  • Semi-Newbie
  • *
  • Posts: 96
  • Gender: Male
Re: Automatically reduce bandwidth consumption (slightly)
« Reply #14 on: April 20, 2005, 02:44:24 AM »
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 ;-)

Offline Dannii

  • SMF Friend
  • SMF Super Hero
  • *
  • Posts: 10,211
  • Mind the volcano!
    • My homepage
Re: Automatically reduce bandwidth consumption (slightly)
« Reply #15 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?
"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."

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re: Automatically reduce bandwidth consumption (slightly)
« Reply #16 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]

Offline Dannii

  • SMF Friend
  • SMF Super Hero
  • *
  • Posts: 10,211
  • Mind the volcano!
    • My homepage
Re: Automatically reduce bandwidth consumption (slightly)
« Reply #17 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.
"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."

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re: Automatically reduce bandwidth consumption (slightly)
« Reply #18 on: July 25, 2005, 11:42:48 PM »
This was mostly just for example, not really for general use.

-[Unknown]

Offline xcooling

  • Semi-Newbie
  • *
  • Posts: 33
  • Gender: Male
    • AmericasArmy South Africa
Re: Automatically reduce bandwidth consumption (slightly)
« Reply #19 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 ?