Performance optimizations

This commit is contained in:
Christian Kratky
2017-04-09 15:10:35 +02:00
parent 8bfad437cf
commit 9816f12df6
16 changed files with 172 additions and 136 deletions
+13 -7
View File
@@ -4,36 +4,42 @@ namespace MQTTnet.Core.Serializer
{
public class ByteReader
{
private readonly int _source;
private int _index;
private readonly int _byte;
public ByteReader(byte @byte)
public ByteReader(int source)
{
_byte = @byte;
_source = source;
}
public bool Read()
{
if (_index >= 8)
{
throw new InvalidOperationException("End of the byte reached.");
throw new InvalidOperationException("End of byte reached.");
}
var result = ((1 << _index) & _byte) > 0;
var result = ((1 << _index) & _source) > 0;
_index++;
return result;
}
public byte Read(int count)
{
if (_index + count > 8)
{
throw new InvalidOperationException("End of byte will be reached.");
}
var result = 0;
for (var i = 0; i < count; i++)
{
if (Read())
if (((1 << _index) & _source) > 0)
{
result |= 1 << i;
}
_index++;
}
return (byte)result;