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 / dotnet.md
Last active July 30, 2024 00:33
Oberon investigation on Mono 6.12.0 vs CoreCLR 9.0.100-preview.7.24375.5

Starting DeltaBlue benchmark ... DeltaBlue: iterations=640000 average: 3us total: 2403826us

Starting Richards benchmark ... Richards: iterations=2000 average: 1388us total: 2777930us

Starting Json benchmark ... Json: iterations=8000 average: 435us total: 3480222us

Starting Havlak benchmark ...

@neon-sunset
neon-sunset / .editorconfig
Last active June 30, 2024 23:42
Trying out a terser C# style
# Remove the line below if you want to inherit .editorconfig settings from higher directories
root = true
# C# files
[*.cs]
#### Core EditorConfig Options ####
# Indentation and spacing
indent_size = 4
@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)
{