Live data from Hacker News

Compile time regular expression in C++

github.com

21–30 of 30 posts

Re: Compile time regular expression in C++

#22
It's a good library but be careful as it can significantly increase compile times. I added a couple of reasonably long regular expressions to a c++ source file and it increased the compile time from near instant (below to 1 second) to 30 seconds. Might be wise to move these to dedicated source files so you don't pay this penalty each time you make changes.

Re: Compile time regular expression in C++

#24
post #21

At the risk of sounding like an idiot, what is the benefit of being able to match at compile time?

One of the stated benefits is that the compiler optimizer can optimize the regex state machine that gets created, resulting in significantly faster match times at runtime. There are some benchmarks if you look up her cppcon talk about the library iirc.

Edit: here is her talk, at 39 minutes she shares the benchmarks. https://m.youtube.com/watch?v=QM3W36COnE4

Re: Compile time regular expression in C++

#26
post #20

Boost Xpressive has had static/"compile time" regex in C++ since the mid 2000s https://www.boost.org/doc/libs/1_83_0/doc/html/xpressive.htm... A performance comparison would be interesting.

I had great results with this back in the day. The template meta programming DSL you had to use was pretty horrifying but it was a triumph of what you could do with template metaprogramming (which was something discovered, not designed, in C++).

The first compile time regular expressions I saw that just used normal regular expressions was D's CTRE which produced an even faster "engine" than Boost Xpressive. This was thanks to D's compile time function evaluation reaching a point something like this was possible.

I just started using this CTRE for a regular expression whose performance had become problematic and I'm very impressed with it so far. It's pretty easy to surpass std::regex but not sacrificing usability was surprising. Build times haven't been affected too much either (for my use case).

Re: Compile time regular expression in C++

#27
post #19

Earlier quoted context omitted.

I've never used cl-ppcre myself, but its docs[1] claim that it provides compile-time regexes: > CL-PPCRE uses compiler macros to pre-compile scanners at load time if possible. This happens if the compiler can determine that the regular expression (no matter if it's a string or an S-expression) is constant at compile time and is intended to save the time for creating scanners at execution time (probably creating the s…

I think this is more like caching the regex than creating it at compile time? Load time I think is basically runtime. I think lisp can be loaded and then rehydrated later, but I'm not sure how common that is.

Hard to say exactly since I don't have experience with cl-ppcre, but this line seems to suggest something is actually happening at complile time:

> This happens if the compiler can determine that the regular expression (no matter if it's a string or an S-expression) is constant at compile time

If this were just a run of the mill caching mechanism, then whether the pattern was a constant at compile time wouldn't matter.

Re: Compile time regular expression in C++

#28

I'd love for someone to add this to rebar[1] so that we can get a good sense of how well it does against other general purpose regex engines. It will be a little tricky to add (since the build step will require emitting a C++ program and compiling it), but it should be possible. [1]: https://github.com/BurntSushi/rebar

The C++ standard library "general regex engine" is crap (at speed), so there's no competition on that front at least...

I know. I'm not really looking to compare it with C++'s standard library regex engine. (Although if someone wanted to do that work, I would welcome it into the benchmark.)

There are other engines written in C++ (in part or in whole) in rebar. And it is useful to compare it with engines not written in C++ too.

Post reply on HN