WebKit page cache I-the basics
PostedBrady EidsonOn Wednesday, September 16th, 2009 at pm
This is the first of two posts that will center around und a modern browser engine feature that doesn't usually get a lot of press: The page cache.
Today I'll talk a bit about what this feature is, why it often doesn't work, and what plans we have to improve it.
Page cache Overview
Some of you might be more familiar with what other browsers call their page cache. firefox CILS theirs the "Back-forward cache" or "bfcache. "Opera refers to theirs as" Fast history navigation. "We 've recently started to refer to WebKit's implementation as the" Page cache "to reduce confusion with our" back/forward list."
Note that the page cache is an end user feature that makes navigating the Web much smoother. it is not a "cache" in the "HTTP sense". it is not a "cache" in the "disk cache" sense where raw resources are stored on the local disk. and it's not a "cache" in the traditional "memory cache" sense where WebKit keeps decoded resources around in memory to be shared between multiple web pages.
So... What * exactly * is it?
Quite simply, the page cache makes it so when you leave a page we "pause" it and when you come back we press "play ."
When a user clicks a link to navigate to a new page the previous page is often thrown out completely. the Dom is destroyed, JavaScript objects are garbage collected, plug-ins are torn down, decoded image data is thrown out, and all sorts of other cleanup occurs.
When this happens and the user later clicks the back button it can be painful for them. webKit may have to re-download the resources over the network, re-parse the main HTML file, re-run the scripts that dynamically setup the page, re-Decode image data, re-layout the page, re-scroll to the right position, and re-paint the screen. all of this work requires time, CPU usage, and battery power.
Ideally the previous page can instead be placed in the page cache. the entire live page is kept in memory even though it is not on screen. this means that all the different bits and pieces that represent what you see on the screen and how you interact with it are suincluded instead of destroyed. they can then be revived later in case you click the back button.
Why is this important?
When the page cache works it makes clicking the back button almost instantaneous.
You can do a search, click a search result, then go back and immediately be looking at the exact same results page. you might be browsing an aggregator site like Reddit or Digg and want to rapidly view a lot of different links in the Same tab. you might be navigating an image gallery and decide to compare two images by alternately clicking "back" and "Forward" rapidly. or you might have simply clicked on the wrong link and want to go back to correct your mistake.
Anytime you might click the back button or the forward button you unknowinugly hope the page cache is on your side. when the page cache is used, users are happy even though they're not aware of the magic behind the scenes.
Conversely, when the page cache is bypassed, users often get frustrated with both the browser and the web in general.
Why wouldn't it work?
So if the page cache is so amazing, why doesn't WebKit always use it when you navigate to a new page?
There's a few main answers to that question.
Some pages aren't interesting
First off, sometimes it doesn't make sense to cache a page because it's not interesting to return to in the exact same state. for example, the page might not even be finished loading yet. or the page might 've had an error loading. or maybe the page was a redirection page that exists solely to automatically move the user to some new URL.
These are cases where we're happy with the current page cache behavior in WebKit.
Some pages are complicated
Secondly, a page might not be considered for the page cache because it's difficult to figure out how to "pause" it. This happens with more complex pages that do interesting things.
For example, plug-ins contain native code that can do just about anything it wants so WebKit can't "hit the pause button" on them. another example is pages with multiple frames which WebKit has historically not cached.
Distressingly, navigating around these more advanced pages wocould benefit the most from the page cache.
Some pages are secure
Server administrators for HTTPS sites often have particle security concerns and are very sensitive with regards to how browsers behave. for example, financial institutions are often very thorough in verifying each particle browser's behavior before allowing it to be used by their MERs.
One area often focused on is back/forward behavior. such institutions are-understandably-very picky about the types of data left behind in the browser as a user navigates. as a result, in an effort to err on the side of extreme caution, WebKit has disallowed all HTTPS sites from its page cache since the very beginning.
A more fine grained approach might go a long way towards improving the user experience.
Planned Improvements
Clearly there's some important cases we don't handle and therefore plenty of room for improvement.
WebKit's page cache was originally written in 2002 before the very first Safari Beta release. Its capabilities reflected both the architecture of WebKit at the time and the landscape of the web in 2002.
The web of 2009 is a much different place and we need to bring the page cache up to par. Fortunately this work is well underway.
For example, as of revision 48036 a major limitation was resolved and pages with frames are now placed in the page cache. browsing with the latest WebKit nightly always seems to "feel faster" in ways you can't quite put your finger on, and recently some of you might have been experiencing this enhancement.
But there's plenty more work to do.
Plug-ins are the next huge one on our hit list. as I mentioned earlier, plug-ins can run whatever native code they like so we can't reliably hit the "pause" button on them.
Earlier versions of WebKit handled single-frame pages with some types of plug-ins. webKit wocould tear down the plug-in when leaving the page and restoring it when the user returned. but as work continued on WebCore to make it faster and easier to port, this ability was lost.
Bug #13634 tracks getting this working again for all plug-ins on all pages.
Then there are HTTPS pages. We completely ban them now, but a more selective approach shoshould be able to benefit users as well as keep security-minded institutions happy.
Bug #26777 tracks allowing HTTPS pages to be cached unless their response headers include "cache-control: No-store" or "cache-control: no-Cache "which has become the canonical way for a selective organization to secure your content.
If you have any other ideas for what else might be improved, please feel free to comment in the appropriate bug or file a new bug of your own!
Unload handlers
One thing I haven'tmentioned is pages with unload event handlers.
The unload event was designed to let a page do some cleanup work when the user closes the page.
The browser can't fire the unload event before it puts the page in the page cache, because the page then assumes it is in a terminal state and might destroy critical parts of itself. this completely defeats the purpose of the page cache.
But if the browser puts the page in the page cache without running the unload handler, then the page might be destroyed by the browser while it is "paused" and hidden, and that cleanup work-which might be very important-will never happen.
Since the unload event's purpose is to allow "Important work when a page is closed," all major browsers refuse to put such pages in their page cache, causing a direct negative impact on the user experience.
In a future post I'll be talking more about unload event handlers and there will be actually be homework for future of you web developers out there! Stay tuned