<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Brainspl.at: Speed up page loads</title>
    <link>http://www.brainspl.at/articles/2006/12/29/speed-up-page-loads</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Brainspl.at</description>
    <item>
      <title>"Speed up page loads" by Garry</title>
      <description>Thanks for the tip Ezra.</description>
      <pubDate>Sat, 05 May 2007 12:09:19 +0000</pubDate>
      <guid>urn:uuid:834cbfac-40fb-48c7-9414-af19bf76b7a6</guid>
      <link>http://www.brainspl.at/articles/2006/12/29/speed-up-page-loads#comment-4329</link>
    </item>
    <item>
      <title>"Speed up page loads" by Bob</title>
      <description>Remember that 4 hosts instead of 1 means 4 DNS lookups instead of 1.  How many of us have been subjected to shitty DNS servers that take forever to resolve?  Not that this alone should stop you of course - it's still a great technique.  Just don't forget to factor it into to the balancing act that is web development.</description>
      <pubDate>Sat, 13 Jan 2007 01:19:37 +0000</pubDate>
      <guid>urn:uuid:fbc39c74-5f55-4512-94e4-924600b52f24</guid>
      <link>http://www.brainspl.at/articles/2006/12/29/speed-up-page-loads#comment-3887</link>
    </item>
    <item>
      <title>"Speed up page loads" by Kevin</title>
      <description>topfunky... nice, this should work out nicely.

:)</description>
      <pubDate>Mon, 08 Jan 2007 23:25:57 +0000</pubDate>
      <guid>urn:uuid:e17cb3db-726c-4b34-a478-6320739858d9</guid>
      <link>http://www.brainspl.at/articles/2006/12/29/speed-up-page-loads#comment-3881</link>
    </item>
    <item>
      <title>"Speed up page loads" by topfunky</title>
      <description>Mac OS X doesn't support wildcard subdomains easily, but I did this on /etc/localhost

&lt;pre&gt;
127.0.0.1 asset1.localhost asset2.localhost asset3.localhost
&lt;/pre&gt;

I also tweaked it a bit to add in the current port so it works in development.</description>
      <pubDate>Mon, 08 Jan 2007 21:25:14 +0000</pubDate>
      <guid>urn:uuid:4de508d9-5e1e-4590-b9bc-6909e5aa552f</guid>
      <link>http://www.brainspl.at/articles/2006/12/29/speed-up-page-loads#comment-3880</link>
    </item>
    <item>
      <title>"Speed up page loads" by Kevin</title>
      <description>Great idea, I was reading up on this about 3 mos ago on evolt I think?!!?   My question is this, how can you set this up to work in a development environment?   I mean its not practical to put images and assets ( that are under construction ) to various domains when building.   Would something like this work if you aliased ports on say localhost?</description>
      <pubDate>Fri, 05 Jan 2007 20:15:24 +0000</pubDate>
      <guid>urn:uuid:604a42cb-f8a3-41e8-8f4a-da9c48cc7897</guid>
      <link>http://www.brainspl.at/articles/2006/12/29/speed-up-page-loads#comment-3874</link>
    </item>
    <item>
      <title>"Speed up page loads" by Joe Ruby</title>
      <description>How in the world do you end up with TWO-HUNDRED-AND-THIRTY-F'ING-ONE IMAGES ON A PAGE?!? ;P</description>
      <pubDate>Mon, 01 Jan 2007 06:26:09 +0000</pubDate>
      <guid>urn:uuid:a45aed51-e8fb-4669-a4c8-4c4e511fac66</guid>
      <link>http://www.brainspl.at/articles/2006/12/29/speed-up-page-loads#comment-3861</link>
    </item>
    <item>
      <title>"Speed up page loads" by Ezra</title>
      <description>This technique does indeed decrease page load times. I have tested it thouroughly for a clients site. The more static assets on the page the more of a difference it makes. On a page with 231 small images it yielded a 20-30% improvement in the load time of the home page. In this case the home page is page cached so I wasn't very worried about the efficiency of the helper method as the home page only gets rendered out to cache once evey 10 minutes.

@Dan- yes there are plenty more things you coudl do with this technique code wise. Maybe soemone will make a plugin. This implementation was just to start a discussion and to see how other people's apps fare when they try this. It is not a hard change to make to an app and the perf improvement is nothing to laugh at.

@Thorsten- This blog is still hosted at rimu hostiong on a tiny vps so it is quite slow. One day when i get some free time I will finally move it to engine yard but it hasn't been a priority yet.

