Skip to content

Instantly share code, notes, and snippets.

View eiriktsarpalis's full-sized avatar

Eirik Tsarpalis eiriktsarpalis

View GitHub Profile
@eiriktsarpalis
eiriktsarpalis / typesafe-serialization.md
Last active March 4, 2025 16:56
Fully type-safe .NET serialization

Fully type-safe .NET serialization

One important application of source generators today is bringing Native AOT support to libraries that map .NET types to programs. Examples of this include System.Text.Json, ConfigurationBinder, S.C.DataAnnotations, etc. A key constraint of the current crop of generators is that they only work with closed types -- for example having a source generated contract for type Foo doesn't imply a source generated contract for List<Foo> or other derivative types: Foo[], Dictionary<string, Foo>, List<List<List<Foo>>>, etc. These need to be configured explicitly.

The Serde.NET and PolyType libraries are experiments in building the next generation of .NET serialization. Both lean heavily on static abstracts but the missing ingredient in unlocking full-blown trait driven programming is extension interface implementations.

To give a more concrete example, let's consider a hypothetical inte

@eiriktsarpalis
eiriktsarpalis / EnumMemberJsonStringEnumConverter.cs
Last active July 14, 2024 10:26
Adding `EnumMemberAttribute` support to System.Text.Json
namespace System.Text.Json.Serialization.DataContractExtensions;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.Json;
using System.Text.Json.Serialization;
/// <summary>
/// Provides an AOT-compatible extension for <see cref="JsonStringEnumConverter"/> that adds support for <see cref="EnumMemberAttribute"/>.
@eiriktsarpalis
eiriktsarpalis / PriorityQueue.cs
Last active September 22, 2020 17:34 — forked from safern/PriorityQueue.cs
PriorityQueue api surface
namespace System.Collections.Generic
{
class PriorityQueue<TElement, TPriority> : IReadOnlyCollection<(TElement Element, TPriority Priority)>
{
#region Constructors
public PriorityQueue() { }
public PriorityQueue(IComparer<TPriority> comparer) { }
public PriorityQueue(IEnumerable<(TElement Element, TPriority Priority)> values) { }
public PriorityQueue(IEnumerable<(TElement Element, TPriority Priority)> values, IComparer<TPriority> comparer) { }
#endregion
@eiriktsarpalis
eiriktsarpalis / Maybe.cs
Last active April 6, 2020 10:26
Encoding a Maybe monad in C# using Eff, https://github.com/nessos/Eff
namespace Maybe
{
public struct Maybe<T>
{
public bool HasValue { get; }
public T Value { get; }
private Maybe(T value)
{
HasValue = true;
@eiriktsarpalis
eiriktsarpalis / Results.md
Last active February 5, 2020 16:53
ObjectIdGenerator Performance
Method N Mean Error StdDev Gen 0 Gen 1 Gen 2 Allocated
ObjectIdGenerator 10 261.0 ns 3.92 ns 3.28 ns 0.0176 - - 280 B
DictObjectIdGenerator 10 415.4 ns 7.92 ns 8.81 ns 0.0167 - - 264 B
ObjectIdGenerator 100 2,237.4 ns 38.87 ns 32.45 ns 0.0458 - - 776 B
DictObjectIdGenerator 100 3,697.2 ns 72.95 ns 71.64 ns 0.0648 - - 1040 B
ObjectIdGenerator 1000 27,641.9 ns 465.12 ns 412.32 ns 0.0916 - - 1912 B
DictObjectIdGenerator 1000 35,959.2 ns
@eiriktsarpalis
eiriktsarpalis / ackermann.cs
Created November 19, 2019 12:48
Task vs ValueTask performance
using System;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
namespace ValueTaskBenchmarks
{
// Uses the Ackermann function to measure performance of task and friends
// c.f. https://en.wikipedia.org/wiki/Ackermann_function
@eiriktsarpalis
eiriktsarpalis / ThunkBuilder.cs
Last active April 6, 2020 10:26
Delayable MethodBuilders
using System;
using System.Collections.Concurrent;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Security;
using System.Threading.Tasks;
namespace ThunkBuilder
{
@eiriktsarpalis
eiriktsarpalis / Atom.cs
Created June 22, 2019 12:20
Clojure-style atoms for C#
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Atom
{
public class Atom<T> where T : class
{
private T _value;
@eiriktsarpalis
eiriktsarpalis / slnTools.fsx
Created December 17, 2018 13:40
Solution Generation
[<RequireQualifiedAccess>]
module SlnTools
// Simple tool for generating Solution files out of a list of projects.
open System
open System.IO
open System.Text.RegularExpressions
let fsharpProjectTypeGuid = Guid.Parse "6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705"
@eiriktsarpalis
eiriktsarpalis / keybase.md
Created October 6, 2018 18:05
KeyBase proof

Keybase proof

I hereby claim:

  • I am eiriktsarpalis on github.
  • I am eiriktsarpalis (https://keybase.io/eiriktsarpalis) on keybase.
  • I have a public key ASDsjo4ajiE7h4qGM_HbGrq2lco-Kj5LfRn2uCjZ2axtIQo

To claim this, I am signing this object: