V8 Regex Optimizations

47 views
Skip to first unread message

Joel Scarfone

unread,
Oct 23, 2019, 11:12:00 AM10/23/19
to v8-users
Hi!

I'm looking for details on how v8's regular expression optimizations work under the covers, and if there might be something to improve the performance of a given regular expressions first execution. From what it looks like trying some things out, v8 does most of it's optimizations after a call that uses the RegExp and not when the constructor is called (eg. through `new RegExp()`).

I am Wondering what some options are in this area to move around the cost of compiling/running the regular expression.

Thanks in advance!

Joel

Mathias Bynens

unread,
Oct 23, 2019, 11:34:38 AM10/23/19
to v8-users
We recently posted an article detailing some recent improvements: https://v8.dev/blog/regexp-tier-up

--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups "v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/v8-users/05a13931-5a1d-4b21-8616-ffd3010dd03a%40googlegroups.com.

Jakob Gruber

unread,
Oct 24, 2019, 1:12:54 AM10/24/19
to v8-u...@googlegroups.com
On Wed, Oct 23, 2019 at 5:12 PM Joel Scarfone <joelrs...@gmail.com> wrote:
Hi!

I'm looking for details on how v8's regular expression optimizations work under the covers, and if there might be something to improve the performance of a given regular expressions first execution. From what it looks like trying some things out, v8 does most of it's optimizations after a call that uses the RegExp and not when the constructor is called (eg. through `new RegExp()`).

I'm not aware of much documentation on this topic. A recent blog post (https://v8.dev/blog/regexp-tier-up) discusses the recent addition of tiering and describes performance work on the interpreter. But for details you'll have to dig through the source code.

As you say, a regexp is compiled lazily at the first exec call. With tiering, we first compile to bytecode, then later recompile to native code. There are many intricacies involved, e.g. we cache compilation artifacts keyed on {pattern,flags}, and constructing a regexp instance is expected to be cheaper from a literal (/abc/) than when using the constructor (new RegExp("abc", "")).
 

I am Wondering what some options are in this area to move around the cost of compiling/running the regular expression.

I think I'd need more details on what you want to achieve to give a meaningful answer. 

Joel Scarfone

unread,
Oct 24, 2019, 4:14:28 PM10/24/19
to v8-users
Let's say I have a Script that uses somewhere in the realm 100 regex's in different functions. I want the first call to each of those functions to be fast, so I move the constructor to the global object to pre-compile the regex's. From the v8 docs, "In its default configuration, V8 compiles regular expressions to native code upon the first execution", so I force this pre-complication with something like `for(var rule of regexRules) { "".match(regex) } `. Doing this, I notice a significant improvement in the execution on first call back into the context to execute a function that uses these regex's. 

Is there a way to change this default configuration?

Joel
To unsubscribe from this group and stop receiving emails from it, send an email to v8-u...@googlegroups.com.

Jakob Gruber

unread,
Oct 28, 2019, 2:52:06 AM10/28/19
to v8-u...@googlegroups.com
If you embed V8 yourself, the runtime flags --regexp-tier-up and --regexp-tier-up-ticks control tier-up behavior. 

Do you have an example of a particularly slow regexp pattern and subject string? 

To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/v8-users/ecb02276-8ccd-4f1f-b3bd-6aba79799985%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages