Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/iiweis/PrimitiveStaticDataGenerator
C# Source Generator for creating methods that return optimized ReadOnlySpan<T> static data from primitive values.
https://github.com/iiweis/PrimitiveStaticDataGenerator
csharp csharp-sourcegenerator
Last synced: about 1 month ago
JSON representation
C# Source Generator for creating methods that return optimized ReadOnlySpan<T> static data from primitive values.
- Host: GitHub
- URL: https://github.com/iiweis/PrimitiveStaticDataGenerator
- Owner: iiweis
- License: mit
- Created: 2021-02-13T00:05:42.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-06-18T04:05:05.000Z (over 2 years ago)
- Last Synced: 2024-08-01T22:44:06.818Z (4 months ago)
- Topics: csharp, csharp-sourcegenerator
- Language: C#
- Homepage:
- Size: 31.3 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- RSCG_Examples - PrimitiveStaticDataGenerator
- csharp-source-generators - PrimitiveStaticDataGenerator - ![stars](https://img.shields.io/github/stars/iiweis/PrimitiveStaticDataGenerator?style=flat-square&cacheSeconds=604800) ![last commit](https://img.shields.io/github/last-commit/iiweis/PrimitiveStaticDataGenerator?style=flat-square&cacheSeconds=86400) for creating methods that return optimized `ReadOnlySpan<T>` static data from primitive values. (Source Generators / Other)
README
# PrimitiveStaticDataGenerator
[![Nuget](https://img.shields.io/nuget/v/PrimitiveStaticDataGenerator?color=1f6feb)](https://www.nuget.org/packages/PrimitiveStaticDataGenerator)
> [!NOTE]
> This library was created at a time when optimization using ReadOnlySpan for constant arrays had the limitation that it did not apply to primitive types that are not single byte.
>
> Since this restriction has been removed by the support of dotnet/roslyn#61414, there is little point in using this library in .NET 7 and later environments.
> For more information on dotnet/roslyn#61414, check out [Performance Improvements in .NET 8 | Initialization](https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-8/#initialization).## Usage
### user code
```cs
using System;
using PrimitiveStaticDataGenerator;namespace MyCode
{
public partial class UserClass
{
[return: PrimitiveStaticData(1, 2, 3, 4, 5)]
public static partial ReadOnlySpan Int();
[return: PrimitiveStaticData('A', 'B', 'C')]
public static partial ReadOnlySpan Char();
}
}
```### generated code(example)
MyCode.UserClass.Char.cs
```cs
using System;
using PrimitiveStaticDataGenerator;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;namespace MyCode
{
public partial class UserClass
{
public static partial ReadOnlySpan Char()
{
ReadOnlySpan span;
if (BitConverter.IsLittleEndian)
{
span = new byte[]
{
65,
0,
66,
0,
67,
0
};
}
else
{
span = new byte[]
{
0,
65,
0,
66,
0,
67
};
}return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As(ref MemoryMarshal.GetReference(span)), 3);
}
}
}
```MyCode.UserClass.Int.cs
```cs
using System;
using PrimitiveStaticDataGenerator;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;namespace MyCode
{
public partial class UserClass
{
public static partial ReadOnlySpan Int()
{
ReadOnlySpan span;
if (BitConverter.IsLittleEndian)
{
span = new byte[]
{
1,
0,
0,
0,
2,
0,
0,
0,
3,
0,
0,
0,
4,
0,
0,
0,
5,
0,
0,
0
};
}
else
{
span = new byte[]
{
0,
0,
0,
1,
0,
0,
0,
2,
0,
0,
0,
3,
0,
0,
0,
4,
0,
0,
0,
5
};
}return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As(ref MemoryMarshal.GetReference(span)), 5);
}
}
}
```## Supported Types
- bool
- char
- sbyte
- byte
- short
- ushort
- int
- uint
- long
- ulong
- float
- double
- string