Skip to content

Commit ee3ca63

Browse files
committed
More performance improvement to avoid unnecessary chrono access
1 parent 4c932e1 commit ee3ca63

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

httplib.h

+25-1
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ struct Request {
669669
bool is_chunked_content_provider_ = false;
670670
size_t authorization_count_ = 0;
671671
std::chrono::time_point<std::chrono::steady_clock> start_time_ =
672-
std::chrono::steady_clock::now();
672+
std::chrono::steady_clock::time_point::min();
673673
};
674674

675675
struct Response {
@@ -8101,6 +8101,9 @@ inline Result ClientImpl::send_with_content_provider(
81018101
req.headers = headers;
81028102
req.path = path;
81038103
req.progress = progress;
8104+
if (global_timeout_msec_ > 0) {
8105+
req.start_time_ = std::chrono::steady_clock::now();
8106+
}
81048107

81058108
auto error = Error::Success;
81068109

@@ -8283,6 +8286,9 @@ inline Result ClientImpl::Get(const std::string &path, const Headers &headers,
82838286
req.path = path;
82848287
req.headers = headers;
82858288
req.progress = std::move(progress);
8289+
if (global_timeout_msec_ > 0) {
8290+
req.start_time_ = std::chrono::steady_clock::now();
8291+
}
82868292

82878293
return send_(std::move(req));
82888294
}
@@ -8348,6 +8354,9 @@ inline Result ClientImpl::Get(const std::string &path, const Headers &headers,
83488354
return content_receiver(data, data_length);
83498355
};
83508356
req.progress = std::move(progress);
8357+
if (global_timeout_msec_ > 0) {
8358+
req.start_time_ = std::chrono::steady_clock::now();
8359+
}
83518360

83528361
return send_(std::move(req));
83538362
}
@@ -8393,6 +8402,9 @@ inline Result ClientImpl::Head(const std::string &path,
83938402
req.method = "HEAD";
83948403
req.headers = headers;
83958404
req.path = path;
8405+
if (global_timeout_msec_ > 0) {
8406+
req.start_time_ = std::chrono::steady_clock::now();
8407+
}
83968408

83978409
return send_(std::move(req));
83988410
}
@@ -8810,6 +8822,9 @@ inline Result ClientImpl::Delete(const std::string &path,
88108822
req.headers = headers;
88118823
req.path = path;
88128824
req.progress = progress;
8825+
if (global_timeout_msec_ > 0) {
8826+
req.start_time_ = std::chrono::steady_clock::now();
8827+
}
88138828

88148829
if (!content_type.empty()) { req.set_header("Content-Type", content_type); }
88158830
req.body.assign(body, content_length);
@@ -8857,6 +8872,9 @@ inline Result ClientImpl::Options(const std::string &path,
88578872
req.method = "OPTIONS";
88588873
req.headers = headers;
88598874
req.path = path;
8875+
if (global_timeout_msec_ > 0) {
8876+
req.start_time_ = std::chrono::steady_clock::now();
8877+
}
88608878

88618879
return send_(std::move(req));
88628880
}
@@ -9540,6 +9558,9 @@ inline bool SSLClient::connect_with_proxy(
95409558
Request req2;
95419559
req2.method = "CONNECT";
95429560
req2.path = host_and_port_;
9561+
if (global_timeout_msec_ > 0) {
9562+
req2.start_time_ = std::chrono::steady_clock::now();
9563+
}
95439564
return process_request(strm, req2, proxy_res, false, error);
95449565
})) {
95459566
// Thread-safe to close everything because we are assuming there are no
@@ -9568,6 +9589,9 @@ inline bool SSLClient::connect_with_proxy(
95689589
req3, auth, 1, detail::random_string(10),
95699590
proxy_digest_auth_username_, proxy_digest_auth_password_,
95709591
true));
9592+
if (global_timeout_msec_ > 0) {
9593+
req3.start_time_ = std::chrono::steady_clock::now();
9594+
}
95719595
return process_request(strm, req3, proxy_res, false, error);
95729596
})) {
95739597
// Thread-safe to close everything because we are assuming there are

0 commit comments

Comments
 (0)