Skip to content

Commit 5dfec8e

Browse files
committed
Add README.
1 parent 8504af0 commit 5dfec8e

File tree

9 files changed

+42
-10
lines changed

9 files changed

+42
-10
lines changed

BitSerialization.PerfTests/BitSerializerBenchmark.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using BenchmarkDotNet.Attributes;
66
using BitSerialization.Common;
77
using BitSerialization.Reflection.OnTheFly;
8-
using BitSerialization.Reflection.PreCalculated;
8+
using BitSerialization.Reflection.Precalculated;
99
using System;
1010
using System.Runtime.InteropServices;
1111

@@ -94,7 +94,7 @@ public void OnTheFly()
9494
}
9595

9696
[Benchmark]
97-
public void PreCalculated()
97+
public void Precalculated()
9898
{
9999
byte[] output = new byte[_DataSerializationSize];
100100
BitSerializer<Struct3>.Serialize(output, _Data);

BitSerialization.Reflection/PreCalculated/BitSerializerT.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
using BitSerialization.Common;
2-
using BitSerialization.Reflection.PreCalculated.Implementation;
2+
using BitSerialization.Reflection.Precalculated.Implementation;
33
using BitSerialization.Reflection.Utilities;
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
77
using System.Reflection;
88
using System.Runtime.CompilerServices;
99

10-
namespace BitSerialization.Reflection.PreCalculated
10+
namespace BitSerialization.Reflection.Precalculated
1111
{
1212
public static class BitSerializer<T>
1313
where T : struct

BitSerialization.Reflection/PreCalculated/Implementation/BitSerializerArray.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using System.Collections.Generic;
88
using System.Reflection;
99

10-
namespace BitSerialization.Reflection.PreCalculated.Implementation
10+
namespace BitSerialization.Reflection.Precalculated.Implementation
1111
{
1212
internal abstract class BitSerializerArray<T>
1313
where T : struct

BitSerialization.Reflection/PreCalculated/Implementation/BitSerializerPrimitiveArrays.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using BitSerialization.Common;
66
using System;
77

8-
namespace BitSerialization.Reflection.PreCalculated.Implementation
8+
namespace BitSerialization.Reflection.Precalculated.Implementation
99
{
1010
internal sealed class BitSerializerUInt8Array<T> :
1111
BitSerializerArray<T>

BitSerialization.Reflection/PreCalculated/Implementation/BitSerializerPrimitives.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using System.Reflection;
1010
using System.Runtime.CompilerServices;
1111

12-
namespace BitSerialization.Reflection.PreCalculated.Implementation
12+
namespace BitSerialization.Reflection.Precalculated.Implementation
1313
{
1414
internal static class BitSerializerPrimitives
1515
{

BitSerialization.Reflection/PreCalculated/Implementation/BitSerializerStructArray.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using BitSerialization.Common;
66
using System;
77

8-
namespace BitSerialization.Reflection.PreCalculated.Implementation
8+
namespace BitSerialization.Reflection.Precalculated.Implementation
99
{
1010
internal sealed class BitSerializerStructArray<T> :
1111
BitSerializerArray<T>

BitSerialization.Reflection/PreCalculated/Implementation/FieldSerializationData.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System;
66
using System.Reflection;
77

8-
namespace BitSerialization.Reflection.PreCalculated.Implementation
8+
namespace BitSerialization.Reflection.Precalculated.Implementation
99
{
1010
// Function type for deserializing a field on an object.
1111
// Note: The FieldInfo value is passed into this function to allow the JIT-compiler to optimize the FieldInfo.SetValue call

BitSerialization.Tests/BitSerializerTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
using BitSerialization.Common;
66
using BitSerialization.Reflection.OnTheFly;
7-
using BitSerialization.Reflection.PreCalculated;
7+
using BitSerialization.Reflection.Precalculated;
88
using KellermanSoftware.CompareNetObjects;
99
using System;
1010
using System.Runtime.InteropServices;

README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# BitSerialization library
2+
3+
> **WARNING**: This code is experimental. Use at your own risk.
4+
5+
A .Net data serialization/deserialization library that assits with communicating with hardware that uses a binary protocol.
6+
7+
## Overview
8+
9+
When communicating with certain types hardware, usually over some kind of serial port, it is not uncommon for the hardware's API to be specified as message frames where the message's payload is made up of a sequence of integers of various sizes.
10+
11+
This library allows the message payload's structure to be specified as a `class` or `struct` and handles the the serialization and deserialization automagically using reflection.
12+
13+
The following types can be used within a message format:
14+
15+
1. Integer types (e.g. `byte`, `int`, etc.)
16+
2. Enum types.
17+
3. Other `struct` and `class` types.
18+
4. Arrays of #1, #2 and #3.
19+
20+
## Why not use .Net's inbuilt data marshalling (p/invoke)?
21+
22+
.Net's data marshalling API (e.g. the [Marshal class](https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.marshal)) is intended to be used when communicating with C-style APIs. This includes correctly handling padding between variables to ensure correct type alignment. In addition, it will only ever use the system's endianess when reading and writing integers. However, hardware APIs generally don't include alignment padding to save space and usually require a specific endianness to be used.
23+
24+
## Three implementations
25+
26+
This library includes three different implementations.
27+
28+
1. **On the fly**: This implementation looks up the type information using reflection every time a serialization or derserialization is requested. This is the simplest implementation. Though it is also the slowest.
29+
30+
2. **Precalculated**: This implementation looks up the type information using reflection a single time and then stores the steps required to serialize and deserialize the type. This implementation is rather complex (due to its "clever" use of generics). But it is substantially faster than the 'on the fly' implementation.
31+
32+
3. **Source generators**: This implementation uses the new [C# source generators](https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/) feature that is currently available in the preview version of .Net. Its implementation is slightly more complicated than the 'on the fly' implementation. Though it is substantially faster than both the 'precalculated' and 'on the fly' implementations.

0 commit comments

Comments
 (0)