markt1964
markt1964

Reputation: 2836

Precompile v8 script for use in multiple isolate

I have implemented a 'require'-like function using embedded v8 that loads a JavaScript file and executes it, but because my program has multiple threads, and as such each thread has its own isolate, I am having to load and compile the file separately in each thread that specifies the same source. I am wanting to, if possible, somehow cache any compiled script so that if another thread (using another isolate) happens to want the same file, I can utilize some sort of precompiled format, and just give it the script to run, instead of having to separately compile it inside of each isolate that needs the same file.

Upvotes: 3

Views: 1623

Answers (2)

roiber
roiber

Reputation: 11

I think ScriptCompiler can help you. With ScriptCompiler::CompileUnboundScript you can create and consume cached data for the script. I did not (yet) test it but the comment looks promising:

/**
   * Compiles the specified script (context-independent).
   * Cached data as part of the source object can be optionally produced to be
   * consumed later to speed up compilation of identical source scripts.
   *
   * Note that when producing cached data, the source must point to NULL for
   * cached data. When consuming cached data, the cached data must have been
   * produced by the same version of V8.
   *
   * \param source Script source code.
   * \return Compiled script object (context independent; for running it must be
   *   bound to a context).
   */

Upvotes: 1

Esailija
Esailija

Reputation: 140234

I don't see how it's possible, all the Code, Script, SharedFunctionInfo etc are JavaScript object specific to an isolate.

You can however build a static snapshot of some V8 state and have this state always loaded by all isolates, but this cannot be dynamically for runtime. This is how what V8 builtins do.

Upvotes: 2

Related Questions