Skip to content

Commit 3496b18

Browse files
committed
Initial commit
0 parents  commit 3496b18

15 files changed

+1258
-0
lines changed

.gitignore

Lines changed: 484 additions & 0 deletions
Large diffs are not rendered by default.

Directory.Build.props

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<Project>
2+
<PropertyGroup>
3+
<UseArtifactsOutput>true</UseArtifactsOutput>
4+
</PropertyGroup>
5+
</Project>

bench/Program.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// See https://aka.ms/new-console-template for more information
2+
using System;
3+
using System.Security.Cryptography;
4+
using BenchmarkDotNet.Attributes;
5+
using BenchmarkDotNet.Running;
6+
7+
namespace MsgPack.Benchmarks;
8+
9+
public class MessagePackUnit
10+
{
11+
private const int N = 10000;
12+
private readonly byte[] data;
13+
14+
private readonly SHA256 sha256 = SHA256.Create();
15+
private readonly MD5 md5 = MD5.Create();
16+
17+
public MessagePackUnit()
18+
{
19+
data = new byte[N];
20+
new Random(42).NextBytes(data);
21+
}
22+
23+
[Benchmark]
24+
public byte[] Sha256() => sha256.ComputeHash(data);
25+
26+
[Benchmark]
27+
public byte[] Md5() => md5.ComputeHash(data);
28+
}
29+
30+
public class Program
31+
{
32+
public static void Main(string[] args)
33+
{
34+
var summary = BenchmarkRunner.Run<MessagePackUnit>();
35+
}
36+
}
37+

bench/bench.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
12+
</ItemGroup>
13+
14+
</Project>

bench/dotnet

Whitespace-only changes.

serde.msgpack.sln

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31903.59
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serde.MsgPack.Tests", "tests\Serde.MsgPack.Tests.csproj", "{FC991F6B-67EA-4C86-9BDE-70914D06BA5A}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serde.MsgPack", "src\Serde.MsgPack.csproj", "{C765AC6A-ACF0-415E-BA83-E05D8CFDF5D9}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(SolutionProperties) = preSolution
16+
HideSolutionNode = FALSE
17+
EndGlobalSection
18+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
19+
{FC991F6B-67EA-4C86-9BDE-70914D06BA5A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
20+
{FC991F6B-67EA-4C86-9BDE-70914D06BA5A}.Debug|Any CPU.Build.0 = Debug|Any CPU
21+
{FC991F6B-67EA-4C86-9BDE-70914D06BA5A}.Release|Any CPU.ActiveCfg = Release|Any CPU
22+
{FC991F6B-67EA-4C86-9BDE-70914D06BA5A}.Release|Any CPU.Build.0 = Release|Any CPU
23+
{C765AC6A-ACF0-415E-BA83-E05D8CFDF5D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24+
{C765AC6A-ACF0-415E-BA83-E05D8CFDF5D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
25+
{C765AC6A-ACF0-415E-BA83-E05D8CFDF5D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
26+
{C765AC6A-ACF0-415E-BA83-E05D8CFDF5D9}.Release|Any CPU.Build.0 = Release|Any CPU
27+
EndGlobalSection
28+
EndGlobal

src/IByteReader.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
using System;
3+
using System.Buffers;
4+
using System.Runtime.CompilerServices;
5+
6+
namespace Serde.IO;
7+
8+
internal interface IByteReader
9+
{
10+
public const short EndOfStream = -1;
11+
12+
/// <summary>
13+
/// Reads a byte without advancing the stream, or returns <see cref="EndOfStream"/> if the end
14+
/// of the stream has been reached.
15+
/// </summary>
16+
short Peek();
17+
18+
/// <summary>
19+
/// Reads a byte and advances the stream, or returns <see cref="EndOfStream"/> if the end of the
20+
/// stream has been reached.
21+
/// </summary>
22+
short Next();
23+
24+
/// <summary>
25+
/// Advances the stream by <paramref name="count"/> bytes. The caller should ensure there is
26+
/// enough remaining data in the stream.
27+
/// </summary>
28+
void Advance(int count = 1);
29+
}

src/MsgPackSerializer.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Serde;
2+
3+
namespace Serde.MsgPack;
4+
5+
public static class MsgPackSerializer
6+
{
7+
public static byte[] Serialize<T>(T value)
8+
where T : ISerialize<T>
9+
{
10+
using var buffer = new ScratchBuffer();
11+
var writer = new MsgPackWriter(buffer);
12+
value.Serialize(value, writer);
13+
return buffer.Span.ToArray();
14+
}
15+
16+
public static byte[] Serialize<T, U>(T value, U proxy)
17+
where U : ISerialize<T>
18+
{
19+
using var buffer = new ScratchBuffer();
20+
var writer = new MsgPackWriter(buffer);
21+
proxy.Serialize(value, writer);
22+
return buffer.Span.ToArray();
23+
}
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
namespace Serde.MsgPack;
3+
4+
partial class MsgPackWriter : ISerializeCollection
5+
{
6+
void ISerializeCollection.End(ISerdeInfo typeInfo)
7+
{
8+
// No action needed, all collections are length-prefixed
9+
}
10+
11+
void ISerializeCollection.SerializeElement<T, U>(T value, U serialize)
12+
{
13+
serialize.Serialize(value, this);
14+
}
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
using Serde;
3+
4+
namespace Serde.MsgPack;
5+
6+
partial class MsgPackWriter : ISerializeType
7+
{
8+
void ISerializeType.End()
9+
{
10+
// No action needed, all collections are length-prefixed
11+
}
12+
13+
void ISerializeType.SerializeField<T, U>(ISerdeInfo typeInfo, int index, T value, U serialize)
14+
{
15+
serialize.Serialize(value, this);
16+
}
17+
}

0 commit comments

Comments
 (0)