Ideas from the Chapel language (swap)

Bruce Adams tortoise_74 at yeah.who.co.uk
Thu Oct 4 01:28:38 PDT 2007


David Brown Wrote:

> On Wed, Oct 03, 2007 at 06:25:08PM -0700, Gregor Richards wrote:
> 
> > For the ridiculously-insane:
> >
> > void swap(T)(ref T a, ref T b)
> > {
> >     synchronized {
> >         // this should be some kind of static for ...
> >         for (size_t i = 0; i < (a.sizeof/size_t.sizeof); i++) {
> >             (cast(size_t*) &a)[i] ^= (cast(size_t*) &b)[i];
> >             (cast(size_t*) &b)[i] = (cast(size_t*) &a)[i] ^ (cast(size_t*) 
> > &b)[i];
> >             (cast(size_t*) &a)[i] ^= (cast(size_t*) &b)[i];
> >         }
> >     }
> > }
> >
> > Add some loop unrolling and that's more efficient than memcpy :P
> 
> And rather devestating if another thread causes a garbage collection.
> Doesn't the synchronized just lock this section of code, not the whole
> program?
> 
> But, why would it be any more efficient than the inside of the loop just
> saying:
> 
>    size_t tmp = ((cast(size_t*) &a)[i];
>    ((cast(size_t*) &a)[i] = ((cast(size_t*) &b)[i];
>    ((cast(size_t*) &b)[i] = tmp;
> 
> This does 2 reads and 2 writes.  The xor version does 4 reads and 3 writes.
> How could it be faster?
> 
> The xor trick really only helps you if you are one architecture where you
> don't have a register to spare.  Shuffling through a register is going to
> be much faster than multiple reads and writes to memory.
> 
> David

I think this is very much implementation defined.
If you are operating on a block of memory rather than data small
enough to fit in a register you have to dereference memory anyway.
And lets not forget platforms with some sort of exchange registers as a primative operation.
Though, I dislike joining the army of people rushing to suggest adding more and more (often unnecessary) features to an already feature rich language, I think that it is implementation defined justifies it being a low level operator implemented by the compiler. Swap is primative enough to have an unambiguous meaning and unlikely to confuse noobs. I'd very much like to hear what a (the) compiler writer has to say on the subject (especially in light of transfer constructors). I wonder if Walter is reading?

Regards,

Bruce.



More information about the Digitalmars-d mailing list