Speculative parsing and chunk size

I've started doing some tests to understand how different browsers download and parse HTML and JavaScript, something I should've done a long time ago. I'll post about the things I find interesting. Here's one:

You probably know that the latest browsers perform "speculative parsing" (a.k.a. "preload scanning"). Say the UI thread is waiting for a script to download. The thread won't (can't) continue building the DOM from the rest of the page, because the script may affect the DOM. So the script must be parsed and executed first. However, there's nothing wrong in at least scanning the rest of the page quickly for resource URLs and queueing them up for download, while waiting for the script to arrive.

However, once the script has arrived, the UI thread will get busy parsing and executing it. It won't be able to look for any more resources if some more page content arrives during that time.

Consider an HTML page that's delivered in two chunks:
<!-- chunk 1 -->
<script src=foo.js></script>
<image src=bar.jpg>

<!-- chunk 2 -->
<image src=quux.jpg>
After the first chunk arrives, the browser parses it, discovers the script (foo.js) and queues it up for download. While waiting for the script to download, the UI thread is idle, so it also scans the rest of the chunk, discovers the image (bar.jpg) and queues it up for download as well. Now, assume the script finishes downloading before the second chunk of the page. The UI thread will start parsing and executing the script. Imagine the script takes a while to execute, and during this time, the second chunk arrives. The UI thread will not get to the chunk until the script is done, so you'll see that the second image (quux.jpg) isn't discovered or queued for download until much later.

I was trying to demonstrate this effect in Chrome. However, even though I was sure that I was flushing the first chunk properly, Chrome didn't seem to even start downloading the first image (bar.jpg) until the script was done. Safari behaved the same way. After some more testing, I realized that it was because my tests were too small. These WebKit browsers do not scan the rest of the chunk unless the chunk is at least 1KB in size! Firefox doesn't have this issue. It scans the rest of the chunk no matter how small it is.

Post a Comment permalink

Hiking

Another week, another first. Went for a hike yesterday, with two of my colleagues. Until now, I've done only mild stretches, such as 4 miles at Rancho San Antonio and 6 miles on flat streets. So, this 20 mile, 4000+ feet elevation gain was a drastic move. We went from Del Valle to Rose Peak, and back, in 10 hours.

It was a nice and warm day; the trail was well kept; the scenery was very pleasant (photos). At the top at Rose Peak, we could see the clouds below us. We walked the last hour in the dark, but thanks to Gurmeet's flashlight, it was not so bad.

Towards the end of the hike, I found it hard to say which I found more painful - uphill or downhill. Uphill was slow-going, heavy-breathing and groin-aching. Downhill was knees-killing. It didn't help that I had to lug around my backpack loaded with a lot of useless things, including an umbrella, extra pair of shoes and a couple of t-shirts.

1 comment permalink

It was my first time. And it was awesome!

Watched an NBA game with my coworkers on Monday. My first time watching one. It was amazing.

I'm not a basketball fan, so I didn't know anything about the teams (Golden State Warriors vs Dallas Mavericks). I kinda assumed that they were some random teams, so I thought it'd be like a random little league game, with a few die-hard fans thrown in.

Whoa, was I wrong.

The thing that struck me the most was the professionalism of the whole thing. The stadium (Oracle Arena) was in full swing. Lights, ads, real-time TV display, awesome camera work in replays, slow mo and angles, popcorn vendors, etc. Every timeout, every gap between the quarters, the NBA had something going on - trampoline stunts, 3rd grade kids playing, pizza give-aways, t-shirt guns, lucky draws, guess-the-song contests, and uh, the cheerleaders. Having played a minor logistics role back in my school days, I appreciated the co-ordination and practice needed to pull off such a smooth operation.

The game itself was not as much fun. The Warriors led for the most part, though both teams were playing very fast and loose. Eventually, the Mavericks tightened up their game, and won handily. Apart from the 3rd grade kids playing full-on basketball, the most fun I had was listening to a couple of vociferous Warriors supporters nearby. "Are you blind, referee??!! Come on man! The guy was sliding on his ass! Dirk No-game-zki!" and so on.

I can only imagine what it would have been like if the Lakers and Kobe were playing...

Post a Comment permalink

Perseverance

Mohandas Gandhi"First they ignore you, then they laugh at you, then they fight you, then you win."

— Mohandas Gandhi.


1 comment permalink quotes

Scientific truth

Richard Dawkins"I resolved to decline future invitations from debating societies that encourage insincere advocacy on issues where scientific truth is at stake."

— Richard Dawkins, Preface to The Blind Watchmaker, 1986.


Post a Comment permalink quotes

Liberty and security

Benjamin Franklin"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety."

— Benjamin Franklin, Historical Review of Pennsylvania, 1759.


Post a Comment permalink quotes

Pursue a dream

Bruce Schneier"When a big company lays you off, they often give you a year's salary to 'go pursue a dream.' If you're stupid, you panic and get another job. If you're smart, you take the money and use the time to figure out what you want to do next."

— Guru Profile, http://www.guru.com/channel/tech/portrait/P66.jhtml, Aug 1999.


Post a Comment permalink quotes