• Stars
    star
    349
  • Rank 117,121 (Top 3 %)
  • Language
    C#
  • License
    MIT License
  • Created over 1 year ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Drop-in replacement of LINQ aggregation operations extremely faster with SIMD.

SimdLinq

GitHub Actions Releases

SimdLinq is a drop-in replacement of LINQ aggregation operations(Sum, Average, Min, Max) extremely faster with SIMD.

image

.NET 7 LINQ supports SIMD but it is very limited, due to compatibility and safety issues, it is only enabled for int[] Average, Min, Max and long[] Min, Max.

SimdLinq accelerates many methods (Sum, LongSum, Average, Min, Max, MinMax, Contains, SequenceEqual) and types (byte, sbyte, short, ushort, int, uint, long, ulong, float, double). It can also be used with List<T>, Span<T>, ReadOnlySpan<T>, Memory<T>, ReadOnlyMemory<T> in addition to T[].

Using the overload resolution priority, all target methods are automatically SIMD by simply referencing the library and setting global using.

Quick Start

This library is distributed via NuGet.

PM> Install-Package SimdLinq

Note that this library requires .NET 7 or above because this library uses static abstract members and the new cross platform .NET 7 SIMD Api.

using SimdLinq; // enable SimdLinq extension methods

var array = Enumerable.Range(1, 100000).ToArray();

var sum = array.Sum(); // uses SimdLinqExtensions.Sum

To enable SimdLinq per file, add the using SimdLinq; using directive. To enable SimdLinq across the project, use global usings in the csproj file.

<ItemGroup>
    <Using Include="SimdLinq" />
</ItemGroup>

SimdLinqExtensions has methods for concrete data types, like int[] or Span<double>, with the same names as LINQ's methods. If a method is eligible for SIMD optimization (such as the Sum of int[]), the SimdLinqExtensions method will be used for improved performance.

Unlike base LINQ, SimdLinq supports Span<T>, allowing you to call methods such as Sum, Min, etc. on Span<T> or ReadOnlySpan<T> collections.

(double Min, double Max) GetMinMax(Span<double> span)
{
    return span.MinMax();
}

MinMax is an original SimdLinq extension, that returns a tuple of Min and Max.

Compatibility

One of the reasons why LINQ's SIMD support in .NET 7 is incomplete, is compatibility. SimdLinq prioritises performance over some safety features and full compatibility with LINQ. Please note the following differences.

Sum/Average

LINQ Sum is checked but SimdLinq is unchecked(SIMD operation is not supported overflow). To reduce the risk of overflow, Sum and Average only support types that are 32-bit or higher(int, long, uint, ulong, double, float).

SimdLinq provides LongSum for int and uint, that returns long/ulong so avoid overflow.

float/double

Unlike LINQ, SimdLinq does not check for NaN in float/double Min/Max calculations. Additionally, the order of calculation for Sum is not sequential, leading to slight differences in floating-point operations compared to regular LINQ. For instance, LINQ returns 1.5710588F, while SimdLinq returns 1.5710589F. If compatibility is not a concern, this difference is not significant, but be aware of potential small tolerance differences.

Supported collections

T[], List<T>, Span<T>, Memory<T>, ReadOnlyMemory<T>, Span<T>, ReadOnlySpan<T>.

Supported methods

  • Sum for int, uint, long, ulong, float, double
  • LongSum for int, uint
  • Average for int, uint, long, ulong, float, double
  • Min for byte, sbyte, short, ushort, int, uint, long, ulong, float, double
  • Max for byte, sbyte, short, ushort, int, uint, long, ulong, float, double
  • MinMax for byte, sbyte, short, ushort, int, uint, long, ulong, float, double
  • Contains for byte, sbyte, short, ushort, int, uint, long, ulong, float, double
  • SequenceEqual for byte, sbyte, short, ushort, int, uint, long, ulong, float, double

SoA

SIMD with LINQ requires primitive array(or Span). Cysharp/StructureOfArraysGenerator makes easy to create SoA array to use SIMD easily.

License

This library is licensed under the MIT License.

More Repositories

1

UniTask

Provides an efficient allocation free async/await integration for Unity.
C#
6,891
star
2

MagicOnion

Unified Realtime/API framework for .NET platform and Unity.
C#
3,546
star
3

MemoryPack

