From: Otto Moerbeek Date: Fri, 23 Oct 2020 08:37:04 +0000 (+0200) Subject: Use move semantics for putting things in packetcache and X-Git-Tag: dnsdist-1.6.0-alpha0~11^2~5 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9e6128d057614510db8343765fc604c53e09aa92;p=thirdparty%2Fpdns.git Use move semantics for putting things in packetcache and tweak sizes to a better estimate, taking into account that the final message will *include* the reponse part. --- diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index 9d8daeaa07..0ee0b52210 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -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: "< pbMessage; if (pbData) { // We take the inmutable string form the cache an are appending a few values - pbMessage = make_unique(pbData->d_message, pbData->d_response, 128, 128); // The extra bytes we are going to add + pbMessage = make_unique(pbData->d_message, pbData->d_response, 64, 10); // The extra bytes we are going to add } else { - pbMessage = make_unique(128, 128); + pbMessage = make_unique(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); diff --git a/pdns/protozero.hh b/pdns/protozero.hh index 08332b3037..3f985ef7f4 100644 --- a/pdns/protozero.hh +++ b/pdns/protozero.hh @@ -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 diff --git a/pdns/recpacketcache.cc b/pdns/recpacketcache.cc index 94c979c50f..a938c12cd5 100644 --- a/pdns/recpacketcache.cc +++ b/pdns/recpacketcache.cc @@ -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(); 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); diff --git a/pdns/recpacketcache.hh b/pdns/recpacketcache.hh index 972e136c0a..da68178e07 100644 --- a/pdns/recpacketcache.hh +++ b/pdns/recpacketcache.hh @@ -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);