Skip to content

Commit

Permalink
Increase internal buffer sizes (#379)
Browse files Browse the repository at this point in the history
* increase internal buffer sizes

Signed-off-by: Colin Sullivan <[email protected]>
  • Loading branch information
ColinSullivan1 authored May 15, 2020
1 parent 6742c53 commit 1f5d474
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 25 deletions.
27 changes: 5 additions & 22 deletions src/NATS.Client/Conn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ internal Connection(Options options)
CRLF_BYTES_LEN = CRLF_BYTES.Length;

// predefine the start of the publish protocol message.
buildPublishProtocolBuffer(Defaults.scratchSize);
buildPublishProtocolBuffer(512);

callbackScheduler.Start();

Expand Down Expand Up @@ -1381,8 +1381,9 @@ private void sendConnect()
StreamReader sr = null;
try
{
// TODO: Make this reader (or future equivalent) unbounded.
// we need the underlying stream, so leave it open.
sr = new StreamReader(br, Encoding.UTF8, false, 512, true);
sr = new StreamReader(br, Encoding.UTF8, false, Defaults.MaxControlLineSize, true);
result = sr.ReadLine();

// If opts.verbose is set, handle +OK.
Expand Down Expand Up @@ -1432,7 +1433,7 @@ private Control readOp()
// the string directly using the buffered reader.
//
// Keep the underlying stream open.
using (StreamReader sr = new StreamReader(br, Encoding.ASCII, false, 1024, true))
using (StreamReader sr = new StreamReader(br, Encoding.ASCII, false, Defaults.MaxControlLineSize, true))
{
return new Control(sr.ReadLine());
}
Expand Down Expand Up @@ -1812,25 +1813,7 @@ internal void deliverMsgs(Channel<Msg> ch)

// Roll our own fast conversion - we know it's the right
// encoding.
char[] convertToStrBuf = new char[Defaults.scratchSize];

// Caller must ensure thread safety.
private string convertToString(byte[] buffer, long length)
{
// expand if necessary
if (length > convertToStrBuf.Length)
{
convertToStrBuf = new char[length];
}

for (int i = 0; i < length; i++)
{
convertToStrBuf[i] = (char)buffer[i];
}

// This is the copy operation for msg arg strings.
return new string(convertToStrBuf, 0, (int)length);
}
char[] convertToStrBuf = new char[Defaults.MaxControlLineSize];

// Since we know we don't need to decode the protocol string,
// just copy the chars into bytes. This increased
Expand Down
2 changes: 1 addition & 1 deletion src/NATS.Client/NATS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public static class Defaults
*/

// Scratch storage for assembling protocol headers
internal const int scratchSize = 512;
internal const int MaxControlLineSize = 4096;

// The size of the bufio writer on top of the socket.
internal const int defaultBufSize = 32768;
Expand Down
2 changes: 0 additions & 2 deletions src/NATS.Client/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ internal Parser(Connection conn)

internal int state = 0;

private const int MAX_CONTROL_LINE_SIZE = 1024;

// For performance declare these as consts - they'll be
// baked into the IL code (thus faster). An enum would
// be nice, but we want speed in this critical section of
Expand Down

0 comments on commit 1f5d474

Please sign in to comment.