Skip to content

Instantly share code, notes, and snippets.

View ogxd's full-sized avatar
👋

Olivier Giniaux ogxd

👋
View GitHub Profile
public static class HashUtils
{
public static uint Lcg(uint value)
{
unchecked {
value = 1664525 * value + 1013904223;
return value;
}
}
@ogxd
ogxd / SizeOfExtensions.cs
Created March 23, 2021 13:28
Measure size of an object through reflection
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
namespace System.Reflection
{
@ogxd
ogxd / ThrottlingMiddleware.cs
Created April 29, 2021 10:05
ASP .NET Core middleware to do control rate, concurrency and throttling over incoming requests.
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
namespace Ogxd
{
public class ThrottlingMiddleware
@ogxd
ogxd / cheatsheet.rs
Last active September 10, 2021 15:38
Rust Cheatsheet
//! Prints objects, cool for debugging
println!("{:#?}", my_object);
//! Empty struct shorthand
struct MyStruct;
public class ManualOptionMonitor<T> : IOptionsMonitor<T>
{
private readonly Dictionary<int, Action<T, string>> _listeners = new Dictionary<int, Action<T, string>>();
private volatile int _nextId;
private T _currentValue;
public T CurrentValue
{
get
{
@ogxd
ogxd / StringConcatenationExtensions.cs
Created March 28, 2022 16:48
Fast String Concatenation (better than StringBuilder when it can apply)
using System;
using System.Collections.Generic;
namespace System;
public static class StringConcatenationExtensions
{
/// <summary>
/// Concatenates strings with manamal allocations and good performance.
/// (only the end result string is allocated)
using System;
using System.Collections.Generic;
using System.Linq;
namespace Ogxd;
/// <summary>
/// Returns a string for a given enum value, without allocations
/// </summary>
/// <typeparam name="T"></typeparam>
sudo -i
dnctl pipe 1 config delay 300
echo "dummynet in proto {tcp,icmp} from any to any pipe 1" | pfctl -f -
pfctl -e
pfctl -f /etc/pf.conf
dnctl -q flush
@ogxd
ogxd / ComUtils.cs
Created January 25, 2023 08:50
Manual COM Loading
public static class ComUtils
{
private delegate int DllGetClassObject(ref Guid clsid, ref Guid iid, out nint classFactoryPtr);
private delegate void CreateInstance(nint self, nint pUnkOuter, ref Guid riid, out nint ppvObjectPtr);
public static nint Load(string library)
{
Guid exceptionProfilerGuid = new Guid("805A308B-061C-47F3-9B30-F785C3186E82");
Guid iclassFactoryGuid = new Guid("00000001-0000-0000-c000-000000000046");
Guid iCorProfilerCallback8Guid = new Guid("5BED9B15-C079-4D47-BFE2-215A140C07E0");
@ogxd
ogxd / Benchmark.cs
Last active July 11, 2023 20:57
Allocation-free string splitting. Please leave a ⭐️ if you liked it!
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Order;
// | Method | str | Mean | Error | StdDev | Allocated |
// |-------------------- |-------------------- |-----------:|----------:|----------:|----------:|
// | Split | this(...)oose [131] | 162.522 ns | 1.7171 ns | 1.6062 ns | 760 B |
// | SplitAllocationFree | this(...)oose [131] | 1.748 ns | 0.0187 ns | 0.0175 ns | - | 🚀
[Orderer(SummaryOrderPolicy.Declared)]
[MemoryDiagnoser(false)]