summaryrefslogtreecommitdiff
path: root/Impostor-dev/src/Impostor.Benchmarks/Data/MessageReader_Bytes_Pooled_Improved.cs
blob: 006684797b1284c1328549e640e0c3651c2d4a7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.Buffers.Binary;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using Microsoft.Extensions.ObjectPool;

namespace Impostor.Benchmarks.Data
{
    public class MessageReader_Bytes_Pooled_Improved : IDisposable
    {
        private readonly ObjectPool<MessageReader_Bytes_Pooled_Improved> _pool;

        public MessageReader_Bytes_Pooled_Improved(ObjectPool<MessageReader_Bytes_Pooled_Improved> pool)
        {
            _pool = pool;
        }

        public byte Tag { get; set; }
        public byte[] Buffer { get; set; }
        public int Position { get; set; }
        public int Length { get; set; }
        public int BytesRemaining => this.Length - this.Position;

        public void Update(byte[] buffer, int position = 0, int length = 0)
        {
            Tag = byte.MaxValue;
            Buffer = buffer;
            Position = position;
            Length = length;
        }

        public void Update(byte tag, byte[] buffer, int position = 0, int length = 0)
        {
            Tag = tag;
            Buffer = buffer;
            Position = position;
            Length = length;
        }

        public MessageReader_Bytes_Pooled_Improved ReadMessage()
        {
            var length = ReadUInt16();
            var tag = ReadByte();
            var pos = Position;

            Position += length;

            var result = _pool.Get();
            result.Update(tag, Buffer, pos, length);
            return result;
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public byte ReadByte()
        {
            return Buffer[Position++];
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public ushort ReadUInt16()
        {
            var res = BinaryPrimitives.ReadUInt16LittleEndian(Buffer.AsSpan(Position));
            Position += sizeof(ushort);
            return res;
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public int ReadInt32()
        {
            var res = BinaryPrimitives.ReadInt32LittleEndian(Buffer.AsSpan(Position));
            Position += sizeof(int);
            return res;
        }

        public MessageReader_Bytes_Pooled_Improved Slice(int start, int length)
        {
            var result = _pool.Get();
            result.Update(Tag, Buffer, start, length);
            return result;
        }

        public void Dispose()
        {
            _pool.Return(this);
        }
    }
}