Skip to content

Commit

Permalink
replace SmallString with that of userver
Browse files Browse the repository at this point in the history
  • Loading branch information
itrofimow committed Jan 1, 2024
1 parent 4985807 commit e56c491
Showing 1 changed file with 16 additions and 41 deletions.
57 changes: 16 additions & 41 deletions frameworks/C++/userver/userver_benchmark/bare/simple_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,19 @@
#include <userver/engine/async.hpp>
#include <userver/utils/datetime/wall_coarse_clock.hpp>
#include <userver/utils/scope_guard.hpp>
#include <userver/utils/small_string.hpp>

namespace userver_techempower::bare {

namespace {

template <std::size_t N>
class SmallString final {
public:
SmallString() = default;

void Append(const char* data, std::size_t length) {
const auto old_size = Size();
data_.resize(old_size + length);
std::memcpy(Data() + old_size, data, length);
}

void Append(std::string_view sw) { Append(sw.data(), sw.size()); }

[[nodiscard]] std::string_view AsSw() const { return {Data(), Size()}; }

[[nodiscard]] char* Data() { return data_.data(); }
[[nodiscard]] const char* Data() const { return data_.data(); }

[[nodiscard]] std::size_t Size() const { return data_.size(); }

void Clear() { data_.resize(0); }

private:
boost::container::small_vector<char, N> data_;
};

struct HttpParser final {
http_parser parser{};
http_parser_settings parser_settings{};

std::function<void(std::string_view)> on_request_cb{};

SmallString<50> url;
userver::utils::SmallString<50> url;

explicit HttpParser(std::function<void(std::string_view)> on_request_cb)
: on_request_cb{std::move(on_request_cb)} {
Expand All @@ -68,26 +43,26 @@ struct HttpParser final {
static int HttpOnUrl(http_parser* parser, const char* data,
std::size_t length) {
auto* self = static_cast<HttpParser*>(parser->data);
self->url.Append(data, length);
self->url.append(std::string_view{data, length});
return 0;
}

static int HttpOnMessageBegin(http_parser* parser) {
auto* self = static_cast<HttpParser*>(parser->data);
self->url.Clear();
self->url.clear();
return 0;
}

static int HttpOnMessageComplete(http_parser* parser) {
auto* self = static_cast<HttpParser*>(parser->data);
self->on_request_cb(self->url.AsSw());
self->on_request_cb(static_cast<std::string_view>(self->url));
return 0;
}
};

class ResponseBuffers final {
public:
using HeadersString = SmallString<200>;
using HeadersString = userver::utils::SmallString<200>;

HeadersString& Next(userver::engine::io::Socket& socket, std::string&& body) {
if (Size() == kMaxResponses) {
Expand All @@ -111,9 +86,9 @@ class ResponseBuffers final {
std::size_t index = 0;
std::size_t total_size = 0;
for (const auto& response : responses_) {
iovec[index++] = {response.headers.Data(), response.headers.Size()};
iovec[index++] = {response.headers.data(), response.headers.size()};
iovec[index++] = {response.body.data(), response.body.size()};
total_size += response.headers.Size() + response.body.size();
total_size += response.headers.size() + response.body.size();
}

if (socket.SendAll(iovec.data(), iovec.size(), {}) != total_size) {
Expand Down Expand Up @@ -191,17 +166,17 @@ void SimpleConnection::Process() {
const auto content_length_str = std::to_string(response.body.size());
auto& headers = buffers.Next(socket_, std::move(response.body));

headers.Append(kCommonHeaders);
headers.Append("Content-Type: ");
headers.Append(response.content_type);
headers.append(kCommonHeaders);
headers.append("Content-Type: ");
headers.append(response.content_type);

headers.Append("\r\nContent-Length: ");
headers.Append(content_length_str);
headers.append("\r\nContent-Length: ");
headers.append(content_length_str);

headers.Append("\r\nDate: ");
headers.Append(GetCachedDate());
headers.append("\r\nDate: ");
headers.append(GetCachedDate());

headers.Append(kHeadersEnd);
headers.append(kHeadersEnd);
};
HttpParser parser{handle_request};

Expand Down

0 comments on commit e56c491

Please sign in to comment.