Compiler analysis of single-use types? Escape analysis of types?

Dicebot via Digitalmars-d digitalmars-d at puremagic.com
Wed Aug 17 17:59:43 PDT 2016


On 08/18/2016 12:25 AM, Ali Çehreli wrote:
> I'm wondering whether there is such a thing as single-use of a type in
> compiler technology. I think if the compiler could determine that a type
> is used only once, it could apply optimizations.
> 
> A colleague of mine raised the issue of D's use of the GC even for
> seemingly local delegates. For example, even though everything remains
> local for the following lambda, countAbove() cannot be @nogc:
> 
> auto countAbove(int[] a, int limit) {
>     return a.filter!(x => x >= limit).count();
> }
> 
> The reason is due to the fact that filter() returns a struct object that
> takes the delegate as an alias template parameter. Here is a reduction
> of the issue with my understanding in comments:

I believe actual reason is that aliased lambda has to allocate a closure
because it refers to stack local variable (limit). This compiles just fine:

auto countAbove(int[] a, int limit) @nogc {
    return a.filter!(x => x >= 1).count();
}

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: OpenPGP digital signature
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20160818/91bc2069/attachment.sig>


More information about the Digitalmars-d mailing list