Skip to content

Commit b35722d

Browse files
committed
posix_data_source_impl: adaptive prefetch size
Signed-off-by: Yingxin <[email protected]>
1 parent 7ce6e56 commit b35722d

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

include/seastar/net/posix-stack.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class posix_data_source_impl final : public data_source_impl {
109109
size_t _buf_size;
110110
public:
111111
explicit posix_data_source_impl(lw_shared_ptr<pollable_fd> fd, compat::polymorphic_allocator<char>* allocator=memory::malloc_allocator,
112-
size_t buf_size = 8192) : _buffer_allocator(allocator), _fd(std::move(fd)),
112+
size_t buf_size = 1 << 13) : _buffer_allocator(allocator), _fd(std::move(fd)),
113113
_buf(make_temporary_buffer<char>(_buffer_allocator, buf_size)), _buf_size(buf_size) {}
114114
future<temporary_buffer<char>> get() override;
115115
future<size_t, temporary_buffer<char>> get_direct(char* buf, size_t size) override;

src/net/posix-stack.cc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,17 @@ posix_ap_server_socket_impl<Transport>::move_connected_socket(socket_address sa,
315315
}
316316
}
317317

318+
static constexpr size_t min_buf_size = 1 << 9;
319+
static constexpr size_t max_buf_size = 1 << 15;
320+
318321
future<temporary_buffer<char>>
319322
posix_data_source_impl::get() {
320323
return _fd->read_some(_buf.get_write(), _buf_size).then([this] (size_t size) {
324+
if (_buf_size == size) {
325+
_buf_size = std::min(max_buf_size, _buf_size << 2);
326+
} else if (size < (_buf_size >> 2)) {
327+
_buf_size = std::max(min_buf_size, _buf_size >> 2);
328+
}
321329
_buf.trim(size);
322330
auto ret = std::move(_buf);
323331
_buf = make_temporary_buffer<char>(_buffer_allocator, _buf_size);
@@ -329,7 +337,12 @@ future<size_t, temporary_buffer<char>>
329337
posix_data_source_impl::get_direct(char* buf, size_t size) {
330338
if (size > _buf_size / 2) {
331339
// this was a large read, we don't prefetch
332-
return _fd->read_some(buf, size).then([] (auto read_size) {
340+
return _fd->read_some(buf, size).then([this] (auto read_size) {
341+
if (_buf_size == read_size) {
342+
_buf_size = std::min(max_buf_size, _buf_size << 2);
343+
} else if (read_size < (_buf_size >> 2)) {
344+
_buf_size = std::max(min_buf_size, _buf_size >> 2);
345+
}
333346
return make_ready_future<size_t, temporary_buffer<char>>(
334347
read_size, temporary_buffer<char>());
335348
});

0 commit comments

Comments
 (0)