Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parseHeader optimization: no intermediate std::vector #1062

Merged
merged 1 commit into from
Jun 8, 2024

Conversation

dayjaby
Copy link
Contributor

@dayjaby dayjaby commented Jun 6, 2024

The header is parsed line by line. However, it's not necessary to store the lines in an intermediate std::vector before doing the parsing. As by doing so, we used 2 heap allocations per header line, which are optimized away by this PR.

Test example:

#include <memory>
#include <string>

#include "cpr/cpr.h"
#include "cpr/cprtypes.h"
#include "cpr/redirect.h"
#include "cpr/session.h"
#include "httpServer.hpp"

using namespace cpr;

int main() {
    HttpServer server;
    server.SetUp();
    std::cout << server.GetBaseUrl() << std::endl;
    Url url{server.GetBaseUrl() + "/hello.html"};
    Response response = cpr::Get(url);
    std::cout << response.header["content-type"];
    server.TearDown();
    return 0;
}

Before, measured with valgrind ./main:

==207737==   total heap usage: 138 allocs, 138 frees, 115,676 bytes allocated

With this PR:

==251897==   total heap usage: 132 allocs, 132 frees, 115,390 bytes allocated

Copy link
Member

@COM8 COM8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, ah! Thanks @dayjaby.

Approved and once the CI is done, we are ready to merge.

@COM8 COM8 added this to the CPR 1.11.0 milestone Jun 8, 2024
@COM8 COM8 merged commit 29079a9 into libcpr:master Jun 8, 2024
49 of 53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants