Does D have too many features?

Jonathan M Davis jmdavisProg at gmx.com
Sat Apr 28 16:51:23 PDT 2012


On Sunday, April 29, 2012 01:42:05 bearophile wrote:
> Jonathan M Davis:
> >> - 'in' operator returning a pointer to the element.
> > 
> > Really? I use that all the time with AAs. Without that, you
> > would have to do
> > two lookups to get an object which might not be in the
> > container, so it would be less efficient.
> 
> LDC1 compiler introduced a small optimization, it looks for
> nearby associative array lookups and removes the second of them
> where possible. In my code I have seen this frees me to use "in"
> nearby followed by [], with the same efficiency of a single AA
> lookup. With this small optimization, D "in" is free to return a
> more clean boolean.

I really don't see anything "unclean" about returing a pointer. It does the 
job fantastically, and you can use it in conditional expressions just like you 
would use a bool. Also, while it may work for the most part, I'd be very 
skeptical about the compiler being able to optimize out lookups as well as 
simply using in would - especially in cases where you don't immediately use 
the pointer.

> This optimization doesn't work if you want to store the pointer
> returned by "in" to use it later, but in my code this pattern
> doesn't happen, I think it's quite uncommon.

Storing it for later use within the function is quite common. Storing it in a 
member variable or the like should be _very_ uncommon, since it's almost 
always a bad idea. But even just storing it in a local variable to use later 
could destroy the locality enough to defeat LDC's optimization.

- Jonathan M Davis


More information about the Digitalmars-d mailing list