Multiple return values...

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Mar 15 17:03:49 PDT 2012


On Fri, Mar 16, 2012 at 12:11:44AM +0100, Simen Kjærås wrote:
> On Thu, 15 Mar 2012 23:44:09 +0100, foobar <foo at bar.com> wrote:
> 
> >Is swap usually inlined by the compiler?
> 
> This is the body of swap for simple types:
> 
>   auto tmp = lhs;
>   lhs = rhs;
>   rhs = tmp;
> 
> For more complex types:
> 
>   ubyte[T.sizeof] t = void;
>   auto a = (cast(ubyte*) &lhs)[0 .. T.sizeof];
>   auto b = (cast(ubyte*) &rhs)[0 .. T.sizeof];
>   t[] = a[];
>   a[] = b[];
>   b[] = t[];
> 
> So yeah. If that's not inlined, something's wrong.

Ideally, though, a swap of large objects should be a single loop of xchg
instructions (in x86 anyway, xchg may not exist on other architectures).
Unless dmd is much more clever than I thought, the above code would
generate 3 loops, which uses more memory and is (probably) slower.


T

-- 
Never ascribe to malice that which is adequately explained by incompetence. -- Napoleon Bonaparte


More information about the Digitalmars-d mailing list