Regex matching cause lots of _d_arrayliteralTX calls

monarch_dodra monarchdodra at gmail.com
Fri Sep 27 14:26:55 PDT 2013


On Friday, 27 September 2013 at 15:22:14 UTC, H. S. Teoh wrote:
> 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().

I think strings are an actual exception to this rule, what with 
being immutable and all (provided you place the info in a string 
of course "char[] ss = s" will allocate of course).

All other types, even immutable (AFAIK) will allocate though, yes.


More information about the Digitalmars-d-learn mailing list