Use RegExp outside of an Isolate?

73 views
Skip to first unread message

Paul Maréchal

unread,
Aug 3, 2020, 4:37:48 PM8/3/20
to v8-dev
Hi,

I am writing a Node.js addon that does work in a native thread. I need to run a regex match in said thread and I was hoping to use V8's RegExp objects.

It is fair to say that I'm not a C++ expert, so I was wondering if anyone had any pointers regarding this? Is it even possible/a good idea?

The API I am looking at has a deprecated static method that looked interesting but I don't understand how I am supposed to use the new one? https://v8docs.nodesource.com/node-10.15/d8/da7/classv8_1_1_reg_exp.html#aad5eb4ac27a23660c32f289da9427367

Thanks in advance,
Paul.

Jakob Kummerow

unread,
Aug 3, 2020, 5:44:34 PM8/3/20
to v8-...@googlegroups.com
A v8::RegExp requires an Isolate. (There are at least two indicators for this: (1) the class RegExp derives from Object, as such they are JavaScript objects, and JavaScript objects always belong to an Isolate; (2) Local<>s are used to hold RegExp instances, and Locals are used to refer to objects on the managed heap, which is part of an Isolate.)

Aside from that, v8::RegExps operate on v8::Strings, which also require an Isolate to live in.


--
--
v8-dev mailing list
v8-...@googlegroups.com
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-dev+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/v8-dev/667aed79-f5df-4005-ab55-e079cfc20b92n%40googlegroups.com.

Maréchal Paul

unread,
Aug 3, 2020, 6:38:13 PM8/3/20
to v8-...@googlegroups.com
Hi Jakob,

Thank you for the answer. Now that you mention it, even with the `Local<RegExp> New(Local<String> pattern, Flags flags)` method, I'd need an isolate to hold the pattern indeed.

Then I have the following question: Is it a good idea for me to try and instantiate an Isolate of some sort, and have that run in the native thread I try to run the regexp into?

You received this message because you are subscribed to a topic in the Google Groups "v8-dev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/v8-dev/B7tJko0Itnk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to v8-dev+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/v8-dev/CAKSzg3QpALxgBg8d4x1Uc8DENY09xRmhT%2Bjuo%2B_GYpOFDfpgng%40mail.gmail.com.

Jakob Kummerow

unread,
Aug 4, 2020, 6:03:35 AM8/4/20
to v8-...@googlegroups.com
Creating a separate Isolate for RegExp work is certainly possible. Whether it's a good idea depends on your constraints. Things to consider:
- creating an Isolate has a cost; you can reduce overhead if you can keep it around for several RegExp evaluations
- on the flip side, keeping an Isolate (and its heap) around consumes more memory
- you could ship another RegExp engine instead, at the cost of some binary size
- depending on how many RegExps you want to evaluate and at what times, you could consider doing that on the main thread in the existing (Node) Isolate, and only do non-RegExp work on the background thread.

Paul Maréchal

unread,
Aug 4, 2020, 11:02:25 AM8/4/20
to v8-dev
Thanks for the pointers! I'll go with creating an Isolate and keep it in the scope of the native thread for all my RegExp evaluations. I was eyeing shipping a different regex engine already, but thanks to what you wrote I'll be able to try using v8's and see if that is enough for me.

Paul Maréchal

unread,
Aug 4, 2020, 11:08:48 AM8/4/20
to v8-dev
I do have another question before creating another Isolate: Would creating a new Context (from the main Node Isolate) be suited to work in a thread?

Leszek Swirski

unread,
Aug 4, 2020, 11:21:21 AM8/4/20
to v8-dev
No, you can only access an Isolate (including Contexts in that Isolate) from a single thread.

Ron Burk

unread,
Aug 4, 2020, 12:41:11 PM8/4/20
to v8-dev
> I need to run a regex match in said thread

Some reason you can't just use the standard C++ library regex functions and save significant CPU/memory/maintenance/time?

Paul Maréchal

unread,
Aug 4, 2020, 2:16:38 PM8/4/20
to v8-dev
> No, you can only access an Isolate (including Contexts in that Isolate) from a single thread.

Ok, thanks!


> Some reason you can't just use the standard C++ library regex functions and save significant CPU/memory/maintenance/time?

I am currently using std::regex, but I have two issues with it:
1. The RegExp I need to evaluate come from a JS context, and there are differences in evaluation between JS and the C++ std lib (not a show stopper, but if I can get the same behavior I'll take it).
2. std::regex appears to be rather slow... I am trying to allow the JS side to filter some things out happening on the C++ side, but with patterns such as ".*/\\.cache" I get worse performance than when not filtering anything (setup time is x4 times slower in some cases, from 2s to 8s...)

Regarding (2) despite increasing the setup time of my addon, the filtering still allows me to save on fd handles. But if v8's RegExp runs fast enough and allows me to get better perfs I'd be happy.
Reply all
Reply to author
Forward
0 new messages