Bill Kelly
7/27/2007 7:36:00 PM
From: "Peter Seebach" <seebs@seebs.net>
>
> The xor swap is virtually never more efficient on reasonable hardware.
> A casual pass through suggests that, even for the special case of numbers
> where a, b, and a^b are all Fixnum-ranged, the straight swap ought to be
> noticably faster.
>
> More importantly: How could doing it that way ever be idiomatic or easy
> to understand? I cannot conceive of a program where I have to swap two
> objects quickly, and the need to swap them quickly is so great that a hack
> like that would be worth it... and I can't just bypass the swap by improving
> the algorithm.
Incidentally, I do recall one interesting use of this XOR swap
technique.
I believe this is how GUI menus were drawn on the screen on the
Amiga. When you'd click the mouse to get a menu, the system
would allocate off-screen bitmap memory, and render the menu
into the off-screen bitmap. Then it would XOR-swap the off-screen
rendered menu, with the location on the screen where the menu
would be displayed. Thus preserving the pixels from the screen
that would be obscured by the menu, so that the pixels could
be restored when the menu was dismissed.
Pretty neat trick, since it avoided allocating an extra buffer
to store the obscured screen pixels. (And it was fast, because
the Amiga bitmap blitter supported logical raster op's including
XOR, etc.)
Regards,
Bill