4 ms·
This isn't V8 beating C; this is one regex library being faster than another (both written in C or C++).
by lambda 13y ago
This isn't V8 beating C; this is one regex library being faster than another (both written in C or C++).
- pmelendez 13y agoAs far as I know Irregexp[1] compiles on V8 byte code so you can't use it isolated without V8. Please, let me know if you find the opposite. [1] http://blog.chromium.org/2009/02/irregexp-google-chromes-new-regexp.html http://blog.chromium.org/2009/02/irregexp-google-chromes-new...
- z92 13y ago> Irregexp[1] compiles on V8 byte code I was thinking: but V8 doesn't have byte code. Then opened the link: "After optimization we generate native machine code which uses backtracking to try different alternatives."
- pmelendez 13y agoWell I might got it wrong but I got the bytecode reference from here: http://v8.googlecode.com/svn/trunk/src/interpreter-irregexp.cc http://v8.googlecode.com/svn/trunk/src/interpreter-irregexp.... Skimming the code it does look like they are interpreting byte code from Irregexp but I would need to read further to say it for sure.
- ahomescu1 13y agoIt's not JavaScript bytecode, but regex bytecode. V8 parses regular expressions to regex ASTs, takes them through some more intermediate forms, then finally to a special regex bytecode. That bytecode is either interpreted, or JIT compiled (depending of some build options, I think). You can look at a list of bytecodes in [1], and at the x64 JIT in [2]. 1 - http://code.google.com/p/v8/source/browse/trunk/src/bytecodes-irregexp.h http://code.google.com/p/v8/source/browse/trunk/src/bytecode... 2 - http://code.google.com/p/v8/source/browse/trunk/src/x64/regexp-macro-assembler-x64.cc http://code.google.com/p/v8/source/browse/trunk/src/x64/rege...
- aegiso 13y agoThat's true. But it does show how good V8 is at optimizing out the JS part of JS when it's irrelevant to the computation, and it shows how well-optimized the regexps in V8 are. None of this is terribly surprising given V8's use case, but only a few years back this would have been a ridiculous result. I enjoyed taking a minute to reflect on that.
- rayiner 13y agoNo, V8's regex library is not written in C. V8 compiles its regular expressions to machine code at runtime. The availability of the compiler at runtime can be a liability for things like startup time or memory use, but can also be a key advantage in situations like regular expression parsing or packet processing.
- debacle 13y agoWell it's not written in JavaScript, so as the OP said it's either C or C++.
- skybrian 13y agoRegular expressions are part of the JavaScript language, so they're as much "written in JavaScript" as any other language construct. The point is that a regular expression compiled by V8 (which is not written in JavaScript) is faster than a regular expression as compiled by some particular C compiler. Of course a C compiler could possibly be written to understand calls to a standard regexp API and compile those to binary.
- rayiner 13y agoThe code that's doing the actual matching is not written in C or C++. It's an internal regex bytecode compiled directly to machine code.
- revelation 13y agoYou need a regex library to turn a regex into machine code. What the parent means is that the V8 regex library compiles them, while the one used in the C code merely interprets the regex. (The C and C++ thing is just adding confusion here)
- ori_b 13y agoThis is actually a JIT compiled regex beating an interpreted one. And it's beating it by a far smaller margin than I would have expected, honestly.
- Tloewald 13y agoReading through the responses to this comment, the upshot w.r.t regex performance is that it's a significant advantage to have native regex support in a language. Because C doesn't the regex ultimately has to be translated into C at parse time, whereas with Javascript the regex is converted to machinecode at run time. This is similar to the advantage of abstraction layers in general, although there's a cost if you create abstraction layers for things no-one much cares about.