Skip to content

Commit

Permalink
Use IOBuf::fromString instead of re-implementing it
Browse files Browse the repository at this point in the history
Reviewed By: avalonalex

Differential Revision: D63992777

fbshipit-source-id: caa452129a22ac650754f7bec668e97de474fdca
  • Loading branch information
TJ Yin authored and facebook-github-bot committed Oct 13, 2024
1 parent fbc1ced commit 4408523
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 22 deletions.
22 changes: 6 additions & 16 deletions thrift/lib/cpp2/op/detail/ValuePatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -367,24 +367,14 @@ class BinaryPatch : public BaseStringPatch<Patch, BinaryPatch<Patch>> {
using Base = BaseStringPatch<Patch, BinaryPatch>;
using T = typename Base::value_type;

static folly::IOBuf stringToIobuf(std::string s) {
std::string* p = new std::string(std::move(s));
return folly::IOBuf(
folly::IOBuf::TAKE_OWNERSHIP,
p->data(),
p->size(),
[](void*, void* userData) {
delete static_cast<std::string*>(userData);
},
static_cast<void*>(p));
}

public:
using Base::apply;
using Base::assign;
using Base::Base;

void assign(std::string s) { assign(stringToIobuf(std::move(s))); }
void assign(std::string s) {
assign(*folly::IOBuf::fromString(std::move(s)));
}

template <typename T>
BinaryPatch& operator=(T&& other) {
Expand Down Expand Up @@ -432,9 +422,9 @@ class BinaryPatch : public BaseStringPatch<Patch, BinaryPatch<Patch>> {
}

void apply(std::string& val) const {
folly::IOBuf buf = stringToIobuf(std::move(val));
apply(buf);
val = buf.to<std::string>();
auto buf = folly::IOBuf::fromString(std::move(val));
apply(*buf);
val = buf->toString();
}

private:
Expand Down
7 changes: 1 addition & 6 deletions thrift/lib/cpp2/test/ProtocolBench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,7 @@ using GetWriter = typename SerializerTraits<T>::Writer;
struct FrozenSerializer {
template <class T>
static void serialize(const T& obj, folly::IOBufQueue* out) {
auto p = new string(frozen::freezeToString(obj));
out->append(folly::IOBuf::takeOwnership(
p->data(),
p->size(),
[](void*, void* p) { delete static_cast<string*>(p); },
static_cast<void*>(p)));
out->append(folly::IOBuf::fromString(frozen::freezeToString(obj)));
}
template <class T>
static size_t deserialize(folly::IOBuf* iobuf, T& t) {
Expand Down

0 comments on commit 4408523

Please sign in to comment.