0

N00b question. I am thinking of embedding v8/SpiderMonkey into my server code. The server is taking thousands of requests per second. So multithreading is a must. I am just wondering if different threads can share compiled immutable js functions and objects? So I don't need to reinitiate them again and again when starting a new thread. Thanks!

abab
  • 71
  • 5

1 Answers1

1

V8 isolates are single-threaded (only one thread can be entered into an isolate at a time and access its heap/execute its code), thus "creating a new thread" means creating a new isolate which is a very heavy-weight thing to do so you definitely wouldn't be able to do it in a response to a request anyway.

You can create many v8 isolates and run them in their own threads in parallel but they will all be isolated instance of a js-runtime. Anything in their js-heaps cannot be shared, although they can of course share the same C object which they have their own thin JS-wrapper copy for.

However, multi-threading is not a must at all for you. Serving thousands of requests per second is trivial in a single-thread if you use asynchronous I/O and create a separate process for each core. If you want to use synchronous I/O and create a thread per request then it's not feasible with V8.

Esailija
  • 138,174
  • 23
  • 272
  • 326