From ba48a6f43adcf795b337aaf001d61beb0bdba505 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Sun, 10 Apr 2016 14:49:11 +0200 Subject: [PATCH] Use BatchBuffer instead of FixedRingBuffer for TCP read. --- source/vibe/core/net.d | 6 ++---- source/vibe/internal/array.d | 11 ++++++++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/source/vibe/core/net.d b/source/vibe/core/net.d index fc7a038..041b05b 100644 --- a/source/vibe/core/net.d +++ b/source/vibe/core/net.d @@ -287,11 +287,11 @@ struct TCPConnection { @safe: import core.time : seconds; - import vibe.internal.array : FixedRingBuffer; + import vibe.internal.array : BatchBuffer; //static assert(isConnectionStream!TCPConnection); struct Context { - FixedRingBuffer!ubyte readBuffer; + BatchBuffer!ubyte readBuffer; } private { @@ -381,7 +381,6 @@ mixin(tracer); waitForData(); auto n = min(count, m_context.readBuffer.length); m_context.readBuffer.popFrontN(n); - if (m_context.readBuffer.empty) m_context.readBuffer.clear(); // start filling at index 0 again count -= n; } } @@ -395,7 +394,6 @@ mixin(tracer); assert(m_context.readBuffer.length > 0); auto l = min(dst.length, m_context.readBuffer.length); m_context.readBuffer.read(dst[0 .. l]); - if (m_context.readBuffer.empty) m_context.readBuffer.clear(); // start filling at index 0 again dst = dst[l .. $]; } } diff --git a/source/vibe/internal/array.d b/source/vibe/internal/array.d index 1e1ad72..6e2024c 100644 --- a/source/vibe/internal/array.d +++ b/source/vibe/internal/array.d @@ -563,16 +563,21 @@ struct BatchBuffer(T, size_t N = 0) { @property void capacity(size_t n) { assert(n >= m_fill); m_buffer.length = n; } } - @property bool empty() { return m_first >= m_fill; } + @property bool empty() const { return m_first >= m_fill; } @property size_t capacity() const { return m_buffer.length; } - @property size_t length() { return m_fill - m_first; } + @property size_t length() const { return m_fill - m_first; } @property ref inout(T) front() inout { assert(!empty); return m_buffer[m_first]; } - void popFront() { assert(!m_empty); m_first++; } + void popFront() { assert(!empty); m_first++; } void popFrontN(size_t n) { assert(n <= length); m_first += n; } inout(T)[] peek() inout { return m_buffer[m_first .. m_fill]; } T[] peekDst() { assert(empty); return m_buffer; } void putN(size_t n) { assert(empty && n <= m_buffer.length); m_fill = n; } void putN(T[] elems) { assert(empty && elems.length <= m_buffer.length); m_buffer[0 .. elems.length] = elems[]; m_fill = elems.length; } + void read(T[] dst) { + assert(length() >= dst.length); + dst[] = m_buffer[0 .. m_fill]; + m_fill = 0; + } }