Some loops are replaced with std::copy_n for the sake of better worst-case performance.