Once people have pretty much finished their apps and are looking to eek out any last performance improvements they can get, techniques like this and a few other that I will post articles about can help to get those last few bits of latency out of the setup. Every little bit counts. This technique might not buy you much if you don't have a ton of objects on the page that have to be fetched. but as the number of objects per page goes up, this trick helps more and more.
</description>
      <pubDate>Sat, 30 Dec 2006 20:41:23 +0000</pubDate>
      <guid>urn:uuid:ceff2dac-1b66-4879-86a9-d40efa28e833</guid>
      <link>http://www.brainspl.at/articles/2006/12/29/speed-up-page-loads#comment-3860</link>
    </item>
    <item>
      <title>"Speed up page loads" by Joe Ruby</title>
      <description>Yeah, I'd like to see some performance numbers on this...&lt;br&gt;
&lt;br&gt;
I have sites with lots of images on a page and they always seem "snappy" enough.</description>
      <pubDate>Sat, 30 Dec 2006 19:23:23 +0000</pubDate>
      <guid>urn:uuid:5d4d21b9-ec30-43c5-9513-a639ccac93d5</guid>
      <link>http://www.brainspl.at/articles/2006/12/29/speed-up-page-loads#comment-3859</link>
    </item>
    <item>
      <title>"Speed up page loads" by Dan Kubb</title>
      <description>&lt;p&gt;Ezra, why not cache the crc32 value in a Hash class variable?&lt;/p&gt;

&lt;p&gt;Also, here's &lt;a href="http://www.ajaxperformance.com/?p=33" rel="nofollow"&gt;another article&lt;/a&gt; that provides graphs that visually illustrate how browsers download the HTML and assets serially. Once you see the graphs you'll understand better why this technique works so well.&lt;/p&gt;</description>
      <pubDate>Sat, 30 Dec 2006 17:59:55 +0000</pubDate>
      <guid>urn:uuid:b684d344-59ab-47c9-ab56-d7bb41966175</guid>
      <link>http://www.brainspl.at/articles/2006/12/29/speed-up-page-loads#comment-3858</link>
    </item>
    <item>
      <title>"Speed up page loads" by David Felstead</title>
      <description>Thorsten - check out this analysis of optimizing page load time, this fellow's already done all the hard yards for us.  The cliff notes is that pipelining is actually disabled by default for all browsers bar Opera, and spreading content over multiple hostnames is a major improvement (if pipelining is disabled, which it most likely is):
  http://www.die.net/musings/page_load_time/</description>
      <pubDate>Sat, 30 Dec 2006 06:30:22 +0000</pubDate>
      <guid>urn:uuid:889b0c07-05c7-4a6e-be50-b4813bc80c1e</guid>
      <link>http://www.brainspl.at/articles/2006/12/29/speed-up-page-loads#comment-3857</link>
    </item>
    <item>
      <title>"Speed up page loads" by Thorsten von Eicken</title>
      <description>Wow, I thought the "preview comment" link didn't work, but no, it does work, it just takes about 30 seconds...</description>
      <pubDate>Sat, 30 Dec 2006 05:42:06 +0000</pubDate>
      <guid>urn:uuid:fe793412-db19-40d3-805b-f21797588b82</guid>
      <link>http://www.brainspl.at/articles/2006/12/29/speed-up-page-loads#comment-3856</link>
    </item>
    <item>
      <title>"Speed up page loads" by Thorsten von Eicken</title>
      <description>Ezra, have you actually tested this? I doubt this produces an improvement that is worth the hassle.

HTTP 1.1 not only supports persistent connections, it also support pipelining. This means that over those two connections the browser can send the requests back-to-back before even receiving a single response. This completely eliminates the dead-time between getting back the last packet of the previous request and then sending the next request. Instead, all the requests are queued at the server, which spits out the responses as fast as TCP will allow.

With two connections I expect that the network bottleneck is pretty much maxed out. Yes, 8 may eek out 10% or 20% more, but that won't make much of a difference to the user.

