Search code examples
c++multithreadingv8precompileembedded-v8

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.


Solution

  • 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.