I have a high-performance node app where every ms needlessly blocked in the main thread is detrimental. Most of the heavy processing is done in a C++ native node addon. Works great.
Sometimes, the Javascript code needs to pass a very long string to the addon C++ code. What bothers me (validated by profiling) is that there is a significant block in the main thread when (copying &) converting the (very long) string to UTF-8, before the copy finally gets offloaded to the worker thread.
Is it possible to offload the copy & conversion to the worker thread?
I'm aware that in general one may not access Javascript objects from a seperate thread, but this is not a hard rule (as some suggest). For example, it is possible (and common) to take a Persistent
reference to a Buffer
object and access the data directly (no copy) in a separate thread. The ideal solution would be doing the same for string data, thus avoiding any copy of the string in the main thread.