Anyway, it would be interesting to see some actual packet traces of what the various browsers do. Aaahhh, the ever returning wish of endless time....</description>
      <pubDate>Sat, 30 Dec 2006 05:40:46 +0000</pubDate>
      <guid>urn:uuid:3d6aa15b-f283-4371-ad7f-e253f8603fc0</guid>
      <link>http://www.brainspl.at/articles/2006/12/29/speed-up-page-loads#comment-3855</link>
    </item>
    <item>
      <title>"Speed up page loads" by Ezra</title>
      <description>Thanks David, I updated the article to use Zlib instead of md5.</description>
      <pubDate>Sat, 30 Dec 2006 01:48:07 +0000</pubDate>
      <guid>urn:uuid:c43b9ac6-67cd-42ff-8638-77ebc2a0183f</guid>
      <link>http://www.brainspl.at/articles/2006/12/29/speed-up-page-loads#comment-3854</link>
    </item>
    <item>
      <title>"Speed up page loads" by David Felstead</title>
      <description>Yow, lost my line breaks...  Ooops.</description>
      <pubDate>Sat, 30 Dec 2006 00:57:48 +0000</pubDate>
      <guid>urn:uuid:35edde36-29fb-4736-9e7a-6d91eb068b3b</guid>
      <link>http://www.brainspl.at/articles/2006/12/29/speed-up-page-loads#comment-3853</link>
    </item>
    <item>
      <title>"Speed up page loads" by David Felstead</title>
      <description>Hey Ezra - nice idea!  I saw a recommendation from a MS or Google guy (can't remember which) saying that based on his experients 5 domains was optimal, from memory.

Also, can I suggest using Zlib.crc32 over Digest::MD5.hexdigest - it is almost an order of magnitude more efficient (hope Zed isn't watching my crappy statistics here :)

[~] davidfelstead$ time ruby -rmd5 -e "250000.times do Digest::MD5.hexdigest('snorkelfish.gif') end"

real    0m1.629s
user    0m1.593s
sys     0m0.016s

VS

[~] davidfelstead$ time ruby -rzlib -e "250000.times do Zlib.crc32('snorkelfish.gif') end"

real    0m0.255s
user    0m0.240s
sys     0m0.008s

Or even just use String#hash, that's twice as fast as crc32 again.  I haven't checked the distributions, but for crc32 and MD5 I will wager it's more than good enough, not sure about string#hash.

Obviously it's not going to make a huuuuuuuuuge difference in performance either way, but why waste CPU cycles?</description>
      <pubDate>Sat, 30 Dec 2006 00:57:01 +0000</pubDate>
      <guid>urn:uuid:aa4fcd01-def9-4c75-bf73-8573b484a76e</guid>
      <link>http://www.brainspl.at/articles/2006/12/29/speed-up-page-loads#comment-3852</link>
    </item>
    <item>
      <title>Speed up page loads</title>
      <description>&lt;p&gt;Here is a little tip that can drastically speed up your page loads for pages that have a lot of static assets. Especially for pages with tons of little images.&lt;/p&gt;


	&lt;p&gt;Most browsers will only open up 2 concurrent connections per cname. this means that if all of your assets are being served from http://example.com and you have a lot of little images and scripts on the page, the clients browser will only open two connections to example.com and pipeline or use those two connections to download all the assets on the page.&lt;/p&gt;


	&lt;p&gt;By using a wildcard subdomain or manually setting dns so that you spread out the static assets over a few different subdomains, you let the browser open two connections per subdomain so all the assets will download in a more parallel fashion. This can be the difference between your page loads lagging at the end while they load up all the little assets and having the page snap into place and seem a lot quicker to the user.&lt;/p&gt;


	&lt;p&gt;So if we use a simple little view helper method for all of our image urls we can spread the load out by faking the browser into thinking it is connecting to multiple servers. For example if we serve all our images from these subdomains:&lt;/p&gt;


&lt;pre&gt;
asset1.example.com
asset2.example.com
asset3.example.com
asset4.example.com
&lt;/pre&gt;

	&lt;p&gt;This will give us 8 concurrent connections from the browser to the server for static assets which dramatically decreases page load time. The thing to watch out for is that you always want to serve the same asset from the same subdomain or else you defeat browser caching and won&amp;#8217;t gain anything from this trick. So we will use an Zlib hash of the asset url modulo 4 to choose a subdomain. Here is a simple helper:&lt;/p&gt;


&lt;pre&gt;
require 'zlib'

  # balance images across many domains to force the opening of more connections  
  # updated to use Zlib.crc32 instead of md5 as per
  # comment from David
  def balanced_asset_url(asset)        
    idx = (Zlib.crc32(asset || "error" ) % 4) + 1
    %!http://asset#{idx}.#{request.domain}#{asset}!
  end
&lt;/pre&gt;

	&lt;p&gt;Then use it like this:&lt;/p&gt;


&lt;pre&gt;
&amp;lt;%= image_tag balanced_asset_url('/images/foo.png') %&amp;gt;
&lt;/pre&gt;

	&lt;p&gt;By hashing the asset path we make sure that each time this helper is called for the same asset it will always return the same subdomain.&lt;/p&gt;


	&lt;p&gt;This technique is most useful when you have many objects on a page that need to make an additional http  request each to render. By tricking the browser into making more concurrent connections when fetching assets we can speed up our page load times and make our sites seem more &amp;#8216;snappy&amp;#8217;&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;m sure you could make this into a nice little plugin or integrate it into the normal image_tag helper if you so desired. This is just an illustration.&lt;/p&gt;</description>
      <pubDate>Fri, 29 Dec 2006 23:51:00 +0000</pubDate>
      <guid>urn:uuid:904326c2-b12a-4d64-9599-a97399de2e71</guid>
      <author>ezmobius</author>
      <link>http://www.brainspl.at/articles/2006/12/29/speed-up-page-loads</link>
      <category>rails</category>
      <trackback:ping>http://www.brainspl.at/articles/trackback/3851</trackback:ping>
    </item>
  </channel>
</rss>

