Skip to content

Commit d1e385a

Browse files
davidkellyavsej
andauthored
Eliminate looping transform in mcbp_parser::next (#347)
* Eliminate looping transform in mcbp_parser::next I found that in mcbp_session_impl::do_read, 39% of the time was in mcbp::parser::next, with the vast majority of that in the std::transform call after uncompressing the data. By using vector::insert instead, that dropped to only 11% of the time in do:read. Probably the upshot here is to look for everywhere we are filling containers with a back_inserter and considering a way to modify that to not loop and instead copy in blocks. * Same, but for the mcbp_parser::feed call. * similarly, but for generate_binary --------- Co-authored-by: Sergey Avseyev <sergey.avseyev@gmail.com>
1 parent 7b5ae9a commit d1e385a

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

core/io/mcbp_parser.cxx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,24 @@ mcbp_parser::next(mcbp_message& msg)
5151
key_size = static_cast<std::uint32_t>(msg.header.keylen >> 8U);
5252
prefix_size = static_cast<std::uint32_t>(framing_extras_size) + static_cast<std::uint32_t>(msg.header.extlen) + key_size;
5353
}
54-
std::copy(buf.begin() + header_size, buf.begin() + header_size + prefix_size, std::back_insert_iterator(msg.body));
54+
msg.body.insert(msg.body.end(), buf.begin() + header_size, buf.begin() + header_size + prefix_size);
5555

5656
bool is_compressed = (msg.header.datatype & static_cast<std::uint8_t>(protocol::datatype::snappy)) != 0;
5757
bool use_raw_value = true;
5858
if (is_compressed) {
5959
std::string uncompressed;
6060
std::size_t offset = header_size + prefix_size;
6161
if (snappy::Uncompress(reinterpret_cast<const char*>(buf.data() + offset), body_size - prefix_size, &uncompressed)) {
62-
std::transform(uncompressed.begin(), uncompressed.end(), std::back_insert_iterator(msg.body), [](auto ch) {
63-
return static_cast<std::byte>(ch);
64-
});
62+
msg.body.insert(msg.body.end(),
63+
reinterpret_cast<std::byte*>(&uncompressed.data()[0]),
64+
reinterpret_cast<std::byte*>(&uncompressed.data()[uncompressed.size()]));
6565
use_raw_value = false;
6666
// patch header with new body size
6767
msg.header.bodylen = utils::byte_swap(static_cast<std::uint32_t>(prefix_size + uncompressed.size()));
6868
}
6969
}
7070
if (use_raw_value) {
71-
std::copy(buf.begin() + header_size + prefix_size, buf.begin() + header_size + body_size, std::back_insert_iterator(msg.body));
71+
msg.body.insert(msg.body.end(), buf.begin() + header_size + prefix_size, buf.begin() + header_size + body_size);
7272
}
7373
buf.erase(buf.begin(), buf.begin() + header_size + body_size);
7474
if (!buf.empty() && !protocol::is_valid_magic(std::to_integer<std::uint8_t>(buf[0]))) {

core/io/mcbp_parser.hxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct mcbp_parser {
3030
void feed(Iterator begin, Iterator end)
3131
{
3232
buf.reserve(buf.size() + static_cast<std::size_t>(std::distance(begin, end)));
33-
std::copy(begin, end, std::back_insert_iterator(buf));
33+
buf.insert(buf.end(), begin, end);
3434
}
3535

3636
void reset()

core/utils/json.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ class to_byte_vector
9696
void write(tao::binary_view data)
9797
{
9898
buffer_.reserve(buffer_.size() + data.size());
99-
std::copy(data.begin(), data.end(), std::back_inserter(buffer_));
99+
buffer_.insert(buffer_.end(), data.begin(), data.end());
100100
}
101101

102102
void write(std::string_view data)
103103
{
104104
buffer_.reserve(buffer_.size() + data.size());
105-
std::transform(data.begin(), data.end(), std::back_inserter(buffer_), [](auto ch) { return static_cast<std::byte>(ch); });
105+
buffer_.insert(buffer_.end(), reinterpret_cast<const std::byte*>(&data[0]), reinterpret_cast<const std::byte*>(&data[data.size()]));
106106
}
107107

108108
inline void escape(const std::string_view s)

0 commit comments

Comments
 (0)