1

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.

logidelic
  • 1,525
  • 15
  • 42

1 Answers1

0

Usage of ArrayBuffer or TypedArray likely minimize duplication of content by avoiding memory copy, here is an example for that.

https://github.com/msatyan/MyNodeC/blob/master/src/mync1/ArrayBuff.cpp https://github.com/msatyan/MyNodeC/blob/master/test/TestArrayBuff.js

To avoid blocking of node.js main thread you may introduce Asynchronous operation by delegating the heavy task to a thread. This following StackOverflow post has an example for such scenario by using promise.

How to create async function using NAPI that return Promises

Satyan
  • 1,346
  • 8
  • 15