marisa-trie icon indicating copy to clipboard operation
marisa-trie copied to clipboard

KeySet::push_back: Use memcpy to copy

Open jmr opened this issue 4 years ago • 1 comments

Replace for loop.

This should at least not be slower, and is probably faster. I didn't benchmark it.

For full C++ style points, this could be std::copy_n(), but that seems unnecessary.

jmr avatar Jun 18 '20 15:06 jmr

For larger types, copy_n may be faster if the toolchain provides specialization that take advantage of type alignment (libstdc++ and libc++ don't seem to) and figure out that the ranges don't overlap.

As we're copying char here, memcpy should always be at least as fast as std::copy(_n).

Update: At least GCC seems to be aware of memcpy semantics and takes advantage of the type alignment (even though the argument is void *). Or maybe this happens at a lower level in the compiler, I didn't dig that deep.

glebm avatar Jun 20 '20 23:06 glebm