From fe2a233b4d466dd8a081100a2bb1ce1a28b92f5f Mon Sep 17 00:00:00 2001 From: Jesse Rosenstock Date: Thu, 18 Jun 2020 11:21:14 -0400 Subject: [PATCH 1/2] KeySet::push_back: Use memmove to copy 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. --- lib/marisa/keyset.cc | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/lib/marisa/keyset.cc b/lib/marisa/keyset.cc index 41354f7..d3a89c6 100644 --- a/lib/marisa/keyset.cc +++ b/lib/marisa/keyset.cc @@ -1,3 +1,4 @@ +#include #include #include "marisa/keyset.h" @@ -14,9 +15,7 @@ void Keyset::push_back(const Key &key) { MARISA_DEBUG_IF(size_ == MARISA_SIZE_MAX, MARISA_SIZE_ERROR); char * const key_ptr = reserve(key.length()); - for (std::size_t i = 0; i < key.length(); ++i) { - key_ptr[i] = key[i]; - } + std::memmove(key_ptr, key.ptr(), key.length()); Key &new_key = key_blocks_[size_ / KEY_BLOCK_SIZE][size_ % KEY_BLOCK_SIZE]; new_key.set_str(key_ptr, key.length()); @@ -33,9 +32,7 @@ void Keyset::push_back(const Key &key, char end_marker) { } char * const key_ptr = reserve(key.length() + 1); - for (std::size_t i = 0; i < key.length(); ++i) { - key_ptr[i] = key[i]; - } + std::memmove(key_ptr, key.ptr(), key.length()); key_ptr[key.length()] = end_marker; Key &new_key = key_blocks_[size_ / KEY_BLOCK_SIZE][size_ % KEY_BLOCK_SIZE]; @@ -62,9 +59,7 @@ void Keyset::push_back(const char *ptr, std::size_t length, float weight) { MARISA_THROW_IF(length > MARISA_UINT32_MAX, MARISA_SIZE_ERROR); char * const key_ptr = reserve(length); - for (std::size_t i = 0; i < length; ++i) { - key_ptr[i] = ptr[i]; - } + std::memmove(key_ptr, ptr, length); Key &key = key_blocks_[size_ / KEY_BLOCK_SIZE][size_ % KEY_BLOCK_SIZE]; key.set_str(key_ptr, length); From 31b2336b1aac6430de464856a4dd9a8fee35c243 Mon Sep 17 00:00:00 2001 From: Jesse Rosenstock Date: Fri, 19 Jun 2020 03:54:13 -0400 Subject: [PATCH 2/2] Use memcpy instead of memmove I got these confused and the regions do not overlap. --- lib/marisa/keyset.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/marisa/keyset.cc b/lib/marisa/keyset.cc index d3a89c6..03e621b 100644 --- a/lib/marisa/keyset.cc +++ b/lib/marisa/keyset.cc @@ -15,7 +15,7 @@ void Keyset::push_back(const Key &key) { MARISA_DEBUG_IF(size_ == MARISA_SIZE_MAX, MARISA_SIZE_ERROR); char * const key_ptr = reserve(key.length()); - std::memmove(key_ptr, key.ptr(), key.length()); + std::memcpy(key_ptr, key.ptr(), key.length()); Key &new_key = key_blocks_[size_ / KEY_BLOCK_SIZE][size_ % KEY_BLOCK_SIZE]; new_key.set_str(key_ptr, key.length()); @@ -32,7 +32,7 @@ void Keyset::push_back(const Key &key, char end_marker) { } char * const key_ptr = reserve(key.length() + 1); - std::memmove(key_ptr, key.ptr(), key.length()); + std::memcpy(key_ptr, key.ptr(), key.length()); key_ptr[key.length()] = end_marker; Key &new_key = key_blocks_[size_ / KEY_BLOCK_SIZE][size_ % KEY_BLOCK_SIZE]; @@ -59,7 +59,7 @@ void Keyset::push_back(const char *ptr, std::size_t length, float weight) { MARISA_THROW_IF(length > MARISA_UINT32_MAX, MARISA_SIZE_ERROR); char * const key_ptr = reserve(length); - std::memmove(key_ptr, ptr, length); + std::memcpy(key_ptr, ptr, length); Key &key = key_blocks_[size_ / KEY_BLOCK_SIZE][size_ % KEY_BLOCK_SIZE]; key.set_str(key_ptr, length);