From 7684f00ebeaad94fa774a0a12d3d44dbcf030687 Mon Sep 17 00:00:00 2001 From: Avi Kivity Date: Tue, 14 Oct 2014 18:05:01 +0300 Subject: [PATCH] virtio: reduce rx allocations when preparing buffers (part 1) Instead of returning a vector of buffer chains, return an iterator range that will produce those vectors on the fly, removing the vector allocation. --- net/virtio.cc | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/net/virtio.cc b/net/virtio.cc index 3783ea5870..04b97161c0 100644 --- a/net/virtio.cc +++ b/net/virtio.cc @@ -11,6 +11,7 @@ #include "core/stream.hh" #include "core/circular_buffer.hh" #include "core/align.hh" +#include "util/function_input_iterator.hh" #include #include #include @@ -488,9 +489,7 @@ virtio_net_device::rxq::prepare_buffers() { if (available.try_wait(opportunistic)) { count += opportunistic; } - std::vector vbc; - vbc.reserve(count); - for (unsigned i = 0; i < count; ++i) { + auto make_buffer_chain = [this] { vring::buffer_chain bc; std::unique_ptr buf(new char[4096]); vring::buffer b; @@ -526,9 +525,11 @@ virtio_net_device::rxq::prepare_buffers() { } }); bc.push_back(std::move(b)); - vbc.push_back(std::move(bc)); - } - _ring.post(vbc.begin(), vbc.end()); + return bc; + }; + auto start = make_function_input_iterator(make_buffer_chain, 0U); + auto finish = make_function_input_iterator(make_buffer_chain, count); + _ring.post(start, finish); }); }