зеркало из
1
0
Форкнуть 0

Some more micro-optimization experiments.

This commit is contained in:
Andrew Peters 2018-01-26 10:46:45 -08:00
Родитель ec28bf3f18
Коммит 4bb31a18c2
4 изменённых файлов: 41 добавлений и 24 удалений

Просмотреть файл

@ -12,8 +12,15 @@ namespace Peregrine.Ado
public class PeregrineCommand : DbCommand
{
private readonly PGSession _session;
private readonly PeregrineDataReader _reader;
private readonly ReadBuffer _readBuffer;
public PeregrineCommand(PGSession session) => _session = session;
public PeregrineCommand(PGSession session)
{
_session = session;
_readBuffer = session.ReadBuffer;
_reader = new PeregrineDataReader(session.ReadBuffer);
}
public Task PrepareAsync() => _session.PrepareAsync("p", CommandText);
@ -21,26 +28,23 @@ namespace Peregrine.Ado
CommandBehavior behavior, CancellationToken cancellationToken)
{
await _session.ExecuteAsync("p");
await _readBuffer.ReceiveAsync();
var readBuffer = _session.ReadBuffer;
var message = _readBuffer.ReadMessage();
await readBuffer.ReceiveAsync();
var message = readBuffer.ReadMessage();
switch (message.Type)
switch (message)
{
case MessageType.BindComplete:
return new PeregrineDataReader(readBuffer);
return _reader;
case MessageType.ErrorResponse:
throw new InvalidOperationException(readBuffer.ReadErrorMessage());
throw new InvalidOperationException(_readBuffer.ReadErrorMessage());
default:
throw new NotImplementedException(message.Type.ToString());
throw new NotImplementedException(message.ToString());
}
}
public override int ExecuteNonQuery()
=> throw new NotImplementedException();

Просмотреть файл

@ -23,12 +23,12 @@ namespace Peregrine.Ado
{
var message = _readBuffer.ReadMessage();
switch (message.Type)
switch (message)
{
case MessageType.DataRow:
{
// Column count
_readBuffer.ReadShort();
_readBuffer.SkipShort();
return _trueResult;
}
@ -40,7 +40,7 @@ namespace Peregrine.Ado
throw new InvalidOperationException(_readBuffer.ReadErrorMessage());
default:
throw new NotImplementedException(message.Type.ToString());
throw new NotImplementedException(message.ToString());
}
}

Просмотреть файл

@ -69,7 +69,7 @@ namespace Peregrine
var message = _readBuffer.ReadMessage();
switch (message.Type)
switch (message)
{
case MessageType.ParseComplete:
break;
@ -78,7 +78,7 @@ namespace Peregrine
throw new InvalidOperationException(_readBuffer.ReadErrorMessage());
default:
throw new NotImplementedException(message.Type.ToString());
throw new NotImplementedException(message.ToString());
}
}
@ -160,7 +160,7 @@ namespace Peregrine
var message = _readBuffer.ReadMessage();
switch (message.Type)
switch (message)
{
case MessageType.BindComplete:
goto read;
@ -191,7 +191,7 @@ namespace Peregrine
throw new InvalidOperationException(_readBuffer.ReadErrorMessage());
default:
throw new NotImplementedException(message.Type.ToString());
throw new NotImplementedException(message.ToString());
}
}
@ -219,7 +219,7 @@ namespace Peregrine
var message = _readBuffer.ReadMessage();
switch (message.Type)
switch (message)
{
case MessageType.AuthenticationRequest:
{
@ -261,10 +261,10 @@ namespace Peregrine
case MessageType.EmptyQueryResponse:
case MessageType.ParameterStatus:
case MessageType.ReadyForQuery:
throw new NotImplementedException($"Unhandled MessageType '{message.Type}'");
throw new NotImplementedException($"Unhandled MessageType '{message}'");
default:
throw new InvalidOperationException($"Unexpected MessageType '{message.Type}'");
throw new InvalidOperationException($"Unexpected MessageType '{message}'");
}
}

Просмотреть файл

@ -3,8 +3,6 @@
using System;
using System.Buffers.Binary;
using System.IO;
using System.Threading.Tasks;
namespace Peregrine
{
@ -23,7 +21,17 @@ namespace Peregrine
_awaitableSocket = awaitableSocket;
}
internal (MessageType Type, int Length) ReadMessage()
internal MessageType ReadMessage()
{
var messageType = (MessageType)ReadByte();
// Skip length
_position += sizeof(int);
return messageType;
}
internal (MessageType Type, int Length) ReadMessageWithLength()
{
var messageType = (MessageType)ReadByte();
var length = ReadInt() - 4;
@ -68,6 +76,11 @@ namespace Peregrine
return bs;
}
public void SkipShort()
{
_position += sizeof(short);
}
public short ReadShort()
{
var result = BinaryPrimitives.ReadInt16BigEndian(_buffer.Span.Slice(_position, 2));