Move Realm creation to another thread #1533
Replies: 11 comments
-
Have a few more ideas relating to throwing some of the builtin construction into threads, beyond just Realm::create() Firstly, in regard to
To prevent the overhead of accessing the global object via a Mutex during execution of JS code, it might be better to have the threads that create the builtins instead send the object they create to the main thread via a channel, so the main thread will be the only thread that mutates the global env. This would also require updating every one of the Maybe to solve this, the worker threads can send construct them concurrently, but the main thread will add them only after they are all finished. But, this also presents a problem: some builtins need certain objects to exist during their own construction. For example, constructors that require the Next, if these objects are constructed concurrently, a further optimization might be to have a thread initialize groups of them so each thread has similar amounts of work. For example, one thread each for Might be an optimization of an optimization, and it would require testing and benchmarking at each step. I think it would be good to have the simple solution first, 1 thread per builtin, and try to see how that measures up to grouping up a few of them, since thread construction isn't free. |
Beta Was this translation helpful? Give feedback.
-
At some point, execution could also be done in streaming, in which case, the realm would need to be ready when the execution starts (which would call the parser streamer). |
Beta Was this translation helpful? Give feedback.
-
I've linked #419 with this because the optimisations in that should bring the realm creation time down significantly. Once that's done I will do another measure to see the impact |
Beta Was this translation helpful? Give feedback.
-
I went back to the May 31st Looking at https://boa-dev.github.io/boa/dev/bench/ it seems Realm Creation has become considerably faster than it used to be, keeping this up in case in the future we want to introduce multi-threading once the builtins are more developed? |
Beta Was this translation helpful? Give feedback.
-
The thing is that currently, we first tokenize + parse the whole program, and then we start creating the Realm. This creation is faster, but it could be parallelized with the lexing + parsing. We need it even if we fail to lex/parse, since we need to generate a Syntax Error. |
Beta Was this translation helpful? Give feedback.
-
The problem with trying to implement a parallel initialization for Realm is that a lot of them depend on each other, even if that was not the case when we create a new |
Beta Was this translation helpful? Give feedback.
-
Parsing and lexing should be independent of realm setup as neither of those tasks require use of the Gc, it’s only when you’re ready to execute it becomes a problem.
Has this changed? |
Beta Was this translation helpful? Give feedback.
-
Not that I'm aware of, I might have mixed up the order, but the thing is that one waits for the other. |
Beta Was this translation helpful? Give feedback.
-
I wonder if for now we can change the order and do the parsing before realm creation. |
Beta Was this translation helpful? Give feedback.
-
We still need the realm to create a |
Beta Was this translation helpful? Give feedback.
-
Closing this as it’s not viable right now, see #1330 |
Beta Was this translation helpful? Give feedback.
-
Realm creation is currently quite slow, and lexing and parsing doesn't start until this is finished. In the screenshot below we can see that Realm::create() runs on the main thread, and blocks everything else until its finished.
I think further optimisations can be done here to:
I'm also happy to discuss any other ideas or opportunities here
Beta Was this translation helpful? Give feedback.
All reactions