Regex matching cause lots of _d_arrayliteralTX calls

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Sep 27 08:20:51 PDT 2013


On Fri, Sep 27, 2013 at 04:52:20PM +0200, JR wrote:
> On Friday, 27 September 2013 at 14:37:05 UTC, Dmitry Olshansky
> wrote:
> >27-Sep-2013 02:00, JR пишет:
> 
> >And the answer is - don't use ENUM with ctRegex.  The problem is that
> >ctRegex returns you a pack of datastructures (=arrays).  Using them
> >with enum makes it behave as if you pasted them as array literals and
> >these do allocate each time.
> >
> >TL;DR: use static and/or auto with ctRegex not enum.
> 
> That fixed it. Thank you both for your help!
> 
> 
> (I was of the notion that that enum merely translate to
> compile-time-evaluated constants.)

It does do that, yes. But the semantics aren't *quite* what one might
expect (I was a victim of this misconception too :-P). What it does is
to, in effect, "copy-n-paste" the value of the enum into whatever code
uses it, every time. That means that if you write:

	enum x = "abc";
	void fun() { auto y = x; }
	void gun() { auto z = x; }

It will *copy* "abc" into y and z (rather than just aliasing the same
underlying string), *each time* you call fun() and gun().

TL;DR: use immutable instead of enum for array-based constants:

	immutable x = "abc";
	void fun() { auto y = x; } // now y is just an alias of x
	void gun() { auto z = x; } // now z is just an alias of x


T

-- 
Debian GNU/Linux: Cray on your desktop.


More information about the Digitalmars-d-learn mailing list