Skip to content

Commit e7c6441

Browse files
committed
performance improvements
1 parent 287ff8f commit e7c6441

8 files changed

Lines changed: 439 additions & 149 deletions

File tree

server/include/http_parser.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ class HttpParserState {
3232

3333
State state_{State::StartLine};
3434
RequestData current_;
35-
std::optional<RequestData> complete_;
3635
std::string line_;
3736
std::string pending_;
3837
bool sawCr_{false};
@@ -43,22 +42,34 @@ class HttpParserState {
4342
bool started_{false};
4443
bool chunked_{false};
4544
bool expectsContinue_{false};
45+
bool hostEmpty_{false};
46+
bool contentLengthInvalid_{false};
47+
bool contentLengthConflict_{false};
48+
bool unsupportedTransferEncoding_{false};
49+
bool invalidExpect_{false};
50+
bool hasContentLength_{false};
51+
size_t hostCount_{0};
4652
size_t headerBytes_{0};
4753
size_t fixedRemaining_{0};
4854
size_t chunkRemaining_{0};
55+
size_t contentLength_{0};
4956
int errorStatus_{0};
5057
std::string errorMessage_;
5158

5259
void ResetForNext();
53-
void ProcessBytes(std::string_view data);
60+
size_t ProcessBytes(std::string_view data);
5461
void SetError(int status, std::string message);
5562
void CompleteCurrent();
5663
void ProcessLine();
64+
void AppendLineData(std::string_view data);
5765
void ProcessLineByte(char ch);
5866
void FinishHeaders();
67+
void TrackHeader(std::string_view name, std::string_view value);
5968

6069
public:
70+
HttpParserState();
6171
void Append(std::string_view data);
72+
size_t Consume(std::string_view data);
6273
bool Empty() const;
6374
void MarkContinueSent();
6475
HttpParseResult ParseNext(RequestData &request);

server/include/io_uring.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "co_future.h"
33
#include <array>
44
#include <atomic>
5+
#include <cstddef>
56
#include <deque>
67
#include <functional>
78
#include <liburing.h>
@@ -12,6 +13,8 @@
1213
#include <string_view>
1314
#define QUEUE_DEPTH 1024
1415
namespace HTTP {
16+
inline constexpr size_t kReadBufferSize = 256;
17+
1518
class IOUring;
1619

1720
class IOUring {
@@ -44,8 +47,8 @@ class IOUring {
4447
~IOUring();
4548
IOUring();
4649
IOUring &operator=(IOUring &&rhs);
47-
void Read(int fileDescriptor, std::array<char, 256> &buffer, std::function<void(int)> complete);
48-
CoFuture<int> ReadAsync(int fileDescriptor, std::array<char, 256> &buffer);
50+
void Read(int fileDescriptor, std::array<char, kReadBufferSize> &buffer, std::function<void(int)> complete);
51+
CoFuture<int> ReadAsync(int fileDescriptor, std::array<char, kReadBufferSize> &buffer);
4952
void Write(int fileDescriptor, std::string_view data, size_t offset, size_t len,
5053
std::function<void(int)> complete);
5154
CoFuture<int> WriteAsync(int fileDescriptor, std::string_view data, size_t offset,

server/include/read_iterator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace HTTP {
55

66
class ReadIterator {
77
IOUring &ring_;
8-
std::array<char, 256> buffer_;
8+
std::array<char, kReadBufferSize> buffer_;
99
size_t length_{0};
1010
size_t position_{0};
1111
int fd_;

server/include/request_data.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ struct RequestData {
2424
std::string query;
2525
std::string version;
2626
std::string body;
27+
bool connectionClose{false};
28+
bool connectionKeepAlive{false};
2729

2830
std::optional<std::string> Header(std::string_view name) const;
2931
};

server/include/server.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "trie.h"
77
#include <atomic>
88
#include <memory>
9+
#include <string>
910
#include <string_view>
1011
#include <vector>
1112
namespace HTTP {
@@ -24,7 +25,8 @@ class Server {
2425
CoFuture<void> WriteRaw(IOUring &ring, int connectionFD, std::string_view data);
2526
CoFuture<void> WriteResponse(IOUring &ring, int connectionFD,
2627
const ResponseData &data,
27-
const RequestData &request, bool keepAlive);
28+
const RequestData &request, bool keepAlive,
29+
std::string &buffer);
2830
CoFuture<void> Process(IOUring &ring, int connectionFD);
2931
friend class ServerBuilder;
3032

0 commit comments

Comments
 (0)