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 / 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 / CecilTool.cs
Last active June 6, 2019 13:56
List usage of non public Method/Field/Type members from a specified assembly to any external assembly references
using System;
using System.Collections.Generic;
using System.IO;
using CecilTool;
using Mono.Cecil;
namespace CecilTool
{
/// <summary>
/// Simple tool using Mono.Cecil to dump the list of usage of internals methods/fields
@xoofx
xoofx / BenchXXH3Fast.cs
Last active September 14, 2019 07:51
Experiment with XXH3Fast and XXH3FastSSE
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using System.Text;
using BenchmarkDotNet.Attributes;
using StarkPlatform.NativeCompiler.Utils;
namespace StarkPlatform.NativeCompiler.Benchmarks
{
[DisassemblyDiagnoser]
public class BenchXXH3
@xoofx
xoofx / BenchGCHandleReused.cs
Created September 21, 2019 16:30
Bench of GCHandle reused
// The following benchmark shows a barely known capability of GCHandle to be reused via their .Target
// as it is avoiding to reallocate an expensive entry into the GC table handle in the CLR runtime
/*
BenchmarkDotNet=v0.10.12, OS=Windows 10.0.18362
AMD Ryzen 9 3900X 12-Core Processor, 1 CPU, 24 logical cores and 12 physical cores
.NET Core SDK=3.0.100-rc1-014190
[Host] : .NET Core 2.1.13 (Framework 4.6.28008.01), 64bit RyuJIT
DefaultJob : .NET Core 2.1.13 (Framework 4.6.28008.01), 64bit RyuJIT
@xoofx
xoofx / LinuxUtil.cs
Created November 11, 2019 19:56
Run the same Linux command transparently from Windows (via WSL) and Linux
// Copyright 2019 - Alexandre MUTEL - License MIT
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
// Example of running a Linux command from .NET that can run transparently on Windows (via WSL) or Linux
// Assuming you are running on the same distribution, you can integrate this in your tests for example.
namespace YourNameSpace.Tests
@xoofx
xoofx / InvalidLdfldaIL.cs
Created December 11, 2019 17:36
Example of the ldflda opcode crashing on Mono (5.21 and 6.4.0)
// Create a .NET Core project, target also net472
// Requires to install NuGet package InlineIL.Fody https://github.com/ltrzesniewski/InlineIL.Fody in your project
// Run this program
using System;
using InlineIL;
namespace PlayIL
{
class Program
{
@xoofx
xoofx / TuplesFeedbackProgram.cs
Last active February 17, 2020 17:52
Feedback on dynamic Tuples experiment https://github.com/YohDeadfall/Yoh.Tuples
// Feedback on https://github.com/YohDeadfall/Yoh.Tuples
/*
| Method | Mean | Error | StdDev | Gen 0 | Gen 1 | Gen 2 | Allocated |
|---------------- |----------:|---------:|---------:|-------:|------:|------:|----------:|
| TestSimpleTuple | 50.02 ns | 0.423 ns | 0.396 ns | 0.0229 | - | - | 192 B |
| TestYohTuple | 739.97 ns | 1.783 ns | 1.580 ns | 0.1822 | - | - | 1528 B |
*/
using System;
using System.Runtime.CompilerServices;
@xoofx
xoofx / TestCodeGen.cs
Created September 8, 2015 14:45
Simple CodeDom vs Roslyn syntax comparison
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.IO;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Formatting;
using NUnit.Framework;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
@xoofx
xoofx / BenchDelegatesApp.cs
Created February 19, 2019 19:56
Benchmarks of calli vs delegate
// | Method | Mean | Error | StdDev |
// |------------- |----------:|----------:|----------:|
// | Calli | 0.6718 ns | 0.0013 ns | 0.0012 ns |
// | Delegate | 1.1366 ns | 0.0099 ns | 0.0088 ns |
// | FastDelegate | 1.6239 ns | 0.0103 ns | 0.0097 ns |
// MyClassLib.cs is compiled in another project (havent tested if compiling with Fody is working with BenchmarkDotnet in the same project)
// This file is referencing BenchDelegates.MyClassLib
using System;
@xoofx
xoofx / SafeNotSafe.cs
Created June 6, 2017 09:30
Proof of concept of (un)safe code breaking https://unbreakable-test.azurewebsites.net/
// proof of concept for breaking https://unbreakable-test.azurewebsites.net/
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
static class Program {
[MethodImpl(MethodImplOptions.NoInlining)]
public static void MethodToPatch()
{
}