Skip to content

Commit

Permalink
Minor enhancement: move some smart pointers around instead of copying…
Browse files Browse the repository at this point in the history
  • Loading branch information
itrofimow authored Feb 15, 2024
1 parent da7f065 commit 88d0668
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
2 changes: 1 addition & 1 deletion examples/benchmark/JsonCtrl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ void JsonCtrl::asyncHandleHttpRequest(
{
Json::Value ret;
ret["message"] = "Hello, World!";
auto resp = HttpResponse::newHttpJsonResponse(ret);
auto resp = HttpResponse::newHttpJsonResponse(std::move(ret));
callback(resp);
}
41 changes: 21 additions & 20 deletions lib/src/HttpRequestParser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,31 +99,32 @@ bool HttpRequestParser::processRequestLine(const char *begin, const char *end)

HttpRequestImplPtr HttpRequestParser::makeRequestForPool(HttpRequestImpl *ptr)
{
std::weak_ptr<HttpRequestParser> weakPtr = shared_from_this();
return std::shared_ptr<HttpRequestImpl>(ptr, [weakPtr](HttpRequestImpl *p) {
auto thisPtr = weakPtr.lock();
if (thisPtr)
{
if (thisPtr->loop_->isInLoopThread())
{
p->reset();
thisPtr->requestsPool_.emplace_back(
thisPtr->makeRequestForPool(p));
}
else
return std::shared_ptr<HttpRequestImpl>(
ptr, [weakPtr = weak_from_this()](HttpRequestImpl *p) {
auto thisPtr = weakPtr.lock();
if (thisPtr)
{
thisPtr->loop_->queueInLoop([thisPtr, p]() {
if (thisPtr->loop_->isInLoopThread())
{
p->reset();
thisPtr->requestsPool_.emplace_back(
thisPtr->makeRequestForPool(p));
});
}
else
{
auto &loop = thisPtr->loop_;
loop->queueInLoop([thisPtr = std::move(thisPtr), p]() {
p->reset();
thisPtr->requestsPool_.emplace_back(
thisPtr->makeRequestForPool(p));
});
}
}
}
else
{
delete p;
}
});
else
{
delete p;
}
});
}

void HttpRequestParser::reset()
Expand Down
14 changes: 8 additions & 6 deletions lib/src/HttpServer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -546,11 +546,12 @@ void HttpServer::httpRequestHandling(
}
}

binderPtr->handleRequest(
auto &binderRef = *binderPtr;
binderRef.handleRequest(
req,
// This is the actual callback being passed to controller
[req, binderPtr, callback = std::move(callback)](
const HttpResponsePtr &resp) {
[req, binderPtr = std::move(binderPtr), callback = std::move(callback)](
const HttpResponsePtr &resp) mutable {
// Check if we need to cache the response
if (resp->expiredTime() >= 0 && resp->statusCode() != k404NotFound)
{
Expand All @@ -562,9 +563,10 @@ void HttpServer::httpRequestHandling(
}
else
{
loop->queueInLoop([binderPtr, resp]() {
binderPtr->responseCache_.setThreadData(resp);
});
loop->queueInLoop(
[binderPtr = std::move(binderPtr), resp]() {
binderPtr->responseCache_.setThreadData(resp);
});
}
}
// post-handling aop
Expand Down

0 comments on commit 88d0668

Please sign in to comment.