Bye bye, fast compilation times

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Feb 6 19:07:07 UTC 2018


On Tue, Feb 06, 2018 at 05:35:44AM +0000, Dmitry Olshansky via Digitalmars-d wrote:
> On Tuesday, 6 February 2018 at 04:35:42 UTC, Steven Schveighoffer wrote:
> > On 2/5/18 11:09 PM, psychoticRabbit wrote:
[...]
> > > ----
> > > import std.net.isemail;
> > > 
> > > void main()
> > > {
> > >      auto checkEmail = "someone at somewhere.com".isEmail();
> > > }
> > > ----
> > 
> > I was surprised at this, then I looked at the first line of isEmail:
> > 
> >     static ipRegex =
> > ctRegex!(`\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}`~
> > 
> > `(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$`.to!(const(Char)[]));
> > 
> > So it's really still related to regex.

Yeah, ctRegex is a bear at compile-time.  Why can't we just use a
runtime regex?  It will at least take "only" 3 seconds to compile. :-D
Or just don't use a regex at all.


> That’s really bad idea - isEmail is template so the burden of freaking
> slow ctRegex is paid on per instantiation basis. Could be horrible
> with separate compilation.
[...]

I'm not sure I'm seeing the value of using ctRegex here.  What's wrong
with a module static runtime regex initialized by a static this()?

And before anyone complains about initializing the regex if user code
never actually uses it, it's possible to use static this() on an
as-needed basis:

	template ipRegex()
	{
		// Eponymous templates FTW!
		Regex!char ipRegex;

		static this()
		{
			ipRegex = regex(`blah blah blah`);
		}
	}

	auto isEmail(... blah blah ...)
	{
		...
		if (ipRegex.match(...)) ...
		...
	}

Basically, if `ipRegex` is never referenced, the template is never
instantiated and the static this() basically doesn't exist. :-D
Pay-as-you-go FTW!


T

-- 
If you want to solve a problem, you need to address its root cause, not just its symptoms. Otherwise it's like treating cancer with Tylenol...


More information about the Digitalmars-d mailing list