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;
};
template <size_t BLOCK_SIZE> struct BlockAllocator {
BlockAllocator() : retain(nullptr), head(nullptr) {}
struct BlockAllocator {
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() {
for (auto mb = retain; mb;) {
@ -59,14 +63,14 @@ template <size_t BLOCK_SIZE> struct BlockAllocator {
}
void *alloc(size_t size) {
if (size >= BLOCK_SIZE) {
if (size >= isolation_threshold) {
auto mb = alloc_mem_block(size);
mb->last = mb->end;
return mb->begin;
}
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;
@ -79,6 +83,11 @@ template <size_t BLOCK_SIZE> struct BlockAllocator {
MemBlock *retain;
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>
@ -101,8 +110,6 @@ StringRef concat_string_ref(BlockAllocator &alloc, const StringRef &a,
return StringRef{dst, a.size() + b.size()};
}
using DefaultBlockAllocator = BlockAllocator<1024>;
} // namespace aria2
#endif // ALLOCATOR_H

View File

@ -116,6 +116,7 @@ Downstream::Downstream(Upstream *upstream, MemchunkPool *mcpool,
: dlnext(nullptr),
dlprev(nullptr),
response_sent_body_length(0),
balloc_(1024, 1024),
req_(balloc_),
resp_(balloc_),
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 {
void append_last_header_key(DefaultBlockAllocator &balloc, bool &key_prev,
size_t &sum, HeaderRefs &headers, const char *data,
size_t len) {
void append_last_header_key(BlockAllocator &balloc, bool &key_prev, size_t &sum,
HeaderRefs &headers, const char *data, size_t len) {
assert(key_prev);
sum += len;
auto &item = headers.back();
@ -371,7 +371,7 @@ void append_last_header_key(DefaultBlockAllocator &balloc, bool &key_prev,
} // 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,
const char *data, size_t len) {
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_; }
DefaultBlockAllocator &Downstream::get_block_allocator() { return balloc_; }
BlockAllocator &Downstream::get_block_allocator() { return balloc_; }
} // namespace shrpx

View File

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