How to avoid ctRegex (solved)
cy via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Aug 21 13:06:49 PDT 2016
At seconds PER (character range) pattern, ctRegex slows down
compilation like crazy, but it's not obvious how to avoid using
it, since Regex(Char) is kind of weird for a type. So, here's
what I do. I think this is right.
in the module scope, you start with:
auto pattern = ctRegex!"foobar";
and you substitute with:
typeof(regex("")) pattern;
static this() {
pattern = regex("foobar");
}
That way you don't have to worry about whether to use a
Regex!char, or a Regex!dchar, or a Regex!ubyte. It gives you the
same functionality, at the cost a few microseconds slowdown on
running your program. And once you're done debugging, you can
always switch back, so...
string defineRegex(string name, string pattern)() {
import std.string: replace;
return q{
debug {
pragma(msg, "fast $name");
import std.regex: regex;
typeof(regex("")) $name;
static this() {
$name = regex(`$pattern`);
}
} else {
pragma(msg, "slooow $name");
import std.regex: ctRegex;
auto $name = ctRegex!`$pattern`;
}
}.replace("$pattern",pattern)
.replace("$name",name);
}
mixin(defineRegex!("naword",r"[\W]+"));
mixin(defineRegex!("alnum",r"[a-zA-Z]+"));
mixin(defineRegex!("pattern","foo([a-z]*?)bar"));
mixin(defineRegex!("pattern2","foobar([^0-9z]+)"));
void main() {
}
/*
$ time rdmd -release /tmp/derp.d
slooow naword
slooow alnum
slooow pattern
slooow pattern2
slooow naword
slooow alnum
slooow pattern
slooow pattern2
rdmd -release /tmp/derp.d 17.57s user 1.57s system 82% cpu
23.210 total
$ time rdmd -debug /tmp/derp.d
fast naword
fast alnum
fast pattern
fast pattern2
fast naword
fast alnum
fast pattern
fast pattern2
rdmd -debug /tmp/derp.d 2.92s user 0.37s system 71% cpu 4.623
total
*/
...sure would be nice if you could cache precompiled regular
expressions as files.
More information about the Digitalmars-d-learn
mailing list