Some basic compiled script questions

Feb 26, 2014 at 8:31 PM
Hey guys, a few questions regarding the V8 script compilation:
  • Why are compiled scripts available to evaluate but not to execute?
  • Are executed scripts always compiled internally?
  • Is there any advantage to adding host objects/types before script compilation or is that a runtime only thing?
Coordinator
Feb 26, 2014 at 9:41 PM
Hi Eric,

Why are compiled scripts available to evaluate but not to execute?

Script compilation is a V8-specific feature, and since in JavaScript evaluation and execution are the same thing, there's no need for two sets of methods. Evaluation and execution are currently different only in VBScript; V8ScriptEngine has some execution methods only because it inherits them from ScriptEngine.

Are executed scripts always compiled internally?

In V8ScriptEngine, all execution and evaluation methods that accept script code in string format internally perform a compilation step followed by an execution step.

Is there any advantage to adding host objects/types before script compilation or is that a runtime only thing?

Our understanding is that script compilation is not affected by the execution environment, which includes the global object hierarchy. In fact, compiled scripts are reusable across V8ScriptEngine instances that share the same V8 runtime.

Cheers!
Feb 26, 2014 at 10:04 PM
Thanks for the fast answer...that was leading to a second question:

I would like to cache my compiled scripts, but it seems from what I've read in the discussions that the compiled scripts are associated with a particular V8 runtime instance. Is that simply a V8 limitation?

In that case I would have to create a managed V8 engine pool with each engine having an associated script cache (which obviously detracts from the benefits of the cache). In my case, I'm going to have a large number of scripts that are user created/edited, so I'm not sure if it's worth the effort. Would that be the suggested approach if I wanted to tackle that?
Coordinator
Feb 27, 2014 at 1:49 PM
Edited Feb 27, 2014 at 1:51 PM
Hello Eric,

I would like to cache my compiled scripts, but it seems from what I've read in the discussions that the compiled scripts are associated with a particular V8 runtime instance. Is that simply a V8 limitation?

Yes. A compiled script is bound to the V8 runtime that created it. There's no cross-runtime script representation that performs better than plain-text JavaScript.

Well, actually, there could be. V8 also supports precompilation, which generates reusable offline data that speeds up compilation, but (a) ClearScript currently doesn't support it, and (b) it would still require per-runtime compilation.

In that case I would have to create a managed V8 engine pool with each engine having an associated script cache (which obviously detracts from the benefits of the cache).

A couple of points: The main benefit of a compiled script is enhanced performance for repeated execution. If a script is likely to be run multiple times in multiple runtimes, it makes sense to compile and cache it for each runtime unless you're memory-constrained. Also, depending on your requirements, it might make sense to share runtimes across engine instances, which would greatly reduce memory usage and enable compiled script sharing at the cost of reduced concurrency.

Good luck!
Feb 27, 2014 at 9:05 PM
Good info, thanks. I'm implementing a runtime pool w/ caching as discussed.
Mar 17, 2014 at 4:54 PM
If this helps anybody, I encapsulated some of the ideas in this post into a project:
https://github.com/eswann/ClearScript.Manager

And a Nuget package:
https://www.nuget.org/packages/ClearScript.Manager/
Coordinator
Mar 18, 2014 at 12:32 PM
Thanks, Eric! Very nice work!