Add isolation_threshold, use field to store block size rather than template parameter

This commit is contained in:
Tatsuhiro Tsujikawa 2016-03-09 21:25:11 +09:00
parent bae37e3e4a
commit fa601e5ba3
3 changed files with 24 additions and 17 deletions

View File

@ -36,8 +36,12 @@ struct MemBlock {
uint8_t *begin, *last, *end; uint8_t *begin, *last, *end;
}; };
template <size_t BLOCK_SIZE> struct BlockAllocator { struct BlockAllocator {
BlockAllocator() : retain(nullptr), head(nullptr) {} BlockAllocator(size_t block_size, size_t isolation_threshold)
: retain(nullptr),
head(nullptr),
block_size(block_size),
isolation_threshold(std::min(block_size, isolation_threshold)) {}
~BlockAllocator() { ~BlockAllocator() {
for (auto mb = retain; mb;) { for (auto mb = retain; mb;) {
@ -59,14 +63,14 @@ template <size_t BLOCK_SIZE> struct BlockAllocator {
} }
void *alloc(size_t size) { void *alloc(size_t size) {
if (size >= BLOCK_SIZE) { if (size >= isolation_threshold) {
auto mb = alloc_mem_block(size); auto mb = alloc_mem_block(size);
mb->last = mb->end; mb->last = mb->end;
return mb->begin; return mb->begin;
} }
if (!head || head->end - head->last < static_cast<ssize_t>(size)) { if (!head || head->end - head->last < static_cast<ssize_t>(size)) {
head = alloc_mem_block(BLOCK_SIZE); head = alloc_mem_block(block_size);
} }
auto res = head->last; auto res = head->last;
@ -79,6 +83,11 @@ template <size_t BLOCK_SIZE> struct BlockAllocator {
MemBlock *retain; MemBlock *retain;
MemBlock *head; MemBlock *head;
// size of single memory block
size_t block_size;
// if allocation greater or equal to isolation_threshold bytes is
// requested, allocate dedicated block.
size_t isolation_threshold;
}; };
template <typename BlockAllocator> template <typename BlockAllocator>
@ -101,8 +110,6 @@ StringRef concat_string_ref(BlockAllocator &alloc, const StringRef &a,
return StringRef{dst, a.size() + b.size()}; return StringRef{dst, a.size() + b.size()};
} }
using DefaultBlockAllocator = BlockAllocator<1024>;
} // namespace aria2 } // namespace aria2
#endif // ALLOCATOR_H #endif // ALLOCATOR_H

View File

@ -116,6 +116,7 @@ Downstream::Downstream(Upstream *upstream, MemchunkPool *mcpool,
: dlnext(nullptr), : dlnext(nullptr),
dlprev(nullptr), dlprev(nullptr),
response_sent_body_length(0), response_sent_body_length(0),
balloc_(1024, 1024),
req_(balloc_), req_(balloc_),
resp_(balloc_), resp_(balloc_),
request_start_time_(std::chrono::high_resolution_clock::now()), request_start_time_(std::chrono::high_resolution_clock::now()),
@ -357,9 +358,8 @@ void add_header(size_t &sum, HeaderRefs &headers, const StringRef &name,
} // namespace } // namespace
namespace { namespace {
void append_last_header_key(DefaultBlockAllocator &balloc, bool &key_prev, void append_last_header_key(BlockAllocator &balloc, bool &key_prev, size_t &sum,
size_t &sum, HeaderRefs &headers, const char *data, HeaderRefs &headers, const char *data, size_t len) {
size_t len) {
assert(key_prev); assert(key_prev);
sum += len; sum += len;
auto &item = headers.back(); auto &item = headers.back();
@ -371,7 +371,7 @@ void append_last_header_key(DefaultBlockAllocator &balloc, bool &key_prev,
} // namespace } // namespace
namespace { namespace {
void append_last_header_value(DefaultBlockAllocator &balloc, bool &key_prev, void append_last_header_value(BlockAllocator &balloc, bool &key_prev,
size_t &sum, HeaderRefs &headers, size_t &sum, HeaderRefs &headers,
const char *data, size_t len) { const char *data, size_t len) {
key_prev = false; key_prev = false;
@ -926,6 +926,6 @@ void Downstream::set_assoc_stream_id(int32_t stream_id) {
int32_t Downstream::get_assoc_stream_id() const { return assoc_stream_id_; } int32_t Downstream::get_assoc_stream_id() const { return assoc_stream_id_; }
DefaultBlockAllocator &Downstream::get_block_allocator() { return balloc_; } BlockAllocator &Downstream::get_block_allocator() { return balloc_; }
} // namespace shrpx } // namespace shrpx

View File

@ -52,7 +52,7 @@ struct BlockedLink;
class FieldStore { class FieldStore {
public: public:
FieldStore(DefaultBlockAllocator &balloc, size_t headers_initial_capacity) FieldStore(BlockAllocator &balloc, size_t headers_initial_capacity)
: content_length(-1), : content_length(-1),
balloc_(balloc), balloc_(balloc),
buffer_size_(0), buffer_size_(0),
@ -112,7 +112,7 @@ public:
int64_t content_length; int64_t content_length;
private: private:
DefaultBlockAllocator &balloc_; BlockAllocator &balloc_;
HeaderRefs headers_; HeaderRefs headers_;
// trailer fields. For HTTP/1.1, trailer fields are only included // trailer fields. For HTTP/1.1, trailer fields are only included
// with chunked encoding. For HTTP/2, there is no such limit. // with chunked encoding. For HTTP/2, there is no such limit.
@ -126,7 +126,7 @@ private:
}; };
struct Request { struct Request {
Request(DefaultBlockAllocator &balloc) Request(BlockAllocator &balloc)
: fs(balloc, 16), : fs(balloc, 16),
recv_body_length(0), recv_body_length(0),
unconsumed_body_length(0), unconsumed_body_length(0),
@ -182,7 +182,7 @@ struct Request {
}; };
struct Response { struct Response {
Response(DefaultBlockAllocator &balloc) Response(BlockAllocator &balloc)
: fs(balloc, 32), : fs(balloc, 32),
recv_body_length(0), recv_body_length(0),
unconsumed_body_length(0), unconsumed_body_length(0),
@ -376,7 +376,7 @@ public:
DefaultMemchunks pop_response_buf(); DefaultMemchunks pop_response_buf();
DefaultBlockAllocator &get_block_allocator(); BlockAllocator &get_block_allocator();
enum { enum {
EVENT_ERROR = 0x1, EVENT_ERROR = 0x1,
@ -397,7 +397,7 @@ public:
int64_t response_sent_body_length; int64_t response_sent_body_length;
private: private:
DefaultBlockAllocator balloc_; BlockAllocator balloc_;
Request req_; Request req_;
Response resp_; Response resp_;