Optimize the implementation of transpose().