diff options
author | babenko <babenko@yandex-team.com> | 2023-01-20 22:13:39 +0300 |
---|---|---|
committer | babenko <babenko@yandex-team.com> | 2023-01-20 22:13:39 +0300 |
commit | 991ef42e20b115accbc9d76e27c5c220d1c37095 (patch) | |
tree | 8a9e2ed1add9edfa4c2350be187c6859ae1631af /library/cpp/yt/yson_string | |
parent | 980dc47153689172f8869a2a0d8dfe1014b2d70e (diff) | |
download | ydb-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.h | 29 | ||||
-rw-r--r-- | library/cpp/yt/yson_string/string.cpp | 18 |
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; |