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.
Asked
Active
Viewed 1,381 times
3
-
It is not clear what 'embedded v8' is (the tag wiki doesn't exist, so there's no help there), but discussing JavaScript in a question tagged C++ (and not JavaScript too) is little surprising. Please review your tags. You might need to explain a bit more what you're trying to do — at least for someone from completely outside the area, it is mystifying. Also, you use 'isolate' as if it is a term that will be known to people in the area; it is not obvious to someone outside what it means. That may not matter if it is a piece of jargon for the experts, but review that too. (Should it be 'thread'?) – Jonathan Leffler Mar 30 '15 at 06:46
-
3I am referring to using Chrome v8 directly from C++, ie, embedded mode. V8 is google's javascript engine, and Isolate's are for each engine's context. – markt1964 Mar 30 '15 at 14:14
-
@markt1964 - Did you find a solution for your problem? – chema989 Oct 03 '16 at 15:29
-
1No, I did not. I'm probably going to migrate away from using v8 in the future – markt1964 Oct 03 '16 at 15:31
-
@JonathanLeffler v8 is a javascript engine that is embedded into a c++ application using a c++ API. The question is appropriately tagged. It has nothing to do with javascript the language. If you don't understand a question, just move on. – xaxxon Oct 03 '16 at 20:15
-
@xaxxon: Yes, I was told 18 months or so ago that V8 is a JavaScript engine. I'm not sure that the information needed repeating. The 'embedded-v8' tag still doesn't have a tag wiki (though 'v8' does). I don't understand how you can claim that a JavaScript engine can have nothing to do with JavaScript the language — that seems like a contradiction in terms, especially when the question is asking about loading JavaScript files. – Jonathan Leffler Oct 03 '16 at 20:22
-
@JonathanLeffler Someone who has full knowledge of everything javascript but no knowledge of c++ or the V8 API would have nothing to contribute to this question. V8 is an execution environment for javascript files and has specific quirks related to running javascript programs - embedded v8 is specifically around the process of embedding the interpreter into a new c++ application using the V8 C++ API. – xaxxon Oct 03 '16 at 20:23
2 Answers
2
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.

Esailija
- 138,174
- 23
- 272
- 326
-
Not the answer I was hoping to see, but I had a feeling that it might be. Thanks. – markt1964 Mar 31 '15 at 15:08
-
1"Isolate" is named as such for a reason. It's isolated. It doesn't make sense to have it precompiled across isolates because each isolate is it's own environment and can look completely different. A function called from within your script can resolve to something totally different in a different isolate. Are you actually seeing issues due to compiling for each isolate or are you prematurely optimizing? – xaxxon Oct 03 '16 at 20:17
-
As the number of scripts that I am needing to compile for each isolate grows, the setup time that I need for making each isolate ready for use in my program grows roughly in proportion to it. Any performance issues I am not yet seeing I am almost certain to see when the number of scripts grows by more than a couple of orders of magnitude. The solution, I believe, is for me to use snapshots per the above suggestion, although at this time I am not sure how to build a snapshot for more than one script – markt1964 Oct 05 '16 at 00:01
1
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).
*/

roiber
- 11
- 1