From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44477) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aBKjJ-000129-Jg for qemu-devel@nongnu.org; Tue, 22 Dec 2015 06:03:46 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aBKjF-0002NV-FY for qemu-devel@nongnu.org; Tue, 22 Dec 2015 06:03:45 -0500 Received: from mail-wm0-x236.google.com ([2a00:1450:400c:c09::236]:37951) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aBKjF-0002NR-9W for qemu-devel@nongnu.org; Tue, 22 Dec 2015 06:03:41 -0500 Received: by mail-wm0-x236.google.com with SMTP id l126so106608769wml.1 for ; Tue, 22 Dec 2015 03:03:40 -0800 (PST) Sender: Paolo Bonzini From: Paolo Bonzini Date: Tue, 22 Dec 2015 12:03:33 +0100 Message-Id: <1450782213-14227-1-git-send-email-pbonzini@redhat.com> Subject: [Qemu-devel] [PATCH v2] iov: avoid memcpy for "simple" iov_from_buf/iov_to_buf List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: stefanha@redhat.com memcpy can take a large amount of time for small reads and writes. For virtio it is a common case that the first iovec can satisfy the whole read or write. In that case, and if bytes is a constant to avoid excessive growth of code, inline the first iteration into the caller. Signed-off-by: Paolo Bonzini --- v1->v2: check for overflow in offset + bytes, which is covered by test-iov include/qemu/iov.h | 34 ++++++++++++++++++++++++++++++---- util/iov.c | 8 ++++---- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/include/qemu/iov.h b/include/qemu/iov.h index 569b2c2..2847551 100644 --- a/include/qemu/iov.h +++ b/include/qemu/iov.h @@ -39,10 +39,36 @@ size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt); * such "large" value is -1 (sinice size_t is unsigned), * so specifying `-1' as `bytes' means 'up to the end of iovec'. */ -size_t iov_from_buf(const struct iovec *iov, unsigned int iov_cnt, - size_t offset, const void *buf, size_t bytes); -size_t iov_to_buf(const struct iovec *iov, const unsigned int iov_cnt, - size_t offset, void *buf, size_t bytes); +size_t iov_from_buf_full(const struct iovec *iov, unsigned int iov_cnt, + size_t offset, const void *buf, size_t bytes); +size_t iov_to_buf_full(const struct iovec *iov, const unsigned int iov_cnt, + size_t offset, void *buf, size_t bytes); + +static inline size_t +iov_from_buf(const struct iovec *iov, unsigned int iov_cnt, + size_t offset, const void *buf, size_t bytes) +{ + if (__builtin_constant_p(bytes) && iov_cnt && + offset <= iov[0].iov_len && bytes <= iov[0].iov_len - offset) { + memcpy(iov[0].iov_base + offset, buf, bytes); + return bytes; + } else { + return iov_from_buf_full(iov, iov_cnt, offset, buf, bytes); + } +} + +static inline size_t +iov_to_buf(const struct iovec *iov, const unsigned int iov_cnt, + size_t offset, void *buf, size_t bytes) +{ + if (__builtin_constant_p(bytes) && iov_cnt && + offset <= iov[0].iov_len && bytes <= iov[0].iov_len - offset) { + memcpy(buf, iov[0].iov_base + offset, bytes); + return bytes; + } else { + return iov_to_buf_full(iov, iov_cnt, offset, buf, bytes); + } +} /** * Set data bytes pointed out by iovec `iov' of size `iov_cnt' elements, diff --git a/util/iov.c b/util/iov.c index a0d5934..f113467 100644 --- a/util/iov.c +++ b/util/iov.c @@ -19,8 +19,8 @@ #include "qemu/iov.h" #include "qemu/sockets.h" -size_t iov_from_buf(const struct iovec *iov, unsigned int iov_cnt, - size_t offset, const void *buf, size_t bytes) +size_t iov_from_buf_full(const struct iovec *iov, unsigned int iov_cnt, + size_t offset, const void *buf, size_t bytes) { size_t done; unsigned int i; @@ -38,8 +38,8 @@ size_t iov_from_buf(const struct iovec *iov, unsigned int iov_cnt, return done; } -size_t iov_to_buf(const struct iovec *iov, const unsigned int iov_cnt, - size_t offset, void *buf, size_t bytes) +size_t iov_to_buf_full(const struct iovec *iov, const unsigned int iov_cnt, + size_t offset, void *buf, size_t bytes) { size_t done; unsigned int i; -- 2.5.0