aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp
diff options
context:
space:
mode:
authorsavrus <savrus@yandex-team.com>2022-10-07 11:08:40 +0300
committersavrus <savrus@yandex-team.com>2022-10-07 11:08:40 +0300
commitab78e5e98f7093c7b8d5307f676067a08f537506 (patch)
tree0ac13e43e0f9c197cf28bef977ff1f40121c1188 /library/cpp
parent53354011eff56242aedc542ce6884d5b7ca3beb8 (diff)
downloadydb-ab78e5e98f7093c7b8d5307f676067a08f537506.tar.gz
Memory reference tracking restyling
Diffstat (limited to 'library/cpp')
-rw-r--r--library/cpp/yt/memory/CMakeLists.txt1
-rw-r--r--library/cpp/yt/memory/ref-inl.h14
-rw-r--r--library/cpp/yt/memory/ref.cpp8
-rw-r--r--library/cpp/yt/memory/ref.h12
-rw-r--r--library/cpp/yt/memory/shared_range.cpp46
-rw-r--r--library/cpp/yt/memory/shared_range.h64
-rw-r--r--library/cpp/yt/yson_string/string.cpp6
-rw-r--r--library/cpp/yt/yson_string/string.h2
8 files changed, 105 insertions, 48 deletions
diff --git a/library/cpp/yt/memory/CMakeLists.txt b/library/cpp/yt/memory/CMakeLists.txt
index 873bd56ae9..a620c5c7ec 100644
--- a/library/cpp/yt/memory/CMakeLists.txt
+++ b/library/cpp/yt/memory/CMakeLists.txt
@@ -19,4 +19,5 @@ target_sources(cpp-yt-memory PRIVATE
${CMAKE_SOURCE_DIR}/library/cpp/yt/memory/blob.cpp
${CMAKE_SOURCE_DIR}/library/cpp/yt/memory/ref.cpp
${CMAKE_SOURCE_DIR}/library/cpp/yt/memory/ref_tracked.cpp
+ ${CMAKE_SOURCE_DIR}/library/cpp/yt/memory/shared_range.cpp
)
diff --git a/library/cpp/yt/memory/ref-inl.h b/library/cpp/yt/memory/ref-inl.h
index 087e32e47c..b3738ab79b 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, ISharedRangeHolderPtr holder)
+Y_FORCE_INLINE TSharedRef::TSharedRef(TRef ref, TSharedRangeHolderPtr holder)
: TSharedRange<char>(ref, std::move(holder))
{ }
-Y_FORCE_INLINE TSharedRef::TSharedRef(const void* data, size_t length, ISharedRangeHolderPtr holder)
+Y_FORCE_INLINE TSharedRef::TSharedRef(const void* data, size_t length, TSharedRangeHolderPtr holder)
: TSharedRange<char>(static_cast<const char*>(data), length, std::move(holder))
{ }
-Y_FORCE_INLINE TSharedRef::TSharedRef(const void* begin, const void* end, ISharedRangeHolderPtr holder)
+Y_FORCE_INLINE TSharedRef::TSharedRef(const void* begin, const void* end, TSharedRangeHolderPtr 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, ISharedRangeHolderPtr holder)
+Y_FORCE_INLINE TSharedMutableRef::TSharedMutableRef(const TMutableRef& ref, TSharedRangeHolderPtr holder)
: TSharedMutableRange<char>(ref, std::move(holder))
{ }
-Y_FORCE_INLINE TSharedMutableRef::TSharedMutableRef(void* data, size_t length, ISharedRangeHolderPtr holder)
+Y_FORCE_INLINE TSharedMutableRef::TSharedMutableRef(void* data, size_t length, TSharedRangeHolderPtr holder)
: TSharedMutableRange<char>(static_cast<char*>(data), length, std::move(holder))
{ }
-Y_FORCE_INLINE TSharedMutableRef::TSharedMutableRef(void* begin, void* end, ISharedRangeHolderPtr holder)
+Y_FORCE_INLINE TSharedMutableRef::TSharedMutableRef(void* begin, void* end, TSharedRangeHolderPtr 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 ISharedRangeHolder
+ : public TSharedRangeHolder
, public TWithExtraSpace<TSharedRefArrayImpl>
{
public:
diff --git a/library/cpp/yt/memory/ref.cpp b/library/cpp/yt/memory/ref.cpp
index 363254653b..faa9e0613b 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 ISharedRangeHolder
+ : public TSharedRangeHolder
{
public:
explicit TBlobHolder(TBlob&& blob)
@@ -36,7 +36,7 @@ private:
////////////////////////////////////////////////////////////////////////////////
class TStringHolder
- : public ISharedRangeHolder
+ : public TSharedRangeHolder
{
public:
TStringHolder(TString&& string, TRefCountedTypeCookie cookie)
@@ -74,7 +74,7 @@ private:
template <class TDerived>
class TAllocationHolderBase
- : public ISharedRangeHolder
+ : public TSharedRangeHolder
{
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;
- ISharedRangeHolderPtr holder(Impl_.Get(), false);
+ TSharedRangeHolderPtr 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 007d15a555..9b01fd2ded 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, ISharedRangeHolderPtr holder);
+ TSharedRef(TRef ref, TSharedRangeHolderPtr holder);
//! Creates a TSharedRef from a pointer and length.
- TSharedRef(const void* data, size_t length, ISharedRangeHolderPtr holder);
+ TSharedRef(const void* data, size_t length, TSharedRangeHolderPtr holder);
//! Creates a TSharedRef from a range.
- TSharedRef(const void* begin, const void* end, ISharedRangeHolderPtr holder);
+ TSharedRef(const void* begin, const void* end, TSharedRangeHolderPtr 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, ISharedRangeHolderPtr holder);
+ TSharedMutableRef(const TMutableRef& ref, TSharedRangeHolderPtr holder);
//! Creates a TSharedMutableRef from a pointer and length.
- TSharedMutableRef(void* data, size_t length, ISharedRangeHolderPtr holder);
+ TSharedMutableRef(void* data, size_t length, TSharedRangeHolderPtr holder);
//! Creates a TSharedMutableRef from a range.
- TSharedMutableRef(void* begin, void* end, ISharedRangeHolderPtr holder);
+ TSharedMutableRef(void* begin, void* end, TSharedRangeHolderPtr holder);
//! Creates an empty TSharedMutableRef.
static TSharedMutableRef MakeEmpty();
diff --git a/library/cpp/yt/memory/shared_range.cpp b/library/cpp/yt/memory/shared_range.cpp
new file mode 100644
index 0000000000..c147ba5041
--- /dev/null
+++ b/library/cpp/yt/memory/shared_range.cpp
@@ -0,0 +1,46 @@
+#include "shared_range.h"
+#include "new.h"
+
+namespace NYT {
+
+////////////////////////////////////////////////////////////////////////////////
+
+TSharedRangeHolderPtr TSharedRangeHolder::Clone(const TSharedRangeHolderCloneOptions& /*options*/)
+{
+ return this;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+TSharedRangeHolderPtr MakeCompositeSharedRangeHolder(std::vector<TSharedRangeHolderPtr> holders)
+{
+ struct THolder
+ : public TSharedRangeHolder
+ {
+ std::vector<TSharedRangeHolderPtr> Holders;
+
+ TSharedRangeHolderPtr Clone(const TSharedRangeHolderCloneOptions& options) override
+ {
+ auto newHolder = New<THolder>();
+ newHolder->Holders.reserve(Holders.size());
+ for (const auto& holder : Holders) {
+ if (!holder) {
+ continue;
+ }
+ if (auto cloned = holder->Clone(options)) {
+ newHolder->Holders.push_back(cloned);
+ }
+ }
+
+ return newHolder;
+ }
+ };
+
+ auto holder = New<THolder>();
+ holder->Holders = std::move(holders);
+ return holder;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+} // namespace NYT
diff --git a/library/cpp/yt/memory/shared_range.h b/library/cpp/yt/memory/shared_range.h
index 17e55e760e..592902fe7a 100644
--- a/library/cpp/yt/memory/shared_range.h
+++ b/library/cpp/yt/memory/shared_range.h
@@ -15,13 +15,20 @@ class TCompactVector;
////////////////////////////////////////////////////////////////////////////////
-DECLARE_REFCOUNTED_STRUCT(ISharedRangeHolder)
+struct TSharedRangeHolderCloneOptions
+{
+ bool KeepMemoryReferenceTracking = true;
+};
+
+DECLARE_REFCOUNTED_STRUCT(TSharedRangeHolder)
-struct ISharedRangeHolder
+struct TSharedRangeHolder
: public TRefCounted
-{ };
+{
+ virtual TSharedRangeHolderPtr Clone(const TSharedRangeHolderCloneOptions& /*options*/);
+};
-DEFINE_REFCOUNTED_TYPE(ISharedRangeHolder)
+DEFINE_REFCOUNTED_TYPE(TSharedRangeHolder)
////////////////////////////////////////////////////////////////////////////////
@@ -36,39 +43,39 @@ public:
{ }
//! Constructs a TSharedRange from TRange.
- TSharedRange(TRange<T> range, ISharedRangeHolderPtr holder)
+ TSharedRange(TRange<T> range, TSharedRangeHolderPtr holder)
: TRange<T>(range)
, Holder_(std::move(holder))
{ }
//! Constructs a TSharedRange from a pointer and length.
- TSharedRange(const T* data, size_t length, ISharedRangeHolderPtr holder)
+ TSharedRange(const T* data, size_t length, TSharedRangeHolderPtr holder)
: TRange<T>(data, length)
, Holder_(std::move(holder))
{ }
//! Constructs a TSharedRange from a range.
- TSharedRange(const T* begin, const T* end, ISharedRangeHolderPtr holder)
+ TSharedRange(const T* begin, const T* end, TSharedRangeHolderPtr 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, ISharedRangeHolderPtr holder)
+ TSharedRange(const TCompactVector<T, N>& elements, TSharedRangeHolderPtr holder)
: TRange<T>(elements)
, Holder_(std::move(holder))
{ }
//! Constructs a TSharedRange from an std::vector.
- TSharedRange(const std::vector<T>& elements, ISharedRangeHolderPtr holder)
+ TSharedRange(const std::vector<T>& elements, TSharedRangeHolderPtr holder)
: TRange<T>(elements)
, Holder_(std::move(holder))
{ }
//! Constructs a TSharedRange from a C array.
template <size_t N>
- TSharedRange(const T (& elements)[N], ISharedRangeHolderPtr holder)
+ TSharedRange(const T (& elements)[N], TSharedRangeHolderPtr holder)
: TRange<T>(elements)
, Holder_(std::move(holder))
{ }
@@ -95,29 +102,32 @@ public:
return TSharedRange<T>(begin, end, Holder_);
}
- const ISharedRangeHolderPtr& GetHolder() const
+ const TSharedRangeHolderPtr& GetHolder() const
{
return Holder_;
}
- ISharedRangeHolderPtr&& ReleaseHolder()
+ TSharedRangeHolderPtr&& ReleaseHolder()
{
return std::move(Holder_);
}
protected:
- ISharedRangeHolderPtr Holder_;
+ TSharedRangeHolderPtr Holder_;
};
////////////////////////////////////////////////////////////////////////////////
+//! Constructs a combined holder from a vector of typed holders.
+TSharedRangeHolderPtr MakeCompositeSharedRangeHolder(std::vector<TSharedRangeHolderPtr> holders);
+
//! Constructs a combined holder instance by taking ownership of a given list of holders.
template <class... THolders>
-ISharedRangeHolderPtr MakeSharedRangeHolder(THolders&&... holders)
+TSharedRangeHolderPtr MakeSharedRangeHolder(THolders&&... holders)
{
struct THolder
- : public ISharedRangeHolder
+ : public TSharedRangeHolder
{
std::tuple<typename std::decay<THolders>::type...> Holders;
};
@@ -131,7 +141,7 @@ template <class T, class TContainer, class... THolders>
TSharedRange<T> DoMakeSharedRange(TContainer&& elements, THolders&&... holders)
{
struct THolder
- : public ISharedRangeHolder
+ : public TSharedRangeHolder
{
typename std::decay<TContainer>::type Elements;
std::tuple<typename std::decay<THolders>::type...> Holders;
@@ -174,7 +184,7 @@ TSharedRange<T> MakeSharedRange(TRange<T> range, THolders&&... holders)
}
template <class T, class THolder>
-TSharedRange<T> MakeSharedRange(TRange<T> range, ISharedRangeHolderPtr holder)
+TSharedRange<T> MakeSharedRange(TRange<T> range, TSharedRangeHolderPtr holder)
{
return TSharedRange<T>(range, std::move(holder));
}
@@ -200,39 +210,39 @@ public:
{ }
//! Constructs a TSharedMutableRange from TMutableRange.
- TSharedMutableRange(TMutableRange<T> range, ISharedRangeHolderPtr holder)
+ TSharedMutableRange(TMutableRange<T> range, TSharedRangeHolderPtr holder)
: TMutableRange<T>(range)
, Holder_(std::move(holder))
{ }
//! Constructs a TSharedMutableRange from a pointer and length.
- TSharedMutableRange(T* data, size_t length, ISharedRangeHolderPtr holder)
+ TSharedMutableRange(T* data, size_t length, TSharedRangeHolderPtr holder)
: TMutableRange<T>(data, length)
, Holder_(std::move(holder))
{ }
//! Constructs a TSharedMutableRange from a range.
- TSharedMutableRange(T* begin, T* end, ISharedRangeHolderPtr holder)
+ TSharedMutableRange(T* begin, T* end, TSharedRangeHolderPtr holder)
: TMutableRange<T>(begin, end)
, Holder_(std::move(holder))
{ }
//! Constructs a TSharedMutableRange from a TCompactVector.
template <size_t N>
- TSharedMutableRange(TCompactVector<T, N>& elements, ISharedRangeHolderPtr holder)
+ TSharedMutableRange(TCompactVector<T, N>& elements, TSharedRangeHolderPtr holder)
: TMutableRange<T>(elements)
, Holder_(std::move(holder))
{ }
//! Constructs a TSharedMutableRange from an std::vector.
- TSharedMutableRange(std::vector<T>& elements, ISharedRangeHolderPtr holder)
+ TSharedMutableRange(std::vector<T>& elements, TSharedRangeHolderPtr holder)
: TMutableRange<T>(elements)
, Holder_(std::move(holder))
{ }
//! Constructs a TSharedMutableRange from a C array.
template <size_t N>
- TSharedMutableRange(T (& elements)[N], ISharedRangeHolderPtr holder)
+ TSharedMutableRange(T (& elements)[N], TSharedRangeHolderPtr holder)
: TMutableRange<T>(elements)
, Holder_(std::move(holder))
{ }
@@ -259,25 +269,25 @@ public:
return TSharedMutableRange<T>(begin, end, Holder_);
}
- ISharedRangeHolderPtr GetHolder() const
+ TSharedRangeHolderPtr GetHolder() const
{
return Holder_;
}
- ISharedRangeHolderPtr&& ReleaseHolder()
+ TSharedRangeHolderPtr&& ReleaseHolder()
{
return std::move(Holder_);
}
protected:
- ISharedRangeHolderPtr Holder_;
+ TSharedRangeHolderPtr Holder_;
};
template <class T, class TContainer, class... THolders>
TSharedMutableRange<T> DoMakeSharedMutableRange(TContainer&& elements, THolders&&... holders)
{
struct THolder
- : public ISharedRangeHolder
+ : public TSharedRangeHolder
{
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 87aa08cce4..c92d4d6e42 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 ISharedRangeHolder
+ : public TSharedRangeHolder
, public TWithExtraSpace<TCapturedYsonStringPayload>
{
char* GetData()
@@ -155,7 +155,7 @@ TString TYsonString::ToString() const
[] (const TNullPayload&) -> TString {
YT_ABORT();
},
- [&] (const ISharedRangeHolderPtr&) {
+ [&] (const TSharedRangeHolderPtr&) {
return TString(AsStringBuf());
},
[] (const TString& payload) {
@@ -170,7 +170,7 @@ TSharedRef TYsonString::ToSharedRef() const
[] (const TNullPayload&) -> TSharedRef {
YT_ABORT();
},
- [&] (const ISharedRangeHolderPtr& holder) {
+ [&] (const TSharedRangeHolderPtr& 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 413851acf4..29845b70ae 100644
--- a/library/cpp/yt/yson_string/string.h
+++ b/library/cpp/yt/yson_string/string.h
@@ -111,7 +111,7 @@ private:
struct TNullPayload
{ };
- std::variant<TNullPayload, ISharedRangeHolderPtr, TString> Payload_;
+ std::variant<TNullPayload, TSharedRangeHolderPtr, TString> Payload_;
const char* Begin_;
ui64 Size_ : 56;