On 03/16/12 01:03, H. S. Teoh wrote: > 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 <f...@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.
x86 xchg w/ mem ref implies lock, plus you'd have to load and store one of the operands anyway. artur