diff options
author | Maksim Kita <kitaetoya@gmail.com> | 2023-05-04 13:54:40 +0000 |
---|---|---|
committer | alexv-smirnov <alex@ydb.tech> | 2023-05-04 16:54:40 +0300 |
commit | df25b3d56ae727879aa8287b6ba1ba123c123cc0 (patch) | |
tree | 7403467e6292769ce16d3a7a093b946fbcd62dd6 | |
parent | 5813fbc93132b092da12b713d11c19c91d551bcb (diff) | |
download | ydb-df25b3d56ae727879aa8287b6ba1ba123c123cc0.tar.gz |
"Public SDK avoid unnecessary protobuf copy"
"Public SDK avoid unnecessary protobuf copy"
Pull Request resolved: #188
-rw-r--r-- | ydb/public/sdk/cpp/client/ydb_table/table.cpp | 4 | ||||
-rw-r--r-- | ydb/public/sdk/cpp/client/ydb_value/value.cpp | 63 | ||||
-rw-r--r-- | ydb/public/sdk/cpp/client/ydb_value/value.h | 7 |
3 files changed, 64 insertions, 10 deletions
diff --git a/ydb/public/sdk/cpp/client/ydb_table/table.cpp b/ydb/public/sdk/cpp/client/ydb_table/table.cpp index 7c27ca7ea3..516c8d805f 100644 --- a/ydb/public/sdk/cpp/client/ydb_table/table.cpp +++ b/ydb/public/sdk/cpp/client/ydb_table/table.cpp @@ -2352,9 +2352,7 @@ public: auto request = MakeOperationRequest<Ydb::Table::BulkUpsertRequest>(settings); request.set_table(table); *request.mutable_rows()->mutable_type() = TProtoAccessor::GetProto(rows.GetType()); - - // TODO: move protobuf instead of copying it!!!!111 - *request.mutable_rows()->mutable_value() = TProtoAccessor::GetProto(rows); + *request.mutable_rows()->mutable_value() = std::move(rows.GetProto()); auto promise = NewPromise<TBulkUpsertResult>(); diff --git a/ydb/public/sdk/cpp/client/ydb_value/value.cpp b/ydb/public/sdk/cpp/client/ydb_value/value.cpp index 1bb65d5e32..8b83b70a8b 100644 --- a/ydb/public/sdk/cpp/client/ydb_value/value.cpp +++ b/ydb/public/sdk/cpp/client/ydb_value/value.cpp @@ -83,7 +83,7 @@ public: TImpl(Ydb::Type&& typeProto) : ProtoType_(std::move(typeProto)) {} - const Ydb::Type ProtoType_; + Ydb::Type ProtoType_; }; //////////////////////////////////////////////////////////////////////////////// @@ -106,6 +106,11 @@ const Ydb::Type& TType::GetProto() const { return Impl_->ProtoType_; } +Ydb::Type& TType::GetProto() +{ + return Impl_->ProtoType_; +} + //////////////////////////////////////////////////////////////////////////////// class TTypeParser::TImpl { @@ -745,6 +750,10 @@ public: GetProto().CopyFrom(TProtoAccessor::GetProto(type)); } + void SetType(TType&& type) { + GetProto() = std::move(type.GetProto()); + } + private: void AddPosition(Ydb::Type* type) { Path_.emplace_back(TProtoPosition{type}); @@ -1014,7 +1023,6 @@ TString TUuidValue::ToString() const { std::memcpy(dw, Buf_.Bytes, sizeof(dw)); NKikimr::NUuid::UuidToString(dw, s); return s.Str(); - } //////////////////////////////////////////////////////////////////////////////// @@ -1029,8 +1037,8 @@ public: : Type_(type) , ProtoValue_(std::move(valueProto)) {} - const TType Type_; - const Ydb::Value ProtoValue_; + TType Type_; + Ydb::Value ProtoValue_; }; //////////////////////////////////////////////////////////////////////////////// @@ -1045,10 +1053,18 @@ const TType& TValue::GetType() const { return Impl_->Type_; } +TType & TValue::GetType() { + return Impl_->Type_; +} + const Ydb::Value& TValue::GetProto() const { return Impl_->ProtoValue_; } +Ydb::Value& TValue::GetProto() { + return Impl_->ProtoValue_; +} + //////////////////////////////////////////////////////////////////////////////// class TValueParser::TImpl { @@ -2225,6 +2241,17 @@ public: SetProtoValue(itemValue); } + void AddListItem(TValue&& itemValue) { + CheckContainerKind(ETypeKind::List); + PopPath(); + PushPath(*GetValue().add_items()); + + if (!CheckType(itemValue.GetType())) { + TypeBuilder_.SetType(std::move(itemValue.GetType())); + } + SetProtoValue(std::move(itemValue)); + } + void EmptyList(const TType& itemType) { BeginList(itemType); EndList(); @@ -2293,6 +2320,16 @@ public: SetProtoValue(memberValue); } + void AddMember(const TString& memberName, TValue&& memberValue) { + AddMember(memberName); + + if (!CheckType(memberValue.GetType())) { + TypeBuilder_.SetType(std::move(memberValue.GetType())); + } + + SetProtoValue(std::move(memberValue)); + } + void EndStruct() { CheckContainerKind(ETypeKind::Struct); @@ -2492,6 +2529,10 @@ private: GetValue().CopyFrom(TProtoAccessor::GetProto(value)); } + void SetProtoValue(TValue&& value) { + GetValue() = std::move(value.GetProto()); + } + bool GetBuildType() { return PathTop().BuildType; } @@ -2546,7 +2587,7 @@ private: return false; } - if (!TypesEqual(GetType(), type)) { + if (!TypesEqual(GetType(), type.GetProto())) { FatalError(TStringBuilder() << "Type mismatch, expected: " << FormatType(GetType()) << ", actual: " << FormatType(type)); return false; @@ -3040,6 +3081,12 @@ TDerived& TValueBuilderBase<TDerived>::AddListItem(const TValue& itemValue) { } template<typename TDerived> +TDerived& TValueBuilderBase<TDerived>::AddListItem(TValue&& itemValue) { + Impl_->AddListItem(std::move(itemValue)); + return static_cast<TDerived&>(*this); +} + +template<typename TDerived> TDerived& TValueBuilderBase<TDerived>::EmptyList(const TType& itemType) { Impl_->EmptyList(itemType); return static_cast<TDerived&>(*this); @@ -3070,6 +3117,12 @@ TDerived& TValueBuilderBase<TDerived>::AddMember(const TString& memberName, cons } template<typename TDerived> +TDerived& TValueBuilderBase<TDerived>::AddMember(const TString& memberName, TValue&& memberValue) { + Impl_->AddMember(memberName, std::move(memberValue)); + return static_cast<TDerived&>(*this); +} + +template<typename TDerived> TDerived& TValueBuilderBase<TDerived>::EndStruct() { Impl_->EndStruct(); return static_cast<TDerived&>(*this); diff --git a/ydb/public/sdk/cpp/client/ydb_value/value.h b/ydb/public/sdk/cpp/client/ydb_value/value.h index 43d516afb9..d50ae3bffb 100644 --- a/ydb/public/sdk/cpp/client/ydb_value/value.h +++ b/ydb/public/sdk/cpp/client/ydb_value/value.h @@ -24,8 +24,8 @@ public: TString ToString() const; void Out(IOutputStream& o) const; -private: const Ydb::Type& GetProto() const; + Ydb::Type& GetProto(); private: class TImpl; @@ -264,9 +264,10 @@ public: TValue(const TType& type, Ydb::Value&& valueProto); const TType& GetType() const; + TType & GetType(); -private: const Ydb::Value& GetProto() const; + Ydb::Value& GetProto(); private: class TImpl; @@ -455,6 +456,7 @@ public: TDerived& BeginList(); TDerived& AddListItem(); TDerived& AddListItem(const TValue& itemValue); + TDerived& AddListItem(TValue&& itemValue); TDerived& EndList(); TDerived& EmptyList(const TType& itemType); TDerived& EmptyList(); @@ -463,6 +465,7 @@ public: TDerived& BeginStruct(); TDerived& AddMember(const TString& memberName); TDerived& AddMember(const TString& memberName, const TValue& memberValue); + TDerived& AddMember(const TString& memberName, TValue&& memberValue); TDerived& EndStruct(); // Tuple |