Skip to content

Instantly share code, notes, and snippets.

View xoofx's full-sized avatar
🏠
Working from home

Alexandre Mutel xoofx

🏠
Working from home
View GitHub Profile
@xoofx
xoofx / PatchBenchmarkDotNet.txt
Last active May 25, 2016 21:47
Hardcoded patch for BenchmarkDotNet to have fixed GC results in SingleRun mode (only working for Markdig benchmarks env)
var config = ManualConfig.Create(DefaultConfig.Instance);
var gcDiagnoser = new MemoryDiagnoser();
config.Add(new Job { Mode = Mode.SingleRun, LaunchCount = 2, WarmupCount = 2, TargetCount = 10 });
config.Add(gcDiagnoser);
_________________________________ Patch for BenchMarkDotNet to have precise GC:
diff --git a/BenchmarkDotNet/Running/MethodInvoker.cs b/BenchmarkDotNet/Running/MethodInvoker.cs
index e8d8de5..6a46aa2 100644
@xoofx
xoofx / EvilArray.cs
Created June 1, 2016 21:56
Cast an array of blittable structs to an array of byte[], transform length as well
using System;
using System.Runtime.InteropServices;
namespace EvilArray
{
/// <summary>
/// Cast an array of structs to an array of byte[]
/// </summary>
class Program
{
@xoofx
xoofx / IP2Location.cs
Last active November 30, 2022 06:10
Fast decode a string IP address
// Related to this benchmark article https://stebet.net/benchmarking-and-performance-optimizations-in-c-using-benchmarkdotnet/
public static class IP2Locationxoofx
{
public static uint IPAddressToInteger(string input)
{
uint ipAddress = 0;
uint acc = 0;
// Note: we assume that the string is well formed
foreach (var c in input)
@xoofx
xoofx / IPIntegerToString.cs
Created June 5, 2016 23:29
Convert an integer to a string IP address
// Follow up of the discussion https://twitter.com/stebets/status/739563007202758656
public static unsafe string IntegerToIPAddress(uint input)
{
var text = stackalloc char[15];
var ptext = text + 15;
for (int i = 0; i < 4; i++)
{
if (i > 0)
{
@xoofx
xoofx / IPIntegerToString2.cs
Created June 6, 2016 04:39
Convert IP integer address to a string
// Follow up of https://twitter.com/stebets/status/739563007202758656
private static readonly string[] ByteToString = Enumerable.Range(0, 256).Select(x => x.ToString()).ToArray();
public unsafe static string FastIntegerToIPAddress2(uint input)
{
// t3.t2.t1.t0
var t0 = ByteToString[input & 0xFF];
input = input >> 8;
var t1 = ByteToString[input & 0xFF];
input = input >> 8;
@xoofx
xoofx / TestBenchNewArrayVsListVsDictionary.cs
Created June 13, 2016 05:29
Test perf allocation/fillup between an Array, a List and a Dictionary
// Check the performance of using an Array (preallocated) vs a List<T>, vs a Dictionary<string, T>
// both in terms of CPU and memory
// * Summary *
//
// BenchmarkDotNet=v0.9.7.0
// OS=Microsoft Windows NT 6.2.9200.0
// Processor=Intel(R) Core(TM) i7-4770 CPU 3.40GHz, ProcessorCount=8
// Frequency=3319351 ticks, Resolution=301.2637 ns, Timer=TSC
// HostCLR=MS.NET 4.0.30319.42000, Arch=32-bit RELEASE
// JitModules=clrjit-v4.6.1080.0
@xoofx
xoofx / FindFirstNonZeroByteAVX.cs
Created October 3, 2016 14:19
FindFirstNonZeroByte (AVX)
// Ugly pure ASM version of https://github.com/aspnet/KestrelHttpServer/pull/1138 and gist https://gist.github.com/benaadams/2dd3f99230757111e91915f638067a09
// Not performing better though...
[Benchmark]
public int TestFindFirstByteAVX()
{
var vector = _vectors[ByteSet];
return FindFirstByteAVX(ref vector);
}
@xoofx
xoofx / BenchLinearVsBinary.cs
Created October 13, 2016 11:10
Performance between a linear and binary search on a small ordered set
/*
Q: At which size is it preferrable to use binary search over a simple linear search for a small ordered set?
R: Under 5 elements, linear search is slightly faster (from 200% to 10% faster)
But in practice, not sure a switch case to select the best method is really worth it
Unless main usecase is having most of the time only 1-2 elements (so it could be optimized manually without a loop and switch to binary otherwise)
At 3-4 elements, linear is only 10-5% faster
Relative performance between linear and binary search:
size x86 x64
@xoofx
xoofx / ResizeWithSkia.cs
Created October 20, 2016 14:54
Resize an image and draw some overlay text with SkiaSharp
// You need to configure your C# project with x86 or x64 platform (Tools\Configuration Manager\Create new Platform on the project)
// otherwise the native libSkiaSharp.dll will not get copied
using System;
using System.IO;
using SkiaSharp;
namespace TestSkia
{
class Program
{