foreach vs. iterators

Walter Bright newshound at digitalmars.com
Sat Nov 4 01:17:09 PST 2006


Oskar Linde wrote:
> Walter Bright wrote:
> 
>> You're right about that being a problem with opApply.
> 
> Does it have to be?
> 
> If the body of the opApply is available to the compiler and the delegate
> parameter is a compile time constant (as it is in the normal foreach case),
> does anything prevent the compiler from inlining the opApply together with
> the delegate body?
> 
> Would there still be a speed hit if this was done?
> 
> I have many cases that would gain a lot if the compiler inlined functions
> taking compile time known delegates together with the delegate call. I hope
> there is nothing fundamental preventing this from happening.

The compiler doesn't inline functions that have loops in them. This 
isn't a fault in D, but a structural limitation in the way the dmd 
compiler works.

I find opApply particularly slick for things like recursive directory 
traversals, associative array walking, etc. But for ordinary linear 
collection types, it is hard to generate code as good from it as pointer 
stepping.



More information about the Digitalmars-d-announce mailing list