Re: Avoid gratuitous closure allocations

2019-09-20 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, September 20, 2019 5:21:22 AM MDT Ali Çehreli via Digitalmars-d-
learn wrote:
> tl;dr Instead of returning an object that uses local state, return an
> object that uses member variables.

The other issue this helps with is problems related to having multiple
contexts. IIRC, without it, some predicates don't work due to the compiler
complaining about there being more than one context. However, I pretty much
always use static structs for ranges though, so I haven't run into the issue
recently enough to recall the exact details. In general though, I'd say that
it's best pratice to use static structs in functions and avoid needing local
context. Sometimes, it makes sense to do so, but in general, giving your
struct access to the local scope in the function is going to result in
closures being allocated when they could have easily been avoided.

- Jonathan M Davis






Re: Avoid gratuitous closure allocations

2019-09-20 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Sep 20, 2019 at 01:03:29PM +, Andrea Fontana via 
Digitalmars-d-learn wrote:
> On Friday, 20 September 2019 at 11:21:22 UTC, Ali Çehreli wrote:
> > tl;dr Instead of returning an object that uses local state, return
> > an object that uses member variables.
> 
> Really good to know tips!

In fact, most (all?) of Phobos code is written this way.


T

-- 
Questions are the beginning of intelligence, but the fear of God is the 
beginning of wisdom.


Re: Avoid gratuitous closure allocations

2019-09-20 Thread Andrea Fontana via Digitalmars-d-learn

On Friday, 20 September 2019 at 11:21:22 UTC, Ali Çehreli wrote:
tl;dr Instead of returning an object that uses local state, 
return an object that uses member variables.


Really good to know tips!