ClearScript V8 Running as a Calculation Engine #593
-
We are allowing users to write their own custom calculations using JavaScript. It would be typical for the user to have a few unique scripts, but several thousand calculations instanced off of those scripts. For example, they might have thousands of data streams that they want to run a script against every time a new event arrives for a particular data stream. So, the script remains the same, however, different parameters are passed to the script such as the stream Id. In addition, it is common for the scripts to use our client to fetch events for those streams from the database. So there is a combination of network I/O and CPU bound logic. I'm trying to think of the most efficient way to invoke potentially thousands of these calculations every minute. I probably don't want to have to use a new V8ScriptEngine. It is probably better to compile once, then invoke the function over and over with the different arguments for each calculation. That being said, if one calculation writes data to a global object, another instance could overwrite it, so I they should probably be isolated. Is there a way to snapshot a V8ScriptEngine and quickly load it back into memory, so that each calculation has its own isolated context. Second, how do I prevent the thousands of instances from blocking threads while waiting for network I/O? Our clients are all async methods that we expose as host objects; however, calling Any ideas on these two items? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi @ninlar,
Yes. If a script is to be executed many times, spinning up a new engine for each invocation is almost always the wrong way to go. Invoking an existing script function is the fastest and often the best way.
Using separate engines is the only way to prevent scripts from sharing a global object. With V8, you have several options:
For (1), simply construct
No, but with a shared runtime, extra
Your application architecture appears to be fairly sophisticated, and it's difficult to make specific recommendations without knowing the details. So here are some general notes:
Please let us know if you have additional questions or thoughts. Thanks! |
Beta Was this translation helpful? Give feedback.
Hi @ninlar,
Yes. If a script is to be executed many times, spinning up a new engine for each invocation is almost always the wrong way to go. Invoking an existing script function is the fastest and often the best way.
Using separate engines is the only way to prevent scripts from sharing a global object. With V8, you have several options: