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 / PoorMansArena.cs
Last active April 12, 2024 03:28
Never tested, likely explodes
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
unsafe class PoorMansArena : IDisposable
{
byte* _buffer = (byte*)NativeMemory.AlignedAlloc(16384, 4096);
int _filled;
int _size;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@neon-sunset
neon-sunset / TwitchChat.cs
Last active March 5, 2024 08:23
Simple Twitch IRC chat reader with U8String abstractions
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using U8;
using U8.IO;
using Warpskimmer;
using var cts = new CancellationTokenSource();
using var sigint = PosixSignalRegistration.Create(
PosixSignal.SIGINT, ctx =>
@neon-sunset
neon-sunset / WalmartDU.cs
Created January 15, 2024 03:15
We have DUs at home!
#pragma warning disable IDE0072, CS8509
using System;
using static Result<string, System.Exception>;
var result = Result.Wrap(Console.ReadLine);
Console.WriteLine(result switch
{
Ok(var value) => $"You entered: {value}",
Err(var error) => $"Error: {error.Message}"
@neon-sunset
neon-sunset / Cacheline.cs
Last active December 9, 2023 17:35
Illustrating cache line sharing penalty
using System.Diagnostics;
// Change to 128 on ARM64 macOS
const int CACHELINE = 64;
const int iterations = 10_000_000;
var coreCount = Environment.ProcessorCount;
Console.WriteLine($"""
Time to increment an int {iterations} times on {coreCount} cores.
Cache line sharing coefficient is the count of cores that have to compete for the same cache line.
// This is an adapted port of Haversine + SIMD implementation by Ash Vardanian
// Reference: https://github.com/ashvardanian/HaversineSimSIMD/blob/main/4.c
using System.Diagnostics;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
var timestamp = Stopwatch.GetTimestamp();
var coords = (Span<(float Lat, float Lon)>)
namespace System.Linq;
public static class ParallelExtensions
{
public static ParallelQuery<TResult> BatchSelect<T, TResult>(
this T[] source,
Func<Memory<T>, TResult> selector,
int degreeOfParallelism = 0)
{
return source.AsMemory().BatchSelect(selector, degreeOfParallelism);
@neon-sunset
neon-sunset / BSeachVectorized.cs
Last active August 10, 2023 17:07
SIMD B-Search
// This seems to be actually broken huh
private static bool BSearchContainsCore(Span<int> haystack, int needle)
{
// We could do better than to use divrem but stupid leetcode does not care for base latency
// so we won't bother with it either, which is wrong but no one will thank you
// for doing things properly anyway.
var rem = haystack.Length % Vector<int>.Count;
var vectors = MemoryMarshal.Cast<int, Vector<int>>(haystack[..^rem]);
var scalars = haystack[^rem..];
var needle = new Vector<int>(target);
using System.Runtime.CompilerServices;
var repro = new Example(new object(), 1234, 5678);
PrintAsMinOpts(repro);
PrintAs(repro);
PrintBitcast(repro);
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
static void PrintAsMinOpts(Example example)
{
@neon-sunset
neon-sunset / TwitchLibComparison.md
Created July 4, 2023 10:29
TwitchLib.Client message handling performance of master vs dev + fixes
BenchmarkDotNet=v0.13.5, OS=macOS 14.0 (23A5276g) [Darwin 23.0.0]
Apple M1 Pro, 1 CPU, 8 logical and 8 physical cores
.NET SDK=8.0.100-preview.7.23327.3
  [Host]        : .NET 8.0.0 (8.0.23.32502), Arm64 RyuJIT AdvSIMD
  DefaultJob    : .NET 8.0.0 (8.0.23.32502), Arm64 RyuJIT AdvSIMD
  NativeAOT 8.0 : .NET 8.0.0-preview.7.23325.2, Arm64 NativeAOT AdvSIMD

Underlying IClient.ReceiveMessage -> TwitchClient message handler -> Registered event handler invokation (if any)

@neon-sunset
neon-sunset / trace
Created June 17, 2023 12:07
Quick .NET trace
dotnet trace collect --format speedscope -- ./{path-to-executable}