aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/yson_string
diff options
context:
space:
mode:
authorbabenko <babenko@yandex-team.com>2023-01-20 22:13:39 +0300
committerbabenko <babenko@yandex-team.com>2023-01-20 22:13:39 +0300
commit991ef42e20b115accbc9d76e27c5c220d1c37095 (patch)
tree8a9e2ed1add9edfa4c2350be187c6859ae1631af /library/cpp/yt/yson_string
parent980dc47153689172f8869a2a0d8dfe1014b2d70e (diff)
downloadydb-991ef42e20b115accbc9d76e27c5c220d1c37095.tar.gz
Refactor and improve TSharedRangeHolder
Diffstat (limited to 'library/cpp/yt/yson_string')
-rw-r--r--library/cpp/yt/yson_string/string-inl.h29
-rw-r--r--library/cpp/yt/yson_string/string.cpp18
2 files changed, 33 insertions, 14 deletions
diff --git a/library/cpp/yt/yson_string/string-inl.h b/library/cpp/yt/yson_string/string-inl.h
index 5c41629cc0..09db98881f 100644
--- a/library/cpp/yt/yson_string/string-inl.h
+++ b/library/cpp/yt/yson_string/string-inl.h
@@ -26,6 +26,35 @@ bool Equals(const TLeft& lhs, const TRight& rhs)
lhs.GetType() == rhs.GetType();
}
+class TYsonStringHolder
+ : public TSharedRangeHolder
+ , public TWithExtraSpace<TYsonStringHolder>
+{
+public:
+ explicit TYsonStringHolder(size_t size)
+ : Size_(size)
+ { }
+
+ char* GetData()
+ {
+ return static_cast<char*>(GetExtraSpacePtr());
+ }
+
+ // TSharedRangeHolder overrides.
+ std::optional<size_t> GetTotalByteSize() const override
+ {
+ return Size_ + sizeof(TYsonStringHolder);
+ }
+
+ static TIntrusivePtr<TYsonStringHolder> Allocate(size_t size)
+ {
+ return NewWithExtraSpace<TYsonStringHolder>(size, size);
+ }
+
+private:
+ const size_t Size_;
+};
+
} // namespace NDetail
inline bool operator == (const TYsonString& lhs, const TYsonString& rhs)
diff --git a/library/cpp/yt/yson_string/string.cpp b/library/cpp/yt/yson_string/string.cpp
index c92d4d6e42..dec428c31e 100644
--- a/library/cpp/yt/yson_string/string.cpp
+++ b/library/cpp/yt/yson_string/string.cpp
@@ -71,23 +71,13 @@ TYsonString::TYsonString()
TYsonString::TYsonString(const TYsonStringBuf& ysonStringBuf)
{
if (ysonStringBuf) {
- struct TCapturedYsonStringPayload
- : public TSharedRangeHolder
- , public TWithExtraSpace<TCapturedYsonStringPayload>
- {
- char* GetData()
- {
- return static_cast<char*>(GetExtraSpacePtr());
- }
- };
-
auto data = ysonStringBuf.AsStringBuf();
- auto payload = NewWithExtraSpace<TCapturedYsonStringPayload>(data.length());
- ::memcpy(payload->GetData(), data.data(), data.length());
- Payload_ = payload;
- Begin_ = payload->GetData();
+ auto holder = NDetail::TYsonStringHolder::Allocate(data.length());
+ ::memcpy(holder->GetData(), data.data(), data.length());
+ Begin_ = holder->GetData();
Size_ = data.Size();
Type_ = ysonStringBuf.GetType();
+ Payload_ = std::move(holder);
} else {
Begin_ = nullptr;
Size_ = 0;