Skip to content

Instantly share code, notes, and snippets.

View neon-sunset's full-sized avatar
💭
So ARM64 has FJCVTZS for JS but nothing to count UTF-8 code point length :(

neon-sunset

💭
So ARM64 has FJCVTZS for JS but nothing to count UTF-8 code point length :(
View GitHub Profile
@neon-sunset
neon-sunset / ARM64.cs
Last active September 22, 2023 16:05
ARM64 snippets for C#
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics.Arm;
namespace System.Runtime.Instrinsics;
public static class VectorExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static int CountMatches<T>(this Vector256<T> mask)
{
using Microsoft.AspNetCore.StaticFiles.Infrastructure;
using Microsoft.Extensions.FileProviders;
var builder = WebApplication.CreateBuilder(args);
// builder.Logging.SetMinimumLevel(LogLevel.Warning);
builder.Services
.AddAntiforgery()
.AddResponseCompression();
@neon-sunset
neon-sunset / SplitExtensions.cs
Last active June 20, 2023 18:14
SplitFirst and SplitLast convenience methods to handle the most common pair slicing pattern. Use .AsSpan() or .AsMemory() for alloction-free version.
// Proposal: https://github.com/dotnet/runtime/issues/75317
// This code is licensed under MIT license
// (c) 2022 neon-sunset
using System.Runtime.CompilerServices;
namespace System;
public static class SplitExtensions
{
@neon-sunset
neon-sunset / Buffer.cs
Last active February 13, 2023 19:26
A small helper to wrap a common pattern of stackalloc with fallback to array pool into a single data structure.
// This code is licensed under MIT license
// (c) 2022-2023 neon-sunset
using System.Buffers;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
[StructLayout(LayoutKind.Auto)]
#pragma warning disable IL2026
using FastCache;
using Markdig;
using Microsoft.AspNetCore.Mvc;
using NonBlocking;
var builder = WebApplication.CreateBuilder();
builder.Services.AddHttpClient();
var app = builder.Build();
@neon-sunset
neon-sunset / HttpBenchmarkTemplate.cs
Last active September 5, 2022 23:59
Lightweight HTTP throughput benchmark template for high-load scenarios. Run dotnet new console, dotnet add package RangeExtensions, copy template to Program.cs and then run with dotnet run -c release.
using System.Diagnostics;
// Set your own execution time here.
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(60));
// dotnet add package RangeExtensions
// Feel free to spawn an arbitrary (within reason) count of workers to simulate desired concurrency.
// Keep in mind that HttpClient might reuse HttpMessageHandler's, feel free to tune to your taste.
var workers = (..64) // ..Environment.ProcessorCount is also a good idea for simpler cases.
.AsEnumerable()
@neon-sunset
neon-sunset / TieredBufferAllocExample.cs
Last active January 22, 2024 11:05
Tiered buffer allocation example: stackalloc -> array pool -> native memory alloc
using Cysharp.Collections; // dotnet add package NativeMemoryArray
namespace BufferAllocExample;
public static class ExampleBufferedBytesProcessor
{
public static int ProcessBytes(IUtf8Processor processor, ReadOnlySpan<char> source, Span<byte> destination)
{
const int StackAllocLimit = 1024; // 1KB
const int ArrayPoolLimit = 1024 * 1024 * 10; // 10MB
using System.Runtime.InteropServices;
var writeable = MemoryMarshal
.AsMemory(false.ToString().AsMemory())
.Span;
"True\0".CopyTo(writeable);
Console.WriteLine((object)false); // Prints 'True'
dotnet publish -c release -f net7.0 -r {RID} -p:PublishSingleFile=true --self-contained true
public static class AsyncEnumerableExtensions
{
/// <summary>
/// Will skip faulted tasks by default
/// </summary>
public static async IAsyncEnumerable<TResult?> ParallelSelectAsync<T, TResult>(
this IEnumerable<T> items,
Func<T, Task<TResult>> operation,
int parallelism = -1,
bool throwOnError = false)