Skip to content

Commit

Permalink
Reduce object copy (#1767)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmy-park authored Jan 28, 2024
1 parent fceada9 commit ffc294d
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -2065,7 +2065,7 @@ void hosted_at(const std::string &hostname, std::vector<std::string> &addrs);

std::string append_query_params(const std::string &path, const Params &params);

std::pair<std::string, std::string> make_range_header(Ranges ranges);
std::pair<std::string, std::string> make_range_header(const Ranges &ranges);

std::pair<std::string, std::string>
make_basic_authentication_header(const std::string &username,
Expand Down Expand Up @@ -2571,7 +2571,7 @@ inline std::string trim_double_quotes_copy(const std::string &s) {

inline void split(const char *b, const char *e, char d,
std::function<void(const char *, const char *)> fn) {
return split(b, e, d, (std::numeric_limits<size_t>::max)(), fn);
return split(b, e, d, (std::numeric_limits<size_t>::max)(), std::move(fn));
}

inline void split(const char *b, const char *e, char d, size_t m,
Expand Down Expand Up @@ -5208,10 +5208,11 @@ inline std::string append_query_params(const std::string &path,
}

// Header utilities
inline std::pair<std::string, std::string> make_range_header(Ranges ranges) {
inline std::pair<std::string, std::string>
make_range_header(const Ranges &ranges) {
std::string field = "bytes=";
auto i = 0;
for (auto r : ranges) {
for (const auto &r : ranges) {
if (i != 0) { field += ", "; }
if (r.first != -1) { field += std::to_string(r.first); }
field += '-';
Expand Down Expand Up @@ -5364,7 +5365,7 @@ inline void Response::set_content_provider(
set_header("Content-Type", content_type);
content_length_ = in_length;
if (in_length > 0) { content_provider_ = std::move(provider); }
content_provider_resource_releaser_ = resource_releaser;
content_provider_resource_releaser_ = std::move(resource_releaser);
is_chunked_content_provider_ = false;
}

Expand All @@ -5374,7 +5375,7 @@ inline void Response::set_content_provider(
set_header("Content-Type", content_type);
content_length_ = 0;
content_provider_ = detail::ContentProviderAdapter(std::move(provider));
content_provider_resource_releaser_ = resource_releaser;
content_provider_resource_releaser_ = std::move(resource_releaser);
is_chunked_content_provider_ = false;
}

Expand All @@ -5384,7 +5385,7 @@ inline void Response::set_chunked_content_provider(
set_header("Content-Type", content_type);
content_length_ = 0;
content_provider_ = detail::ContentProviderAdapter(std::move(provider));
content_provider_resource_releaser_ = resource_releaser;
content_provider_resource_releaser_ = std::move(resource_releaser);
is_chunked_content_provider_ = true;
}

Expand Down Expand Up @@ -7612,14 +7613,15 @@ inline Result ClientImpl::Get(const std::string &path, const Params &params,
if (params.empty()) { return Get(path, headers); }

std::string path_with_query = append_query_params(path, params);
return Get(path_with_query, headers, progress);
return Get(path_with_query, headers, std::move(progress));
}

inline Result ClientImpl::Get(const std::string &path, const Params &params,
const Headers &headers,
ContentReceiver content_receiver,
Progress progress) {
return Get(path, params, headers, nullptr, content_receiver, progress);
return Get(path, params, headers, nullptr, std::move(content_receiver),
std::move(progress));
}

inline Result ClientImpl::Get(const std::string &path, const Params &params,
Expand All @@ -7628,12 +7630,13 @@ inline Result ClientImpl::Get(const std::string &path, const Params &params,
ContentReceiver content_receiver,
Progress progress) {
if (params.empty()) {
return Get(path, headers, response_handler, content_receiver, progress);
return Get(path, headers, std::move(response_handler),
std::move(content_receiver), std::move(progress));
}

std::string path_with_query = append_query_params(path, params);
return Get(path_with_query, headers, response_handler, content_receiver,
progress);
return Get(path_with_query, headers, std::move(response_handler),
std::move(content_receiver), std::move(progress));
}

inline Result ClientImpl::Head(const std::string &path) {
Expand Down Expand Up @@ -9008,19 +9011,20 @@ inline Result Client::Get(const std::string &path, const Headers &headers,
}
inline Result Client::Get(const std::string &path, const Params &params,
const Headers &headers, Progress progress) {
return cli_->Get(path, params, headers, progress);
return cli_->Get(path, params, headers, std::move(progress));
}
inline Result Client::Get(const std::string &path, const Params &params,
const Headers &headers,
ContentReceiver content_receiver, Progress progress) {
return cli_->Get(path, params, headers, content_receiver, progress);
return cli_->Get(path, params, headers, std::move(content_receiver),
std::move(progress));
}
inline Result Client::Get(const std::string &path, const Params &params,
const Headers &headers,
ResponseHandler response_handler,
ContentReceiver content_receiver, Progress progress) {
return cli_->Get(path, params, headers, response_handler, content_receiver,
progress);
return cli_->Get(path, params, headers, std::move(response_handler),
std::move(content_receiver), std::move(progress));
}

inline Result Client::Head(const std::string &path) { return cli_->Head(path); }
Expand Down

0 comments on commit ffc294d

Please sign in to comment.