Array!T and find are slow

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed May 14 21:31:16 PDT 2014


On Thu, 15 May 2014 01:29:23 +0000
Kapps via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com> wrote:

> On Wednesday, 14 May 2014 at 23:50:34 UTC, Meta wrote:
> > On the topic of lazy, why *is* it so slow, exactly? I thought
> > it was just shorthand for taking a function that evaluates the
> > expression, and wrapping said expression in that function at
> > the call site. That is, I thought that:
> >
> > int doSomething(lazy int n)
> > {
> >     return n();
> > }
> >
> > Was more or less equivalent to:
> >
> > int doSomething(int function(int) n)
> > {
> >     return n();
> > }
>
> It's more equivalent to:
>
> int doSomething(int delegate(int) n)
> {
>      return n();
> }
>
> And (I could be very likely wrong here), I believe that it's
> expensive because it's not scope and possibly requires a closure.
> Again, very likely may be wrong.

Yeah. It generates a delegate. You even use the value internally as a
delegate. So, that's definitely part of the problem, though IIRC, there were
other issues with it. However, I don't remember at the moment. The big one
IIRC (which may be due to its nature as a delegate) is simply that it can't be
inlined, and in many cases, you very much what the code to be inlined (enforce
would be a prime example of that).

enforce(cond, "failure");

really should just translate to something close to

if(!cond) throw new Exception("failure");

but it doesn't do anything close to that. And as long as it doesn't, enforce
is of questionable value in any code that cares about efficiency.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list