s-yata / marisa-trie

MARISA: Matching Algorithm with Recursively Implemented StorAge
Other
506 stars 89 forks source link

KeySet::push_back: Use memcpy to copy #30

Open jmr opened 4 years ago

jmr commented 4 years ago

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.

glebm commented 4 years ago

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.