Zero encoding extreme performance binary serializer for C# and Unity.
C#
2,543
star
4

ZString

Zero Allocation StringBuilder for .NET and Unity.
C#
1,756
star
5

MasterMemory

Embedded Typed Readonly In-Memory Document Database for .NET and Unity.
C#
1,358
star
6

MessagePipe

High performance in-memory/distributed messaging pipeline for .NET and Unity.
C#
1,184
star
7

ConsoleAppFramework

Micro-framework for console applications to building CLI tools/Daemon/Batch for .NET, C#.
C#
1,155
star
8

ZLogger

Zero Allocation Text/Structured Logger for .NET with StringInterpolation and Source Generator, built on top of a Microsoft.Extensions.Logging.
C#
1,003
star
9

Ulid

Fast .NET C# Implementation of ULID for .NET and Unity.
C#
762
star
10

R3

The new future of dotnet/reactive and UniRx.
C#
603
star
11

csbindgen

Generate C# FFI from Rust for automatically brings native code and C native library to .NET and Unity.
Rust
508
star
12

ProcessX

Simplify call an external process with the async streams in C# 8.0.
C#
391
star
13

ObservableCollections

High performance observable collections and synchronized views, for WPF, Blazor, Unity.
C#
349
star
14

UnitGenerator

C# Source Generator to create value-object, inspired by units of measure.
C#
285
star
15

AlterNats

An alternative high performance NATS client for .NET.
C#
281
star
16

RuntimeUnitTestToolkit

CLI/GUI Frontend of Unity Test Runner to test on any platform.
C#
263
star
17

YetAnotherHttpHandler

YetAnotherHttpHandler brings the power of HTTP/2 (and gRPC) to Unity and .NET Standard.
C#
249
star
18

NativeMemoryArray

Utilized native-memory backed array for .NET and Unity - over the 2GB limitation and support the modern API(IBufferWriter, ReadOnlySequence, scatter/gather I/O, etc...).
C#
231
star
19

PrivateProxy

Source Generator and .NET 8 UnsafeAccessor based high-performance strongly-typed private accessor for unit testing and runtime.
C#
207
star
20

LogicLooper

A library for building server application using loop-action programming model on .NET.
C#
203
star
21

MagicPhysX

.NET PhysX 5 binding to all platforms(win, osx, linux) for 3D engine, deep learning, dedicated server of gaming.
Rust
202
star
22

StructureOfArraysGenerator

Structure of arrays source generator to make CPU Cache and SIMD friendly data structure for high-performance code in .NET and Unity.
C#
199
star
23

Utf8StreamReader

Utf8 based StreamReader for high performance text processing.
C#
194
star
24

DFrame

Distributed load testing framework for .NET and Unity.
C#
185
star
25

LitJWT

Lightweight, Fast JWT(JSON Web Token) implementation for .NET.
C#
185
star
26

KcpTransport

KcpTransport is a Pure C# implementation of RUDP for high-performance real-time network communication
C#
183
star
27

Utf8StringInterpolation

Successor of ZString; UTF8 based zero allocation high-peformance String Interpolation and StringBuilder.
C#
124
star
28

ValueTaskSupplement

Append supplemental methods(WhenAny, WhenAll, Lazy) to ValueTask.
C#
122
star
29

CsprojModifier

CsprojModifier performs additional processing when Unity Editor generates the .csproj.
C#
120
star
30

Kokuban

Simplifies styling strings in the terminal for .NET application.
C#
119
star
31

Claudia

Unofficial Anthropic Claude API client for .NET.
C#
118
star
32

SlnMerge

SlnMerge merges the solution files when generating solution file by Unity Editor.
C#
110
star
33

GrpcWebSocketBridge

Yet Another gRPC over HTTP/1 using WebSocket implementation, primarily targets .NET platform.
C#
59
star
34

WebSerializer

Convert Object into QueryString/FormUrlEncodedContent for C# HttpClient REST Request.
C#
56
star
35

RandomFixtureKit

Fill random/edge-case value to target type for unit testing, supports both .NET Standard and Unity.
C#
42
star
36

Actions

29
star
37

DocfxTemplate

Patchworked DocFX v2 template for Cysharp
JavaScript
6
star
38

com.unity.ide.visualstudio-backport

Backport of com.unity.ide.visualstudio to before Unity 2019.4.21
C#
1
star