News for hackers. Stuff that matters.
Re:Swapping two blocks of memory inside a larger block, in c... by praptak on Tuesday January 06, 2026 @11:50AM (#46514762)
I think this was discussed in Jon Bentley "programming pearls"?

Also in the same book it was mentioned that the disjoint cycles method (also mentioned in the article) was worse for paging/caching than the three reverses method.

Re:Swapping two blocks of memory inside a larger block, in c... by ot on Tuesday January 06, 2026 @12:01PM (#46514936)
That's probably true for small primitive types, but if your objects are expensive to move (like a large struct) it might be beneficial to minimize swaps.
Re:Swapping two blocks of memory inside a larger block, in c... by praptak on Tuesday January 06, 2026 @02:20PM (#46517171)
Yeah, it might be interesting to run some profiling of both algorithms and see how they perform dependent on the size of the blocks being swapped (which doesn't even have to be equal to the size of the object in the array).
Re:Swapping two blocks of memory inside a larger block, in c... by taeric on Tuesday January 06, 2026 @01:46PM (#46516662)
It is discussed in that book. Very fun read, all told. Highly recommended if folks find this sort of thing fun. I think I should thumb through it again. :D
Re:Swapping two blocks of memory inside a larger block, in c... by jnellis on Tuesday January 06, 2026 @01:54PM (#46516779)
Java chooses to use the cycle method mostly. They also reference Bentley.

https://github.com/openjdk/jdk/blob/f1e0e0c25ec62a543b9cbfab...