Selaa lähdekoodia

Merge pull request #16699 from soheilhy/string-copy

Avoid allocating temporary strings in Channel::CreateCall().
Yang Gao 7 vuotta sitten
vanhempi
commit
91e751c2cf
1 muutettua tiedostoa jossa 12 lisäystä ja 6 poistoa
  1. 12 6
      src/cpp/client/channel_cc.cc

+ 12 - 6
src/cpp/client/channel_cc.cc

@@ -20,6 +20,7 @@
 
 #include <chrono>
 #include <condition_variable>
+#include <cstring>
 #include <memory>
 #include <mutex>
 
@@ -64,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) {
@@ -110,16 +115,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;
-    if (!context->authority().empty()) {
-      host_str = context->authority_.c_str();
+    const string* host_str = nullptr;
+    if (!context->authority_.empty()) {
+      host_str = &context->authority_;
     } else if (!host_.empty()) {
-      host_str = host_.c_str();
+      host_str = &host_;
     }
-    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 = SliceFromCopiedString(*host_str);
     }
     c_call = grpc_channel_create_call(
         c_channel_, context->propagate_from_call_,