diff options
author | savrus <savrus@yandex-team.com> | 2022-09-19 22:36:24 +0300 |
---|---|---|
committer | savrus <savrus@yandex-team.com> | 2022-09-19 22:36:24 +0300 |
commit | c62e39eff60f637bfe4eeae1984034d010cc3e80 (patch) | |
tree | 03495974687e9d403be629256547f347665f06ff | |
parent | aecd1bb1e7a5e9fe53d3828747a6cbc36f9f4e41 (diff) | |
download | ydb-c62e39eff60f637bfe4eeae1984034d010cc3e80.tar.gz |
Introduce shared range holders
-rw-r--r-- | library/cpp/yt/memory/ref-inl.h | 14 | ||||
-rw-r--r-- | library/cpp/yt/memory/ref.cpp | 8 | ||||
-rw-r--r-- | library/cpp/yt/memory/ref.h | 12 | ||||
-rw-r--r-- | library/cpp/yt/memory/shared_range.h | 65 | ||||
-rw-r--r-- | library/cpp/yt/yson_string/string.cpp | 6 | ||||
-rw-r--r-- | library/cpp/yt/yson_string/string.h | 4 |
6 files changed, 57 insertions, 52 deletions
diff --git a/library/cpp/yt/memory/ref-inl.h b/library/cpp/yt/memory/ref-inl.h index 6f2104962c..087e32e47c 100644 --- a/library/cpp/yt/memory/ref-inl.h +++ b/library/cpp/yt/memory/ref-inl.h @@ -99,15 +99,15 @@ Y_FORCE_INLINE TMutableRef TMutableRef::Slice(size_t startOffset, size_t endOffs //////////////////////////////////////////////////////////////////////////////// -Y_FORCE_INLINE TSharedRef::TSharedRef(TRef ref, TSharedRange<char>::THolderPtr holder) +Y_FORCE_INLINE TSharedRef::TSharedRef(TRef ref, ISharedRangeHolderPtr holder) : TSharedRange<char>(ref, std::move(holder)) { } -Y_FORCE_INLINE TSharedRef::TSharedRef(const void* data, size_t length, TSharedRange<char>::THolderPtr holder) +Y_FORCE_INLINE TSharedRef::TSharedRef(const void* data, size_t length, ISharedRangeHolderPtr holder) : TSharedRange<char>(static_cast<const char*>(data), length, std::move(holder)) { } -Y_FORCE_INLINE TSharedRef::TSharedRef(const void* begin, const void* end, TSharedRange<char>::THolderPtr holder) +Y_FORCE_INLINE TSharedRef::TSharedRef(const void* begin, const void* end, ISharedRangeHolderPtr holder) : TSharedRange<char>(static_cast<const char*>(begin), static_cast<const char*>(end), std::move(holder)) { } @@ -158,15 +158,15 @@ Y_FORCE_INLINE TSharedRef TSharedRef::Slice(const void* begin, const void* end) //////////////////////////////////////////////////////////////////////////////// -Y_FORCE_INLINE TSharedMutableRef::TSharedMutableRef(const TMutableRef& ref, TSharedMutableRange<char>::THolderPtr holder) +Y_FORCE_INLINE TSharedMutableRef::TSharedMutableRef(const TMutableRef& ref, ISharedRangeHolderPtr holder) : TSharedMutableRange<char>(ref, std::move(holder)) { } -Y_FORCE_INLINE TSharedMutableRef::TSharedMutableRef(void* data, size_t length, TSharedMutableRange<char>::THolderPtr holder) +Y_FORCE_INLINE TSharedMutableRef::TSharedMutableRef(void* data, size_t length, ISharedRangeHolderPtr holder) : TSharedMutableRange<char>(static_cast<char*>(data), length, std::move(holder)) { } -Y_FORCE_INLINE TSharedMutableRef::TSharedMutableRef(void* begin, void* end, TSharedMutableRange<char>::THolderPtr holder) +Y_FORCE_INLINE TSharedMutableRef::TSharedMutableRef(void* begin, void* end, ISharedRangeHolderPtr holder) : TSharedMutableRange<char>(static_cast<char*>(begin), static_cast<char*>(end), std::move(holder)) { } @@ -257,7 +257,7 @@ size_t GetByteSize(const std::vector<T>& parts) //////////////////////////////////////////////////////////////////////////////// class TSharedRefArrayImpl - : public TRefCounted + : public ISharedRangeHolder , public TWithExtraSpace<TSharedRefArrayImpl> { public: diff --git a/library/cpp/yt/memory/ref.cpp b/library/cpp/yt/memory/ref.cpp index 257a8c9050..363254653b 100644 --- a/library/cpp/yt/memory/ref.cpp +++ b/library/cpp/yt/memory/ref.cpp @@ -22,7 +22,7 @@ char MutableEmptyRefData[1] = {0}; //////////////////////////////////////////////////////////////////////////////// class TBlobHolder - : public TRefCounted + : public ISharedRangeHolder { public: explicit TBlobHolder(TBlob&& blob) @@ -36,7 +36,7 @@ private: //////////////////////////////////////////////////////////////////////////////// class TStringHolder - : public TRefCounted + : public ISharedRangeHolder { public: TStringHolder(TString&& string, TRefCountedTypeCookie cookie) @@ -74,7 +74,7 @@ private: template <class TDerived> class TAllocationHolderBase - : public TRefCounted + : public ISharedRangeHolder { public: ~TAllocationHolderBase() @@ -359,7 +359,7 @@ TMutableRef TSharedRefArrayBuilder::AllocateAndAdd(size_t size) YT_ASSERT(CurrentAllocationPtr_ + size <= Impl_->GetBeginAllocationPtr() + AllocationCapacity_); TMutableRef ref(CurrentAllocationPtr_, size); CurrentAllocationPtr_ += size; - TRefCountedPtr holder(Impl_.Get(), false); + ISharedRangeHolderPtr holder(Impl_.Get(), false); TSharedRef sharedRef(ref, std::move(holder)); Add(std::move(sharedRef)); return ref; diff --git a/library/cpp/yt/memory/ref.h b/library/cpp/yt/memory/ref.h index f93dc6dcfa..007d15a555 100644 --- a/library/cpp/yt/memory/ref.h +++ b/library/cpp/yt/memory/ref.h @@ -111,13 +111,13 @@ public: TSharedRef() = default; //! Creates a TSharedRef with a given holder. - TSharedRef(TRef ref, THolderPtr holder); + TSharedRef(TRef ref, ISharedRangeHolderPtr holder); //! Creates a TSharedRef from a pointer and length. - TSharedRef(const void* data, size_t length, THolderPtr holder); + TSharedRef(const void* data, size_t length, ISharedRangeHolderPtr holder); //! Creates a TSharedRef from a range. - TSharedRef(const void* begin, const void* end, THolderPtr holder); + TSharedRef(const void* begin, const void* end, ISharedRangeHolderPtr holder); //! Creates an empty TSharedRef. static TSharedRef MakeEmpty(); @@ -189,13 +189,13 @@ public: TSharedMutableRef() = default; //! Creates a TSharedMutableRef with a given holder. - TSharedMutableRef(const TMutableRef& ref, THolderPtr holder); + TSharedMutableRef(const TMutableRef& ref, ISharedRangeHolderPtr holder); //! Creates a TSharedMutableRef from a pointer and length. - TSharedMutableRef(void* data, size_t length, THolderPtr holder); + TSharedMutableRef(void* data, size_t length, ISharedRangeHolderPtr holder); //! Creates a TSharedMutableRef from a range. - TSharedMutableRef(void* begin, void* end, THolderPtr holder); + TSharedMutableRef(void* begin, void* end, ISharedRangeHolderPtr holder); //! Creates an empty TSharedMutableRef. static TSharedMutableRef MakeEmpty(); diff --git a/library/cpp/yt/memory/shared_range.h b/library/cpp/yt/memory/shared_range.h index 9841d7a0df..17e55e760e 100644 --- a/library/cpp/yt/memory/shared_range.h +++ b/library/cpp/yt/memory/shared_range.h @@ -13,52 +13,62 @@ namespace NYT { template <class T, size_t N> class TCompactVector; +//////////////////////////////////////////////////////////////////////////////// + +DECLARE_REFCOUNTED_STRUCT(ISharedRangeHolder) + +struct ISharedRangeHolder + : public TRefCounted +{ }; + +DEFINE_REFCOUNTED_TYPE(ISharedRangeHolder) + +//////////////////////////////////////////////////////////////////////////////// + //! TRange with ownership semantics. template <class T> class TSharedRange : public TRange<T> { public: - using THolderPtr = TRefCountedPtr; - //! Constructs a null TSharedRange. TSharedRange() { } //! Constructs a TSharedRange from TRange. - TSharedRange(TRange<T> range, THolderPtr holder) + TSharedRange(TRange<T> range, ISharedRangeHolderPtr holder) : TRange<T>(range) , Holder_(std::move(holder)) { } //! Constructs a TSharedRange from a pointer and length. - TSharedRange(const T* data, size_t length, THolderPtr holder) + TSharedRange(const T* data, size_t length, ISharedRangeHolderPtr holder) : TRange<T>(data, length) , Holder_(std::move(holder)) { } //! Constructs a TSharedRange from a range. - TSharedRange(const T* begin, const T* end, THolderPtr holder) + TSharedRange(const T* begin, const T* end, ISharedRangeHolderPtr holder) : TRange<T>(begin, end) , Holder_(std::move(holder)) { } //! Constructs a TSharedRange from a TCompactVector. template <size_t N> - TSharedRange(const TCompactVector<T, N>& elements, THolderPtr holder) + TSharedRange(const TCompactVector<T, N>& elements, ISharedRangeHolderPtr holder) : TRange<T>(elements) , Holder_(std::move(holder)) { } //! Constructs a TSharedRange from an std::vector. - TSharedRange(const std::vector<T>& elements, THolderPtr holder) + TSharedRange(const std::vector<T>& elements, ISharedRangeHolderPtr holder) : TRange<T>(elements) , Holder_(std::move(holder)) { } //! Constructs a TSharedRange from a C array. template <size_t N> - TSharedRange(const T (& elements)[N], THolderPtr holder) + TSharedRange(const T (& elements)[N], ISharedRangeHolderPtr holder) : TRange<T>(elements) , Holder_(std::move(holder)) { } @@ -85,18 +95,18 @@ public: return TSharedRange<T>(begin, end, Holder_); } - const THolderPtr& GetHolder() const + const ISharedRangeHolderPtr& GetHolder() const { return Holder_; } - THolderPtr&& ReleaseHolder() + ISharedRangeHolderPtr&& ReleaseHolder() { return std::move(Holder_); } protected: - THolderPtr Holder_; + ISharedRangeHolderPtr Holder_; }; @@ -104,10 +114,10 @@ protected: //! Constructs a combined holder instance by taking ownership of a given list of holders. template <class... THolders> -TRefCountedPtr MakeCompositeHolder(THolders&&... holders) +ISharedRangeHolderPtr MakeSharedRangeHolder(THolders&&... holders) { struct THolder - : public TRefCounted + : public ISharedRangeHolder { std::tuple<typename std::decay<THolders>::type...> Holders; }; @@ -121,7 +131,7 @@ template <class T, class TContainer, class... THolders> TSharedRange<T> DoMakeSharedRange(TContainer&& elements, THolders&&... holders) { struct THolder - : public TRefCounted + : public ISharedRangeHolder { typename std::decay<TContainer>::type Elements; std::tuple<typename std::decay<THolders>::type...> Holders; @@ -160,11 +170,11 @@ TSharedRange<T> MakeSharedRange(const std::vector<T>& elements, THolders&&... ho template <class T, class... THolders> TSharedRange<T> MakeSharedRange(TRange<T> range, THolders&&... holders) { - return TSharedRange<T>(range, MakeCompositeHolder(std::forward<THolders>(holders)...)); + return TSharedRange<T>(range, MakeSharedRangeHolder(std::forward<THolders>(holders)...)); } template <class T, class THolder> -TSharedRange<T> MakeSharedRange(TRange<T> range, TIntrusivePtr<THolder> holder) +TSharedRange<T> MakeSharedRange(TRange<T> range, ISharedRangeHolderPtr holder) { return TSharedRange<T>(range, std::move(holder)); } @@ -185,46 +195,44 @@ class TSharedMutableRange : public TMutableRange<T> { public: - using THolderPtr = TRefCountedPtr; - //! Constructs a null TSharedMutableRange. TSharedMutableRange() { } //! Constructs a TSharedMutableRange from TMutableRange. - TSharedMutableRange(TMutableRange<T> range, THolderPtr holder) + TSharedMutableRange(TMutableRange<T> range, ISharedRangeHolderPtr holder) : TMutableRange<T>(range) , Holder_(std::move(holder)) { } //! Constructs a TSharedMutableRange from a pointer and length. - TSharedMutableRange(T* data, size_t length, THolderPtr holder) + TSharedMutableRange(T* data, size_t length, ISharedRangeHolderPtr holder) : TMutableRange<T>(data, length) , Holder_(std::move(holder)) { } //! Constructs a TSharedMutableRange from a range. - TSharedMutableRange(T* begin, T* end, THolderPtr holder) + TSharedMutableRange(T* begin, T* end, ISharedRangeHolderPtr holder) : TMutableRange<T>(begin, end) , Holder_(std::move(holder)) { } //! Constructs a TSharedMutableRange from a TCompactVector. template <size_t N> - TSharedMutableRange(TCompactVector<T, N>& elements, THolderPtr holder) + TSharedMutableRange(TCompactVector<T, N>& elements, ISharedRangeHolderPtr holder) : TMutableRange<T>(elements) , Holder_(std::move(holder)) { } //! Constructs a TSharedMutableRange from an std::vector. - TSharedMutableRange(std::vector<T>& elements, THolderPtr holder) + TSharedMutableRange(std::vector<T>& elements, ISharedRangeHolderPtr holder) : TMutableRange<T>(elements) , Holder_(std::move(holder)) { } //! Constructs a TSharedMutableRange from a C array. template <size_t N> - TSharedMutableRange(T (& elements)[N], THolderPtr holder) + TSharedMutableRange(T (& elements)[N], ISharedRangeHolderPtr holder) : TMutableRange<T>(elements) , Holder_(std::move(holder)) { } @@ -251,26 +259,25 @@ public: return TSharedMutableRange<T>(begin, end, Holder_); } - THolderPtr GetHolder() const + ISharedRangeHolderPtr GetHolder() const { return Holder_; } - THolderPtr&& ReleaseHolder() + ISharedRangeHolderPtr&& ReleaseHolder() { return std::move(Holder_); } protected: - THolderPtr Holder_; - + ISharedRangeHolderPtr Holder_; }; template <class T, class TContainer, class... THolders> TSharedMutableRange<T> DoMakeSharedMutableRange(TContainer&& elements, THolders&&... holders) { struct THolder - : public TRefCounted + : public ISharedRangeHolder { typename std::decay<TContainer>::type Elements; std::tuple<typename std::decay<THolders>::type...> Holders; diff --git a/library/cpp/yt/yson_string/string.cpp b/library/cpp/yt/yson_string/string.cpp index 15eafdf615..87aa08cce4 100644 --- a/library/cpp/yt/yson_string/string.cpp +++ b/library/cpp/yt/yson_string/string.cpp @@ -72,7 +72,7 @@ TYsonString::TYsonString(const TYsonStringBuf& ysonStringBuf) { if (ysonStringBuf) { struct TCapturedYsonStringPayload - : public TRefCounted + : public ISharedRangeHolder , public TWithExtraSpace<TCapturedYsonStringPayload> { char* GetData() @@ -155,7 +155,7 @@ TString TYsonString::ToString() const [] (const TNullPayload&) -> TString { YT_ABORT(); }, - [&] (const THolder&) { + [&] (const ISharedRangeHolderPtr&) { return TString(AsStringBuf()); }, [] (const TString& payload) { @@ -170,7 +170,7 @@ TSharedRef TYsonString::ToSharedRef() const [] (const TNullPayload&) -> TSharedRef { YT_ABORT(); }, - [&] (const THolder& holder) { + [&] (const ISharedRangeHolderPtr& holder) { return TSharedRef(Begin_, Size_, holder); }, [] (const TString& payload) { diff --git a/library/cpp/yt/yson_string/string.h b/library/cpp/yt/yson_string/string.h index 8feb01806d..413851acf4 100644 --- a/library/cpp/yt/yson_string/string.h +++ b/library/cpp/yt/yson_string/string.h @@ -111,9 +111,7 @@ private: struct TNullPayload { }; - using THolder = TRefCountedPtr; - - std::variant<TNullPayload, THolder, TString> Payload_; + std::variant<TNullPayload, ISharedRangeHolderPtr, TString> Payload_; const char* Begin_; ui64 Size_ : 56; |