@@ -152,6 +152,10 @@ void UpdateIoBufCapacity(const io::IoBuf& io_buf, ConnectionStats* stats,
152152 }
153153}
154154
155+ size_t UsedMemoryInternal (const Connection::PipelineMessage& msg) {
156+ return sizeof (msg) + msg.HeapMemory ();
157+ }
158+
155159struct TrafficLogger {
156160 // protects agains closing the file while writing or data races when opening the file.
157161 // Also, makes sure that LogTraffic are executed atomically.
@@ -377,19 +381,6 @@ class PipelineCacheSizeTracker {
377381
378382thread_local PipelineCacheSizeTracker tl_pipe_cache_sz_tracker;
379383
380- void Connection::PipelineMessage::SetArgs (const RespVec& args) {
381- auto * next = storage.data ();
382- for (size_t i = 0 ; i < args.size (); ++i) {
383- RespExpr::Buffer buf = args[i].GetBuf ();
384- size_t s = buf.size ();
385- if (s)
386- memcpy (next, buf.data (), s);
387- next[s] = ' \0 ' ;
388- this ->args [i] = MutableSlice (next, s);
389- next += (s + 1 );
390- }
391- }
392-
393384Connection::MCPipelineMessage::MCPipelineMessage (MemcacheParser::Command cmd_in,
394385 std::string_view value_in)
395386 : cmd{std::move (cmd_in)}, value{value_in}, backing_size{0 } {
@@ -425,23 +416,13 @@ Connection::MCPipelineMessage::MCPipelineMessage(MemcacheParser::Command cmd_in,
425416 }
426417}
427418
428- void Connection::PipelineMessage::Reset (size_t nargs, size_t capacity) {
429- storage.resize (capacity);
430- args.resize (nargs);
431- }
432-
433- size_t Connection::PipelineMessage::StorageCapacity () const {
434- return storage.capacity () + args.capacity ();
435- }
436-
437419size_t Connection::MessageHandle::UsedMemory () const {
438420 struct MessageSize {
439421 size_t operator ()(const PubMessagePtr& msg) {
440422 return sizeof (PubMessage) + (msg->channel .size () + msg->message .size ());
441423 }
442424 size_t operator ()(const PipelineMessagePtr& msg) {
443- return sizeof (PipelineMessage) + msg->args .capacity () * sizeof (MutableSlice) +
444- msg->storage .capacity ();
425+ return UsedMemoryInternal (*msg);
445426 }
446427 size_t operator ()(const MonitorMessage& msg) {
447428 return msg.capacity ();
@@ -555,11 +536,10 @@ void Connection::AsyncOperations::operator()(const PubMessage& pub_msg) {
555536}
556537
557538void Connection::AsyncOperations::operator ()(Connection::PipelineMessage& msg) {
558- DVLOG (2 ) << " Dispatching pipeline: " << ToSV ( msg.args . front () );
539+ DVLOG (2 ) << " Dispatching pipeline: " << msg.Front ( );
559540
560541 ++self->local_stats_ .cmds ;
561- self->service_ ->DispatchCommand (ParsedArgs{msg.args }, self->reply_builder_ .get (),
562- self->cc_ .get ());
542+ self->service_ ->DispatchCommand (ParsedArgs{msg}, self->reply_builder_ .get (), self->cc_ .get ());
563543
564544 self->last_interaction_ = time (nullptr );
565545 self->skip_next_squashing_ = false ;
@@ -644,7 +624,7 @@ Connection::Connection(Protocol protocol, util::HttpListenerBase* http_listener,
644624 static atomic_uint32_t next_id{1 };
645625
646626 constexpr size_t kReqSz = sizeof (Connection::PipelineMessage);
647- static_assert (kReqSz <= 256 && kReqSz >= 200 );
627+ static_assert (kReqSz <= 256 );
648628
649629 switch (protocol) {
650630 case Protocol::REDIS:
@@ -1537,16 +1517,17 @@ void Connection::SquashPipeline() {
15371517 DCHECK_EQ (reply_builder_->GetProtocol (), Protocol::REDIS); // Only Redis is supported.
15381518
15391519 vector<ParsedArgs> squash_cmds;
1540- squash_cmds.reserve (dispatch_q_.size ());
1520+
1521+ squash_cmds.reserve (std::min<uint32_t >(dispatch_q_.size (), pipeline_squash_limit_cached));
15411522
15421523 uint64_t start = CycleClock::Now ();
15431524
15441525 for (const auto & msg : dispatch_q_) {
15451526 CHECK (holds_alternative<PipelineMessagePtr>(msg.handle ))
15461527 << msg.handle .index () << " on " << DebugInfo ();
15471528
1548- auto & pmsg = get<PipelineMessagePtr>(msg.handle );
1549- squash_cmds.emplace_back (ParsedArgs ( pmsg-> args ) );
1529+ const auto & pmsg = get<PipelineMessagePtr>(msg.handle );
1530+ squash_cmds.emplace_back (* pmsg);
15501531 if (squash_cmds.size () >= pipeline_squash_limit_cached) {
15511532 // We reached the limit of commands to squash, so we dispatch them.
15521533 break ;
@@ -1755,25 +1736,15 @@ void Connection::AsyncFiber() {
17551736}
17561737
17571738Connection::PipelineMessagePtr Connection::FromArgs (const RespVec& args) {
1758- DCHECK (!args.empty ());
1759- size_t backed_sz = 0 ;
1760- for (const auto & arg : args) {
1761- CHECK_EQ (RespExpr::STRING, arg.type );
1762- backed_sz += arg.GetBuf ().size () + 1 ; // for '\0'
1763- }
1764- DCHECK (backed_sz);
1765-
1766- static_assert (alignof (PipelineMessage) == 8 );
1767-
17681739 PipelineMessagePtr ptr;
1769- if (ptr = GetFromPipelinePool (); ptr) {
1770- ptr->Reset (args.size (), backed_sz);
1771- } else {
1740+ if (ptr = GetFromPipelinePool (); !ptr) {
17721741 // We must construct in place here, since there is a slice that uses memory locations
1773- ptr = make_unique<PipelineMessage>(args. size (), backed_sz );
1742+ ptr = make_unique<PipelineMessage>();
17741743 }
17751744
1776- ptr->SetArgs (args);
1745+ auto map = [](const RespExpr& expr) { return expr.GetView (); };
1746+ auto range = base::it::Transform (map, base::it::Range (args.begin (), args.end ()));
1747+ ptr->Assign (range.begin (), range.end (), args.size ());
17771748 return ptr;
17781749}
17791750
@@ -1782,7 +1753,7 @@ void Connection::ShrinkPipelinePool() {
17821753 return ;
17831754
17841755 if (tl_pipe_cache_sz_tracker.CheckAndUpdateWatermark (pipeline_req_pool_.size ())) {
1785- stats_->pipeline_cmd_cache_bytes -= pipeline_req_pool_.back ()-> StorageCapacity ( );
1756+ stats_->pipeline_cmd_cache_bytes -= UsedMemoryInternal (* pipeline_req_pool_.back ());
17861757 pipeline_req_pool_.pop_back ();
17871758 }
17881759}
@@ -1792,7 +1763,7 @@ Connection::PipelineMessagePtr Connection::GetFromPipelinePool() {
17921763 return nullptr ;
17931764
17941765 auto ptr = std::move (pipeline_req_pool_.back ());
1795- stats_->pipeline_cmd_cache_bytes -= ptr-> StorageCapacity ( );
1766+ stats_->pipeline_cmd_cache_bytes -= UsedMemoryInternal (*ptr );
17961767 pipeline_req_pool_.pop_back ();
17971768 return ptr;
17981769}
@@ -1968,7 +1939,7 @@ void Connection::RecycleMessage(MessageHandle msg) {
19681939 pending_pipeline_cmd_cnt_--;
19691940 pending_pipeline_bytes_ -= used_mem;
19701941 if (stats_->pipeline_cmd_cache_bytes < qbp.pipeline_cache_limit ) {
1971- stats_->pipeline_cmd_cache_bytes += (* pipe)-> StorageCapacity ( );
1942+ stats_->pipeline_cmd_cache_bytes += UsedMemoryInternal (*(* pipe));
19721943 pipeline_req_pool_.push_back (std::move (*pipe));
19731944 }
19741945 }
0 commit comments