Skip to content

Commit 88d0668

Browse files
authored
Minor enhancement: move some smart pointers around instead of copying them (#1954)
1 parent da7f065 commit 88d0668

3 files changed

Lines changed: 30 additions & 27 deletions

File tree

examples/benchmark/JsonCtrl.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ void JsonCtrl::asyncHandleHttpRequest(
66
{
77
Json::Value ret;
88
ret["message"] = "Hello, World!";
9-
auto resp = HttpResponse::newHttpJsonResponse(ret);
9+
auto resp = HttpResponse::newHttpJsonResponse(std::move(ret));
1010
callback(resp);
1111
}

lib/src/HttpRequestParser.cc

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -99,31 +99,32 @@ bool HttpRequestParser::processRequestLine(const char *begin, const char *end)
9999

100100
HttpRequestImplPtr HttpRequestParser::makeRequestForPool(HttpRequestImpl *ptr)
101101
{
102-
std::weak_ptr<HttpRequestParser> weakPtr = shared_from_this();
103-
return std::shared_ptr<HttpRequestImpl>(ptr, [weakPtr](HttpRequestImpl *p) {
104-
auto thisPtr = weakPtr.lock();
105-
if (thisPtr)
106-
{
107-
if (thisPtr->loop_->isInLoopThread())
108-
{
109-
p->reset();
110-
thisPtr->requestsPool_.emplace_back(
111-
thisPtr->makeRequestForPool(p));
112-
}
113-
else
102+
return std::shared_ptr<HttpRequestImpl>(
103+
ptr, [weakPtr = weak_from_this()](HttpRequestImpl *p) {
104+
auto thisPtr = weakPtr.lock();
105+
if (thisPtr)
114106
{
115-
thisPtr->loop_->queueInLoop([thisPtr, p]() {
107+
if (thisPtr->loop_->isInLoopThread())
108+
{
116109
p->reset();
117110
thisPtr->requestsPool_.emplace_back(
118111
thisPtr->makeRequestForPool(p));
119-
});
112+
}
113+
else
114+
{
115+
auto &loop = thisPtr->loop_;
116+
loop->queueInLoop([thisPtr = std::move(thisPtr), p]() {
117+
p->reset();
118+
thisPtr->requestsPool_.emplace_back(
119+
thisPtr->makeRequestForPool(p));
120+
});
121+
}
120122
}
121-
}
122-
else
123-
{
124-
delete p;
125-
}
126-
});
123+
else
124+
{
125+
delete p;
126+
}
127+
});
127128
}
128129

129130
void HttpRequestParser::reset()

lib/src/HttpServer.cc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -546,11 +546,12 @@ void HttpServer::httpRequestHandling(
546546
}
547547
}
548548

549-
binderPtr->handleRequest(
549+
auto &binderRef = *binderPtr;
550+
binderRef.handleRequest(
550551
req,
551552
// This is the actual callback being passed to controller
552-
[req, binderPtr, callback = std::move(callback)](
553-
const HttpResponsePtr &resp) {
553+
[req, binderPtr = std::move(binderPtr), callback = std::move(callback)](
554+
const HttpResponsePtr &resp) mutable {
554555
// Check if we need to cache the response
555556
if (resp->expiredTime() >= 0 && resp->statusCode() != k404NotFound)
556557
{
@@ -562,9 +563,10 @@ void HttpServer::httpRequestHandling(
562563
}
563564
else
564565
{
565-
loop->queueInLoop([binderPtr, resp]() {
566-
binderPtr->responseCache_.setThreadData(resp);
567-
});
566+
loop->queueInLoop(
567+
[binderPtr = std::move(binderPtr), resp]() {
568+
binderPtr->responseCache_.setThreadData(resp);
569+
});
568570
}
569571
}
570572
// post-handling aop

0 commit comments

Comments
 (0)