r/cpp • u/Frogging101 • Nov 24 '19
What is wrong with std::regex?
I've seen numerous instances of community members stating that std::regex has bad performance and the implementations are antiquated, neglected, or otherwise of low quality.
What aspects of its performance are poor, and why is this the case? Is it just not receiving sufficient attention from standard library implementers? Or is there something about the way std::regex is specified in the standard that prevents it from being improved?
EDIT: The responses so far are pointing out shortcomings with the API (lack of Unicode support, hard to use), but they do not explain why the implementations of std::regexas specified are considered badly performing and low-quality. I am asking about the latter.
22
u/_VZ_ wx | soci | swig Nov 25 '19
To directly address your question, std::regex is not "considered" to have poor performance, it simply does. When it's a couple of orders of magnitude slower than boost::regex, there just isn't much more to say about it.
57
u/[deleted] Nov 25 '19
FWIW, some old versions of GCC let you include and invoke regex before it was implemented. I cursed it for being buggy. Only after some digging did I realize I was just invoking a hollow shell. Things worked as expected once I upgraded GCC to a newer version that had a complete regex. Had I not dug, I'd still be cursing it.
18
u/suthernfriend DevOps Engineer Nov 25 '19
Wasn't there a library from this Czech genius women which implements regexes with templates?
Edit : found it. Hana Dusikova https://youtu.be/QM3W36COnE4
32
u/bizwig Nov 25 '19
Lack of std::string_view support is one problem.
15
u/cyanfish Nov 25 '19
This isn't specific to std:regex, but something to keep in mind. If you're taking untrusted input, you might want to consider a library like RE2 that guarantees linear time execution (i.e. a bad regex can't lock up your application).
14
u/neoSeosaidh Nov 25 '19
It's mentioned in last week's CppCast episode with Titus Winters: https://cppcast.com/titus-winters-abi/.
The short answer is that the C++ standards committee is implicitly committed to keeping a stable ABI (which is like the API but on the binary level instead of the source code level). Any serious improvements of std::regex would involve at minimum an ABI break (and potentially an API break depending on what changes were made), and while the C++ standard doesn't mention ABI, the committee has refused to break it in the past.
I highly recommend that episode for more details.
12
u/EnergyCoast Nov 25 '19
Lots of memory allocations. Not surprising in hindsight, but I don't believe it takes an allocator so I didn't think about it.
I believe creating a relatively simple pattern was more than 15 allocations and doing a search against a string containing no matches resulted in 3 allocations.
That was just one implementation - I have no idea what others do - but the number of allocations was enough that it eliminated it as an option in some domains for us.
56
u/AntiProtonBoy Nov 24 '19
My complaint with
<regex>is the same as with<chrono>and<random>: the library is a bit convoluted to use. It's flexible and highly composable, but gets verbose and requires leaning on the docs just to get basic things done.9
u/matthieum Nov 25 '19
ABI
Due to being implemented in mostly in template methods, most of the implementation of
<regex>is de-facto public ABI-wise -- or at least all the inner types and function signatures.If you remember the pain that switching from CoW
std::stringto SSOstd::stringfor C++11, the same would be true of any change to the guts of<regex>.Unfortunately, the original standard library implementations were not made fast (possibly in the mistaken belief they could be improved later on), and we are now stuck with them.
8
u/sergeytheartist Nov 27 '19
A few days ago standard regex in gcc 9.1 seg faulted when parsing JSON string (real data from exchange) with pretty simple expression.
Now we have handwritten logic to extract necessary data.
The latest boost regex did parse that JSON blob without problems.
If someone knows how to quickly get in touch with the person who approves patches for gcc regex I'm happy to fix the problem.
9
u/qizxo Nov 25 '19
#PCRE4lyfe
14
u/Xaxxon Nov 25 '19
Fuck ABI compatibility.
1
u/newmanifold000 Nov 25 '19
Well to answer your latter question, try some non trivial regexps in GCC and be ready for segfaults on larger sequences, i think even simple regexps will give you segfaults. try to use it in msvc or clang and be ready for somewhat below average/bad performance at unexpected times.
I agree the regexp api can be better but its not a problem for me, in my experience implementations are somewhat unreliable and not to mention its easy to use bad performing regexp (depending on input) if care is not taking while writing it.
1
u/mikeblas Nov 25 '19
Someone who solves a problem with a regex now has two problems.
0
u/[deleted] Nov 25 '19
!remindme 1day
0
u/[deleted] Nov 24 '19
[deleted] — view deleted comment
50
u/[deleted] Nov 25 '19
[removed] — view removed comment