]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Use move semantics for putting things in packetcache and
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Fri, 23 Oct 2020 08:37:04 +0000 (10:37 +0200)
committerOtto Moerbeek <otto@drijf.net>
Tue, 10 Nov 2020 08:17:13 +0000 (09:17 +0100)
tweak sizes to a better estimate, taking into account that the
final message will *include* the reponse part.

pdns/pdns_recursor.cc
pdns/protozero.hh
pdns/recpacketcache.cc
pdns/recpacketcache.hh

index 9d8daeaa078ade0dd7a7bc943d2231ab47f0a0d6..0ee0b52210404b71a4db7b95ae24363fba833ffb 100644 (file)
@@ -1945,9 +1945,8 @@ static void startDoResolve(void *p)
                                             pw.getHeader()->rcode == RCode::ServFail ? SyncRes::s_packetcacheservfailttl :
                                             min(minTTL,SyncRes::s_packetcachettl),
                                             dq.validationState,
-                                            pbDataForCache);
+                                            std::move(pbDataForCache));
       }
-      //      else cerr<<"Not putting in packet cache: "<<sr.wasVariable()<<endl;
     }
     else {
       bool hadError = sendResponseOverTCP(dc, packet);
@@ -2715,13 +2714,21 @@ static string* doProcessUDPQuestion(const std::string& question, const ComboAddr
         std::unique_ptr<pdns::ProtoZero::Message> pbMessage;
         if (pbData) {
           // We take the inmutable string form the cache an are appending a few values
-          pbMessage = make_unique<pdns::ProtoZero::Message>(pbData->d_message, pbData->d_response, 128, 128); // The extra bytes we are going to add
+          pbMessage = make_unique<pdns::ProtoZero::Message>(pbData->d_message, pbData->d_response, 64, 10); // The extra bytes we are going to add
         } else {
-          pbMessage = make_unique<pdns::ProtoZero::Message>(128, 128);
+          pbMessage = make_unique<pdns::ProtoZero::Message>(64, 10);
           pbMessage->setType(2); // Response
           pbMessage->setServerIdentity(SyncRes::s_serverID);
         }
 
+        // In response part
+        if (g_useKernelTimestamp && tv.tv_sec) {
+          pbMessage->setQueryTime(tv.tv_sec, tv.tv_usec);
+        }
+        else {
+          pbMessage->setQueryTime(g_now.tv_sec, g_now.tv_usec);
+        }
+        // In message part
         Netmask requestorNM(source, source.sin4.sin_family == AF_INET ? luaconfsLocal->protobufMaskV4 : luaconfsLocal->protobufMaskV6);
         ComboAddress requestor = requestorNM.getMaskedNetwork();
         pbMessage->setMessageIdentity(uniqueId);
@@ -2731,12 +2738,6 @@ static string* doProcessUDPQuestion(const std::string& question, const ComboAddr
         pbMessage->setId(dh->id);
 
         pbMessage->setTime();
-        if (g_useKernelTimestamp && tv.tv_sec) {
-          pbMessage->setQueryTime(tv.tv_sec, tv.tv_usec);
-        }
-        else {
-          pbMessage->setQueryTime(g_now.tv_sec, g_now.tv_usec);
-        }
         pbMessage->setEDNSSubnet(ednssubnet.source, ednssubnet.source.isIPv4() ? luaconfsLocal->protobufMaskV4 : luaconfsLocal->protobufMaskV6);
         pbMessage->setRequestorId(requestorId);
         pbMessage->setDeviceId(deviceId);
index 08332b3037a82ea36f5d5faee7a6cc9d1ed9e4a7..3f985ef7f4b5feb060256a8f9e845d18e7cf38e4 100644 (file)
@@ -47,10 +47,10 @@ namespace pdns {
       Message(const std::string& buf1, const std::string& buf2, std::string::size_type sz1, std::string::size_type sz2) :
         d_msgbuf{buf1}, d_rspbuf{buf2}, d_message{d_msgbuf}, d_response{d_rspbuf}
       {
-        // We expect to grow the buffers
+        // We expect to grow the buffers, in the end the d_message will contains the (grown) d_response
         // This is extra space in addition to what's already there
         // Different from what string.reserve() does
-        d_message.reserve(sz1);
+        d_message.reserve(sz1 + d_rspbuf.length() + sz2);
         d_response.reserve(sz2);
       }
       const std::string& getMessageBuf() const
index 94c979c50f9aaf19457b7108bff1e4765abf5087..a938c12cd54232819f45af8ad0054cf5dfd0decd 100644 (file)
@@ -141,7 +141,7 @@ bool RecursorPacketCache::getResponsePacket(unsigned int tag, const std::string&
 }
 
 
-void RecursorPacketCache::insertResponsePacket(unsigned int tag, uint32_t qhash, std::string&& query, const DNSName& qname, uint16_t qtype, uint16_t qclass, std::string&& responsePacket, time_t now, uint32_t ttl, const vState& valState, const OptPBData& pbdata)
+void RecursorPacketCache::insertResponsePacket(unsigned int tag, uint32_t qhash, std::string&& query, const DNSName& qname, uint16_t qtype, uint16_t qclass, std::string&& responsePacket, time_t now, uint32_t ttl, const vState& valState, OptPBData&& pbdata)
 {
   auto& idx = d_packetCache.get<HashTag>();
   auto range = idx.equal_range(tie(tag,qhash));
@@ -160,7 +160,7 @@ void RecursorPacketCache::insertResponsePacket(unsigned int tag, uint32_t qhash,
     iter->d_vstate = valState;
 #ifdef HAVE_PROTOBUF
     if (pbdata) {
-      iter->d_pbdata = *pbdata;
+      iter->d_pbdata = std::move(*pbdata);
     }
 #endif
 
@@ -178,7 +178,7 @@ void RecursorPacketCache::insertResponsePacket(unsigned int tag, uint32_t qhash,
     e.d_vstate = valState;
 #ifdef HAVE_PROTOBUF
     if (pbdata) {
-      e.d_pbdata = *pbdata;
+      e.d_pbdata = std::move(*pbdata);
     }
 #endif
     d_packetCache.insert(e);
index 972e136c0ae329b0a7ad73638b9be3f227ef8c29..da68178e071e05c9b87a4cbd494c6d2a5ad64b52 100644 (file)
@@ -64,7 +64,7 @@ public:
   bool getResponsePacket(unsigned int tag, const std::string& queryPacket, const DNSName& qname, uint16_t qtype, uint16_t qclass, time_t now, std::string* responsePacket, uint32_t* age, vState* valState, uint32_t* qhash, OptPBData* pbdata);
   bool getResponsePacket(unsigned int tag, const std::string& queryPacket, DNSName& qname, uint16_t *qtype, uint16_t* qclass, time_t now, std::string* responsePacket, uint32_t* age, vState* valState, uint32_t* qhash, OptPBData* pbdata);
   
-  void insertResponsePacket(unsigned int tag, uint32_t qhash, std::string&& query, const DNSName& qname, uint16_t qtype, uint16_t qclass, std::string&& responsePacket, time_t now, uint32_t ttl, const vState& valState, const OptPBData& pbdata);
+  void insertResponsePacket(unsigned int tag, uint32_t qhash, std::string&& query, const DNSName& qname, uint16_t qtype, uint16_t qclass, std::string&& responsePacket, time_t now, uint32_t ttl, const vState& valState, OptPBData&& pbdata);
   void doPruneTo(size_t maxSize=250000);
   uint64_t doDump(int fd);
   int doWipePacketCache(const DNSName& name, uint16_t qtype=0xffff, bool subtree=false);