gydong commented on a change in pull request #1175:
URL: https://github.com/apache/incubator-brpc/pull/1175#discussion_r474359936



##########
File path: src/brpc/policy/baidu_rpc_protocol.cpp
##########
@@ -148,8 +148,14 @@ void SendRpcResponse(int64_t correlation_id,
     Socket* sock = accessor.get_sending_socket();
     std::unique_ptr<Controller, LogErrorTextAndDelete> recycle_cntl(cntl);
     ConcurrencyRemover concurrency_remover(method_status, cntl, received_us);
-    std::unique_ptr<const google::protobuf::Message> recycle_req(req);
-    std::unique_ptr<const google::protobuf::Message> recycle_res(res);
+    butil::ArenaObjDeleter<const google::protobuf::Message> 
+            del(!cntl->get_memory_arena()->OwnObject());
+    std::unique_ptr<const google::protobuf::Message, 
+                    butil::ArenaObjDeleter<const google::protobuf::Message>
+    > recycle_req(req, del);

Review comment:
       这种indent方式有些怪,可以统一成下面这样吗
   ```
   
       std::unique_ptr<google::protobuf::Message, 
               butil::ArenaObjDeleter<google::protobuf::Message>> req;
       std::unique_ptr<google::protobuf::Message, 
               butil::ArenaObjDeleter<google::protobuf::Message>> res;
   ```

##########
File path: src/brpc/controller.h
##########
@@ -510,6 +511,17 @@ friend void 
policy::ProcessThriftRequest(InputMessageBase*);
     // -1 means no deadline.
     int64_t deadline_us() const { return _deadline_us; }
 
+    std::shared_ptr<butil::MemoryArenaDefault> get_memory_arena() {
+        if (!_arena) {
+            _arena.reset(new butil::MemoryArenaDefault);
+        }
+        return _arena;
+    }
+    void share_memory_arena(Controller *cntl) {

Review comment:
       为保持和现有代码风格一致,引用和指针符号统一对齐到左边与类型挨着吧

##########
File path: src/butil/memory_arena.h
##########
@@ -0,0 +1,159 @@
+
+#ifndef BUTIL_MEMORY_ARENA_H
+#define BUTIL_MEMORY_ARENA_H
+
+#include <memory>
+#include "google/protobuf/message.h"
+#include "butil/logging.h"
+
+namespace butil {
+
+template<class T>
+struct ArenaObjDeleter {
+    constexpr ArenaObjDeleter() noexcept = default;
+
+    ArenaObjDeleter(bool own) noexcept
+        : _own_obj(own) {
+    }
+
+    void operator()(T *ptr) const {
+        if (_own_obj) {
+            VLOG(199) << "delete!";

Review comment:
       VLOG(RPC_VLOG_LEVEL)

##########
File path: src/butil/memory_arena.h
##########
@@ -0,0 +1,159 @@
+
+#ifndef BUTIL_MEMORY_ARENA_H
+#define BUTIL_MEMORY_ARENA_H
+
+#include <memory>
+#include "google/protobuf/message.h"
+#include "butil/logging.h"
+
+namespace butil {
+
+template<class T>
+struct ArenaObjDeleter {
+    constexpr ArenaObjDeleter() noexcept = default;
+
+    ArenaObjDeleter(bool own) noexcept
+        : _own_obj(own) {
+    }
+
+    void operator()(T *ptr) const {
+        if (_own_obj) {
+            VLOG(199) << "delete!";
+            delete ptr;
+        }
+    }
+
+private:
+    bool _own_obj;
+};
+
+namespace internal {
+
+// Template meta to check protobuf version.
+//   protobuf 3.x: ArenaCheck::test<T>(0) => int8_t
+//   protobuf 2.x: ArenaCheck::test<T>(0) => int64_t
+struct ArenaCheck {
+  template<class T> static int8_t test(decltype(&T::GetArena));
+  template<class T> static int64_t test(...);
+};
+
+// Don't specialize by yourselves, MemoryArenaDefault and MemoryArenaSimple
+// will be enough.
+// A simple implementation.
+template<class Def, class Enable = void>
+class MemoryArena {
+public:
+    std::unique_ptr<
+        google::protobuf::Message, 
+        ArenaObjDeleter<google::protobuf::Message>
+    > 
+    CreateMessage(const google::protobuf::Message &proto_type) {
+        google::protobuf::Message *msg = proto_type.New();
+        VLOG(199) << __FUNCTION__;

Review comment:
       __FUNCTION__ 是哪里定义的宏,用__func__变量会更标准些吧




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to