TupleOverloadGenerator.Types 1.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package TupleOverloadGenerator.Types --version 1.0.2                
NuGet\Install-Package TupleOverloadGenerator.Types -Version 1.0.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TupleOverloadGenerator.Types" Version="1.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TupleOverloadGenerator.Types --version 1.0.2                
#r "nuget: TupleOverloadGenerator.Types, 1.0.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install TupleOverloadGenerator.Types as a Cake Addin
#addin nuget:?package=TupleOverloadGenerator.Types&version=1.0.2

// Install TupleOverloadGenerator.Types as a Cake Tool
#tool nuget:?package=TupleOverloadGenerator.Types&version=1.0.2                

<br /> <p align="center"> <img src="img/banner.png" alt="Logo" width="305" height="125"> </p> <h1 align="center">TupleOverloadGenerator</h1> <p align="center">Overload <code>params</code> array parameter with tuples avoiding heap allocations.</p>

Supported

Generator and Types NuGet package:

<PackageReference Include="TupleOverloadGenerator.Types" Version="1.0.2" />
<PackageReference Include="TupleOverloadGenerator" Version="1.0.2" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />

This is experimental and uses undefined behaviour! Only tested on Linux

Motivation

When producing a library we often wish to allow a variable number of arguments to be passed to a given function, such as string Concatenation. Historically the params keyword followed by an array type string[] has been to conveniently introduce a parameter with a variable number of arguments. However an array introduces a few problems, the most prevalent of which is that the array is allocated on the heap.

Modern libraries should therefore allow the user to pass a Span instead of an array, this approach is the most performant, yet calling the function is inconvenient and still requires a heap allocation for non managed, blittable types, where stackalloc is not usable.

DONT
Span<string> parts = stackalloc string[12];
parts[0] = "Hello"; [...]
AffixConcat concat = new("[", "]");
return concat.Concat(parts);

Alternatively an ArrayPool can be used, reducing the number of allocations from n to 1 for any given size. We still have allocations, and the syntax becomes even more unwieldy.

DONT
var poolArray = ArrayPool<string>.Shared.Rent(12);
Span<string> parts = poolArray.AsSpan(0, 12);
parts[0] = "Hello"; [...]
AffixConcat concat = new("[", "]");
var result = concat.Concat(parts);
ArrayPool<string>.Shared.Return(poolArray);

The solution is overloads with inline arrays. These can be represented by tuples with a single generic type parameter. This source generator generates these overloads for parameters decorated with the [TupleOverload] attribute.

DO
AffixConcat concat = new("[", ", ", "]");
return concat.Concat(("Hello", "World"));

Example

using System;
using System.Text;

namespace BasicUsageExamples;

public partial readonly record struct AffixConcat(string Prefix, string Infix, string Suffix) {
    public string Concat(ReadOnlySpan<string> parts) {
        StringBuilder sb = new();
        sb.Append(Prefix);
        var en = parts.GetEnumerator();
        if (en.MoveNext()) {
            sb.Append(en.Current);
            while (en.MoveNext()) {
                sb.Append(Infix);
                sb.Append(en.Current);
            }
        }
        sb.Append(Suffix);
        return sb.ToString();
    }

    public string Concat([TupleOverload] params string[] parts) {
        ReadOnlySpan<string> partsSpan = parts.AsSpan();
        return Concat(partsSpan);
    }
}

The above example displays the conditions required to use the sourcegenerator.

  1. A namespace directly containing a type definition. Omitted namespace doesnt allow partial definitions, and nested types are not supported.
  2. The partial type definition, e.g. sealed partial class, partial record, partial ref struct, ...
  3. A method with a parameter array, e.g. params string[].
  4. The parameter array decorated with the [TupleOverload] attribute.
  5. The parameter exclusively called with allowed methods. No other member can be used!

Please note that the example above is for demonstration purposes only! I advise using a ref struct and a ValueStringBuilder for real world applications.

Behind the scenes

TupleOverloadGenerator.Types adds three methods to tuple, which are ensured for arrays aswell, so that they can be used interchangeably. If any members on the params array are called, except these methods, the generator will fail!

  • AsSpan(): Span<T> - Returns the span over the tuple/array
  • AsRoSpan(): ReadOnlySpan<T> - Returns the span over the tuple/array
  • GetPinnableReference(): ref T - Returns the pinnable reference to the first element in the tuple/array.

The sourcegenerator TupleOverloadGenerator primarly replaces the params parameter type with a given tuple type (e.g. params string[](string, string, string)).

Q: Tuples cannot be cast to a span can they? No, they cannot. At least not trivially. To obtain a span from a tuple, we have to cheat, and by cheat I mean unsafe hacks that may not work in the future.

The source generator adds the following extension methods to the value types with 1-21 parameters:

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
[...]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T GetPinnableReference<T>(in this ValueTuple<T, T> tuple) {
    return ref Unsafe.AsRef(in tuple).Item1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReadOnlySpan<T> AsSpan<T>(in this ValueTuple<T, T> tuple) {
    return MemoryMarshal.CreateReadOnlySpan(ref tuple.GetPinnableReference(), 2);
}

GetPinnableReference

GetPinnableReference returns a reference to the first element in the tuple, treating it as a inline array. This is unsafe, because RYU may reorder the items, so that the following layout applies:

[Item2][padding][Item1][padding][Item3][padding]

If the structure has padding inconsistent with the array allocation padding (which is unlikely, but again undefined), or the structure is reordered this will not work! Therefore its best used with pointer sized values, such as nint, object, Func<>, etc.

AsSpan

As span creates a span from the reference to the first element in the tuple with length equal to the number of elements in the tuple.

The primary issue here is that MemoryMarshal.CreateReadOnlySpan is not specified to work with a tuple! At some point Microsoft may deicide that this should throw an exception instead of succeeding. We are working with undefined behaviour here!

Other then that the in keyword for the parameter too can be a problem. It specifies that the readonly-reference to the struct is passed instead of the struct itself. In and of itself this is not a problem, but the memory analyzer will complain when returning the span to a different context.

All in all I have tested this with 3.1.423, 6.0.401 and 7.0.0-rc.2.22472.3 on Linux. This is untested on macOS and Windows.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.3 842 10/17/2022
1.0.2 381 10/15/2022
1.0.1 396 10/15/2022