Lazy and GC Allocations

Steven Schveighoffer schveiguy at
Mon Feb 20 02:15:32 UTC 2023

On 2/19/23 7:50 PM, Etienne wrote:
> Hello,
> I'm wondering at which moment the following would make an allocation of 
> the scope variables on the GC. Should I assume that the second parameter 
> of enforce being lazy, we would get a delegate/literal that saves the 
> current scope on the GC even if it's not needed? I'm asking purely for a 
> performance perspective of avoiding GC allocations.
> ```
> void main() {
>   int a = 5;
>   enforce(true, format("a: %d", a));
> }
> ```

enforce takes a lazy variable, which I believe is scope by default, so 
no closure should be allocated.

Indeed, you can't really "save" the hidden delegate somewhere, so the 
calling function knows that the delgate can't escape.


More information about the Digitalmars-d-learn mailing list