Bug 22227 - Caching AST and/or bytecode of JavaScripts
Summary: Caching AST and/or bytecode of JavaScripts
Status: RESOLVED WONTFIX
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: 528+ (Nightly build)
Hardware: PC All
: P3 Enhancement
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-11-13 02:19 PST by Gabor Loki
Modified: 2012-11-23 15:45 PST (History)
4 users (show)

See Also:


Attachments
export/import AST for caching (125.81 KB, patch)
2008-11-13 02:30 PST, Gabor Loki
ggaren: review-
Details | Formatted Diff | Diff
result of AST caching on regression tests (1.21 KB, text/plain)
2008-12-01 04:12 PST, Gabor Loki
no flags Details
result of AST caching on cnn.com (194 bytes, text/plain)
2008-12-01 04:14 PST, Gabor Loki
no flags Details
result of AST caching on kongregate.com (239 bytes, text/plain)
2008-12-01 04:15 PST, Gabor Loki
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Gabor Loki 2008-11-13 02:19:32 PST
I was wondering why JSC does not provide a way to serialize a JavaScript. It could be very handy in several cases, for example:
- large JS code, but only a small part of it will be executed
- with timeout or refresh rate WebCore does not need to touch the network for external/included JS

Anyway, at least the AST caching could be profitable. I mean the parser does know nothing about the source. It have to check tokens, do forward lookup, handle syntax errors, etc. So the parser do many checks in contrast to a simple binary reader. If we caching AST and writing into a well-defined binary form we could save those parser's checks.

If we do one step forward and cache bytecode too we will save AST traversing from code generation and reading/building this part of the AST from the binary form.

The drawback of this method is the serialization, the caching.

I have implemented a simple AST exporting/importing feature for JSC. With it we can measure how effective this kind of caching approach.

I would like to read your options about it. Is anyone interested in it? Is it a good idea, or not? How could we make it a profitable feature?
Comment 1 Gabor Loki 2008-11-13 02:30:04 PST
Created attachment 25121 [details]
export/import AST for caching

This patch adds export and import features for AST.

I don't expect a 'r+' for this patch. It is not a final code. The reason of why I ask for a review is that I would like to start a discussion about this caching method and its implementation possibilities.

The patch does not cause any JS regression, and does no performance regression on SunSpider and V8 tests.
Comment 2 Geoffrey Garen 2008-11-13 15:42:07 PST
Parsing doesn't show up very hot on most performance benchmarks. We're also exploring ways to avoid most of the overhead of parsing until a function executes. I'm skeptical of the advantages of caching parse trees.
Comment 3 Gabor Loki 2008-11-14 01:55:59 PST
IMHO the performance benchmarks mostly show how fast the execution of bytecode.
They almost say nothing about parsing and code generation, because most tests contain more or less loops, and its execution could cost more time than parsing them.

I'd appreciate if you can give me a clear view why you are skeptical.
Anyway, I will try to find some living example where this caching could work.
Comment 4 Geoffrey Garen 2008-11-25 18:09:08 PST
> IMHO the performance benchmarks mostly show how fast the execution of bytecode.
> They almost say nothing about parsing and code generation, because most tests
> contain more or less loops, and its execution could cost more time than parsing
> them.

SunSpider is very careful to include the cost of lexing, parsing, and bytecode generation in each iteration of a benchmark. When we look at Shark profiles of SunSpider, we see lexing and parsing show up a bit, but it's a very small bit, and easy to optimize.

Our general policy in JavaScriptCore and WebCore is not to bother caching data that is very cheap to recompute. It seems unreasonable to risk going to disk to retrieve data that you could otherwise compute in memory.

Perhaps there are other, better benchmarks that show parsing to be more important. But I think it would be quite strange to write an application that just loaded more and more code without ever executing it.

We also have an optimization to avoid creating an AST at all until a function executes. So, in the case of an application that loads a lot of code without executing any of it, JavaScriptCore will actually do quite well.

> Anyway, I will try to find some living example where this caching could work.

Did you end up finding something compelling?
Comment 5 Geoffrey Garen 2008-11-25 18:10:27 PST
Comment on attachment 25121 [details]
export/import AST for caching

r- because we don't have an example in which this feature would be a performance advantage.
Comment 6 Gabor Loki 2008-12-01 04:12:23 PST
Created attachment 25624 [details]
result of AST caching on regression tests

Here is the first results of AST caching. It is measured during executing regression tests. This is not an online result, but it shows the advantage of caching.
Comment 7 Gabor Loki 2008-12-01 04:14:52 PST
Created attachment 25625 [details]
result of AST caching on cnn.com

Here is the first online result.
Only the 'Interpreter::evaluate' function was measured in this method.
Comment 8 Gabor Loki 2008-12-01 04:15:58 PST
Created attachment 25626 [details]
result of AST caching on kongregate.com

Here is another online result.
Only the 'Interpreter::evaluate' function was measured in this method.
Comment 9 Gabor Loki 2008-12-01 04:24:35 PST
All attached results contain three different measurements:
- 'Reference' (patched version of WebKit without CACHE_AST define)
   JS performance without AST cache.

- 'Build and use' (patched version of WebKit)
   After parsing each JS source export the AST into a file
   Next time for each JS source skip parsing, load and build AST from file.

- 'Only use' (patched version of WebKit)
   Load and build AST from file.
Comment 10 Gabor Loki 2008-12-01 04:30:04 PST
The summary of the attached results:

SunSpider: no significant effect (no progression/no regression)

Regression tests: 9% progression
1. Online example: 5 times faster
2. Online example: 3.5 times faster
Comment 11 Gabor Loki 2012-11-23 09:38:48 PST
The idea of caching AST is a little bit old. I won't be able to work on this in the near future. If you think this idea is still interesting, we can keep this feature request open. Otherwise it should be closed as won't fix.