Use BatchBuffer instead of FixedRingBuffer for TCP read.

This commit is contained in:
Sönke Ludwig 2016-04-10 14:49:11 +02:00
parent 77dc93baa7
commit ba48a6f43a
2 changed files with 10 additions and 7 deletions

View file

@ -287,11 +287,11 @@ struct TCPConnection {
@safe: @safe:
import core.time : seconds; import core.time : seconds;
import vibe.internal.array : FixedRingBuffer; import vibe.internal.array : BatchBuffer;
//static assert(isConnectionStream!TCPConnection); //static assert(isConnectionStream!TCPConnection);
struct Context { struct Context {
FixedRingBuffer!ubyte readBuffer; BatchBuffer!ubyte readBuffer;
} }
private { private {
@ -381,7 +381,6 @@ mixin(tracer);
waitForData(); waitForData();
auto n = min(count, m_context.readBuffer.length); auto n = min(count, m_context.readBuffer.length);
m_context.readBuffer.popFrontN(n); m_context.readBuffer.popFrontN(n);
if (m_context.readBuffer.empty) m_context.readBuffer.clear(); // start filling at index 0 again
count -= n; count -= n;
} }
} }
@ -395,7 +394,6 @@ mixin(tracer);
assert(m_context.readBuffer.length > 0); assert(m_context.readBuffer.length > 0);
auto l = min(dst.length, m_context.readBuffer.length); auto l = min(dst.length, m_context.readBuffer.length);
m_context.readBuffer.read(dst[0 .. l]); 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 .. $]; dst = dst[l .. $];
} }
} }

View file

@ -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 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 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]; } @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; } void popFrontN(size_t n) { assert(n <= length); m_first += n; }
inout(T)[] peek() inout { return m_buffer[m_first .. m_fill]; } inout(T)[] peek() inout { return m_buffer[m_first .. m_fill]; }
T[] peekDst() { assert(empty); return m_buffer; } T[] peekDst() { assert(empty); return m_buffer; }
void putN(size_t n) { assert(empty && n <= m_buffer.length); m_fill = n; } 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 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;
}
} }