forked from UnigramDev/Unigram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParseBenchmarks.cs
More file actions
192 lines (167 loc) · 5.8 KB
/
Copy pathParseBenchmarks.cs
File metadata and controls
192 lines (167 loc) · 5.8 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//
// Copyright (c) Fela Ameghino 2015-2026
//
// Distributed under the GNU General Public License v3.0. (See accompanying
// file LICENSE or copy at https://www.gnu.org/licenses/gpl-3.0.txt)
//
using BenchmarkDotNet.Attributes;
using System;
using System.Collections.Generic;
using System.Text.Json;
using Telegram.Td;
using Telegram.Td.Api;
namespace Telegram.Benchmarks
{
/// <summary>
/// End-to-end: what Client.Receive pays per payload, minus the interop.
/// </summary>
[MemoryDiagnoser]
public class ParseBenchmarks
{
public IEnumerable<Payload> Payloads => Corpus.Load();
[ParamsSource(nameof(Payloads))]
public Payload Payload { get; set; } = null!;
[Benchmark]
public Telegram.Td.Api.Object FromJson()
{
return ClientJson.FromJson(Payload.Bytes.AsSpan(0, Payload.Length), BenchmarkResultHandler.Instance);
}
}
/// <summary>
/// Field dispatch, head to head, on the same bytes into the same objects:
/// CRC32 of the property name into switch(hash), against switch(length) plus an exact compare.
/// </summary>
[MemoryDiagnoser]
public class DispatchBenchmarks
{
private byte[] _message = null!;
private byte[] _localFile = null!;
[GlobalSetup]
public void Setup()
{
_message = Fixtures.Load("message.json");
_localFile = Fixtures.Load("localFile.json");
}
// Both parsers expect the reader parked on the first property after "@type", which is
// where DoFromJson leaves it.
private static Utf8JsonReader Open(byte[] bytes)
{
var reader = new Utf8JsonReader(bytes);
reader.Read(); // {
reader.Read(); // "@type"
reader.Read(); // "message"
reader.Read(); // first property name
return reader;
}
[Benchmark(Baseline = true), BenchmarkCategory("message")]
public Message Message_Crc32()
{
var reader = Open(_message);
return ClientJson.FromJson_Message_Current(ref reader, BenchmarkResultHandler.Instance);
}
[Benchmark, BenchmarkCategory("message")]
public Message Message_LengthAndCompare()
{
var reader = Open(_message);
return ClientJson.FromJson_Message_Alt(ref reader, BenchmarkResultHandler.Instance);
}
[Benchmark, BenchmarkCategory("localFile")]
public LocalFile LocalFile_Crc32()
{
var reader = Open(_localFile);
return ClientJson.FromJson_LocalFile_Current(ref reader, BenchmarkResultHandler.Instance);
}
[Benchmark, BenchmarkCategory("localFile")]
public LocalFile LocalFile_LengthAndCompare()
{
var reader = Open(_localFile);
return ClientJson.FromJson_LocalFile_Alt(ref reader, BenchmarkResultHandler.Instance);
}
}
/// <summary>
/// Client.Receive finds the payload length with a byte-at-a-time scan over the whole payload.
/// td_receive already has the length; the patched ABI could just return it.
/// </summary>
public class LengthBenchmarks
{
private byte[] _bytes = null!;
[Params(482, 1401, 68200)]
public int Size { get; set; }
[GlobalSetup]
public void Setup()
{
_bytes = new byte[Size + 1];
_bytes.AsSpan(0, Size).Fill((byte)'x');
}
[Benchmark(Baseline = true)]
public unsafe int ScanForNul()
{
fixed (byte* ptr = _bytes)
{
byte* end = ptr;
while (*end != 0)
{
end++;
}
return (int)(end - ptr);
}
}
[Benchmark]
public int IndexOfNul()
{
return _bytes.AsSpan().IndexOf((byte)0);
}
[Benchmark]
public int LengthFromAbi()
{
return Size;
}
}
/// <summary>
/// Vectors land in a List<T> that doubles its way up from 4. A pooled scratch buffer plus
/// one exact-sized array is the same parse with one allocation instead of log2(n) of them.
/// </summary>
[MemoryDiagnoser]
public class VectorBenchmarks
{
private byte[] _messages = null!;
[GlobalSetup]
public void Setup()
{
foreach (var payload in Corpus.Load())
{
if (payload.Name.EndsWith("messages", StringComparison.Ordinal))
{
_messages = payload.Bytes;
return;
}
}
throw new InvalidOperationException("messages payload not found");
}
private Utf8JsonReader OpenArray()
{
var reader = new Utf8JsonReader(_messages.AsSpan(0, _messages.Length - 1));
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.PropertyName && reader.ValueTextEquals("messages"u8))
{
reader.Read(); // GetObjectArray expects to start on the '[', as ParseObject leaves it
return reader;
}
}
throw new InvalidOperationException("messages property not found");
}
[Benchmark(Baseline = true)]
public int GrowingList()
{
var reader = OpenArray();
return reader.GetObjectArray(BenchmarkResultHandler.Instance, ClientJson.FromJson_Message_Current).Count;
}
[Benchmark]
public int PooledExact()
{
var reader = OpenArray();
return PooledArray.GetObjectArray(ref reader, BenchmarkResultHandler.Instance, ClientJson.FromJson_Message_Current).Length;
}
}
}