Avoid additional memory copy and allocation by using swap