Skip to content

Commit

Permalink
optimize string parse (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
jl2922 authored Feb 9, 2018
1 parent 58ec884 commit 8f36c0f
Showing 1 changed file with 3 additions and 18 deletions.
21 changes: 3 additions & 18 deletions src/basic_type/string_serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,15 @@ class Serializer<std::string, B> {
static void serialize(const std::string& str, OutputBuffer<B>& ob) {
const size_t n_bytes = str.size();
Serializer<size_t, B>::serialize(n_bytes, ob);
ob.write(str.c_str(), n_bytes);
ob.write(str.data(), n_bytes);
}

static void parse(std::string& str, InputBuffer<B>& ib) {
size_t n_bytes;
Serializer<size_t, B>::parse(n_bytes, ib);
char buf[PARSE_BUFFER_SIZE];
str.clear();
str.reserve(n_bytes);
while (n_bytes > 0) {
if (n_bytes > PARSE_BUFFER_SIZE) {
ib.read(buf, PARSE_BUFFER_SIZE);
str.append(buf, PARSE_BUFFER_SIZE);
n_bytes -= PARSE_BUFFER_SIZE;
} else {
ib.read(buf, n_bytes);
str.append(buf, n_bytes);
n_bytes = 0;
}
}
str.resize(n_bytes);
ib.read(&str[0], n_bytes);
}

private:
constexpr static size_t PARSE_BUFFER_SIZE = 1 << 10;
};

} // namespace hps
Expand Down

0 comments on commit 8f36c0f

Please sign in to comment.