You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brpc.apache.org by GitBox <gi...@apache.org> on 2021/05/19 05:31:13 UTC

[GitHub] [incubator-brpc] huntinux edited a comment on issue #535: 支持pb3的arena分配

huntinux edited a comment on issue #535:
URL: https://github.com/apache/incubator-brpc/issues/535#issuecomment-843758103


   尝试改了http协议里面的pb创建方式
   `diff --git a/src/brpc/policy/http_rpc_protocol.cpp b/src/brpc/policy/http_rpc_protocol.cpp
   index 8787574..c5a498d 100644
   --- a/src/brpc/policy/http_rpc_protocol.cpp
   +++ b/src/brpc/policy/http_rpc_protocol.cpp
   @@ -16,6 +16,7 @@
    //          Ge,Jun (gejun@baidu.com)
    
    #include <google/protobuf/descriptor.h>             // MethodDescriptor
   +#include <google/protobuf/arena.h>             // MethodDescriptor
    #include <gflags/gflags.h>
    #include <json2pb/pb_to_json.h>                    // ProtoMessageToJson
    #include <json2pb/json_to_pb.h>                    // JsonToProtoMessage
   @@ -71,6 +72,8 @@ DEFINE_bool(pb_enum_as_number, false, "[Not recommended] Convert enums in "
                "protobuf to json as numbers, affecting both client-side and "
                "server-side");
    
   +DEFINE_bool(enable_pb_arena, false, "Create request/response message on arena");
   +
    // Read user address from the header specified by -http_header_of_user_ip
    static bool GetUserAddressFromHeaderImpl(const HttpHeader& headers,
                                             butil::EndPoint* user_addr) {
   @@ -685,6 +688,7 @@ public:
            : _cntl(std::move(s._cntl))
            , _req(std::move(s._req))
            , _res(std::move(s._res))
   +        , _arena(std::move(s._arena))
            , _method_status(std::move(s._method_status))
            , _received_us(s._received_us)
            , _h2_stream_id(s._h2_stream_id) {
   @@ -693,6 +697,7 @@ public:
    
        void own_request(google::protobuf::Message* req) { _req.reset(req); }
        void own_response(google::protobuf::Message* res) { _res.reset(res); }
   +    void own_arena(google::protobuf::Arena* arena) { _arena.reset(arena); }
        void set_method_status(MethodStatus* ms) { _method_status = ms; }
        void set_received_us(int64_t t) { _received_us = t; }
        void set_h2_stream_id(int id) { _h2_stream_id = id; }
   @@ -701,6 +706,7 @@ private:
        std::unique_ptr<Controller, LogErrorTextAndDelete> _cntl;
        std::unique_ptr<google::protobuf::Message> _req;
        std::unique_ptr<google::protobuf::Message> _res;
   +    std::unique_ptr<google::protobuf::Arena> _arena;
        MethodStatus* _method_status;
        int64_t _received_us;
        int _h2_stream_id;
   @@ -1424,10 +1430,19 @@ void ProcessHttpRequest(InputMessageBase *msg) {
        google::protobuf::Service* svc = sp->service;
        const google::protobuf::MethodDescriptor* method = sp->method;
        accessor.set_method(method);
   -    google::protobuf::Message* req = svc->GetRequestPrototype(method).New();
   -    resp_sender.own_request(req);
   -    google::protobuf::Message* res = svc->GetResponsePrototype(method).New();
   -    resp_sender.own_response(res);
   +    google::protobuf::Message* req = nullptr;
   +    google::protobuf::Message* res = nullptr;
   +    if (FLAGS_enable_pb_arena) {
   +      google::protobuf::Arena* arena = new google::protobuf::Arena();
   +      req = svc->GetRequestPrototype(method).New(arena);
   +      res = svc->GetResponsePrototype(method).New(arena);
   +      resp_sender.own_arena(arena);
   +    } else {
   +      req = svc->GetRequestPrototype(method).New();
   +      resp_sender.own_request(req);
   +      res = svc->GetResponsePrototype(method).New();
   +      resp_sender.own_response(res);
   +    }
    
        if (__builtin_expect(!req || !res, 0)) {
            PLOG(FATAL) << "Fail to new req or res";`


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org