From c1a1d66864a637f06b409b1768bc05a982b28949 Mon Sep 17 00:00:00 2001 From: Soheil Hassas Yeganeh Date: Tue, 25 Sep 2018 17:50:45 -0400 Subject: [PATCH 1/4] Avoid allocating temporary strings in Channel::CreateCall(). Add `SliceFromArray()` which takes a `char*` instead of `const string&`, to save string allocations for copying from a `char *`. Use the new API to eliminate two string allocations and copies per call for method and host names. release-note: no --- include/grpcpp/impl/codegen/slice.h | 4 ++++ src/cpp/client/channel_cc.cc | 6 ++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/include/grpcpp/impl/codegen/slice.h b/include/grpcpp/impl/codegen/slice.h index 8966559dc85..9cdca3ab3c9 100644 --- a/include/grpcpp/impl/codegen/slice.h +++ b/include/grpcpp/impl/codegen/slice.h @@ -138,6 +138,10 @@ inline grpc_slice SliceFromCopiedString(const grpc::string& str) { str.length()); } +inline grpc_slice SliceFromArray(const char* arr, size_t len) { + return g_core_codegen_interface->grpc_slice_from_copied_buffer(arr, len); +} + } // namespace grpc #endif // GRPCPP_IMPL_CODEGEN_SLICE_H diff --git a/src/cpp/client/channel_cc.cc b/src/cpp/client/channel_cc.cc index c59059f0450..5d1a9f4b961 100644 --- a/src/cpp/client/channel_cc.cc +++ b/src/cpp/client/channel_cc.cc @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -116,10 +117,11 @@ internal::Call Channel::CreateCall(const internal::RpcMethod& method, } else if (!host_.empty()) { host_str = host_.c_str(); } - grpc_slice method_slice = SliceFromCopiedString(method.name()); + grpc_slice method_slice = + SliceFromArray(method.name(), strlen(method.name())); grpc_slice host_slice; if (host_str != nullptr) { - host_slice = SliceFromCopiedString(host_str); + host_slice = SliceFromArray(host_str, strlen(host_str)); } c_call = grpc_channel_create_call( c_channel_, context->propagate_from_call_, From 8442cc213b2316272764b6a89e5ded5929e35a78 Mon Sep 17 00:00:00 2001 From: Soheil Hassas Yeganeh Date: Tue, 25 Sep 2018 18:30:22 -0400 Subject: [PATCH 2/4] Fix styling issue added in c1a1d668 --- include/grpcpp/impl/codegen/slice.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/grpcpp/impl/codegen/slice.h b/include/grpcpp/impl/codegen/slice.h index 9cdca3ab3c9..75c093d1da0 100644 --- a/include/grpcpp/impl/codegen/slice.h +++ b/include/grpcpp/impl/codegen/slice.h @@ -139,7 +139,7 @@ inline grpc_slice SliceFromCopiedString(const grpc::string& str) { } inline grpc_slice SliceFromArray(const char* arr, size_t len) { - return g_core_codegen_interface->grpc_slice_from_copied_buffer(arr, len); + return g_core_codegen_interface->grpc_slice_from_copied_buffer(arr, len); } } // namespace grpc From 369cfe118cbfda61c3fc205a18d1b1924d853700 Mon Sep 17 00:00:00 2001 From: Soheil Hassas Yeganeh Date: Tue, 25 Sep 2018 21:59:15 -0400 Subject: [PATCH 3/4] Use SliceFromCopiedString() for host name. This is to address Yang's review comment. --- src/cpp/client/channel_cc.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cpp/client/channel_cc.cc b/src/cpp/client/channel_cc.cc index 5d1a9f4b961..510ae73478e 100644 --- a/src/cpp/client/channel_cc.cc +++ b/src/cpp/client/channel_cc.cc @@ -111,17 +111,17 @@ internal::Call Channel::CreateCall(const internal::RpcMethod& method, context->propagation_options_.c_bitmask(), cq->cq(), method.channel_tag(), context->raw_deadline(), nullptr); } else { - const char* host_str = nullptr; + const string* host_str = nullptr; if (!context->authority().empty()) { - host_str = context->authority_.c_str(); + host_str = &context->authority_; } else if (!host_.empty()) { - host_str = host_.c_str(); + host_str = &host_; } grpc_slice method_slice = SliceFromArray(method.name(), strlen(method.name())); grpc_slice host_slice; if (host_str != nullptr) { - host_slice = SliceFromArray(host_str, strlen(host_str)); + host_slice = SliceFromCopiedString(*host_str); } c_call = grpc_channel_create_call( c_channel_, context->propagate_from_call_, From 80ce1865d765f3a3623a8d6420b8e82e3f4b5cca Mon Sep 17 00:00:00 2001 From: Soheil Hassas Yeganeh Date: Thu, 27 Sep 2018 11:26:05 -0400 Subject: [PATCH 4/4] Make SliceFromArray() static in channel_cc.cc. Also, use `context->authority_` instead of `context->authority()` for consistency. --- include/grpcpp/impl/codegen/slice.h | 4 ---- src/cpp/client/channel_cc.cc | 6 +++++- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/grpcpp/impl/codegen/slice.h b/include/grpcpp/impl/codegen/slice.h index 75c093d1da0..8966559dc85 100644 --- a/include/grpcpp/impl/codegen/slice.h +++ b/include/grpcpp/impl/codegen/slice.h @@ -138,10 +138,6 @@ inline grpc_slice SliceFromCopiedString(const grpc::string& str) { str.length()); } -inline grpc_slice SliceFromArray(const char* arr, size_t len) { - return g_core_codegen_interface->grpc_slice_from_copied_buffer(arr, len); -} - } // namespace grpc #endif // GRPCPP_IMPL_CODEGEN_SLICE_H diff --git a/src/cpp/client/channel_cc.cc b/src/cpp/client/channel_cc.cc index 510ae73478e..31c02893b11 100644 --- a/src/cpp/client/channel_cc.cc +++ b/src/cpp/client/channel_cc.cc @@ -65,6 +65,10 @@ Channel::~Channel() { namespace { +inline grpc_slice SliceFromArray(const char* arr, size_t len) { + return g_core_codegen_interface->grpc_slice_from_copied_buffer(arr, len); +} + grpc::string GetChannelInfoField(grpc_channel* channel, grpc_channel_info* channel_info, char*** channel_info_field) { @@ -112,7 +116,7 @@ internal::Call Channel::CreateCall(const internal::RpcMethod& method, method.channel_tag(), context->raw_deadline(), nullptr); } else { const string* host_str = nullptr; - if (!context->authority().empty()) { + if (!context->authority_.empty()) { host_str = &context->authority_; } else if (!host_.empty()) { host_str = &host_;