Skip to content

Instantly share code, notes, and snippets.

View ogxd's full-sized avatar
👋

Olivier Giniaux ogxd

👋
View GitHub Profile
@ogxd
ogxd / ConcatenateExtensions.cs
Created January 19, 2024 08:35
Fast String Concatenation
public static class ConcatenateExtensions
{
public static unsafe string Concatenate(this IReadOnlyList<string?> strings)
{
int size = 0;
for (int i = 0; i < strings.Count; i++)
{
string? str = strings[i];
if (str == null)
{

The Problem

What are we trying to solve?

Exception handling in .NET has several pitfalls:

Performance Overhead & Resiliency

Exception handling in .NET can be expensive in performance, especially when exceptions are thrown frequently. The process of throwing an exception involves capturing a stack trace and unwinding the stack, which is a costly operation. If an application relies heavily on exceptions for control flow, this can significantly degrade performance.

using System;
public class C {
public static int GetUnrollCountWithDivision(ReadOnlySpan<byte> bytes)
{
return bytes.Length >> 7 << 3;
}
public static int GetUnrollCountWithBitshift(ReadOnlySpan<byte> bytes)
@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)]
@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");
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
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>
@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)
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 / cheatsheet.rs
Last active September 10, 2021 15:38
Rust Cheatsheet
//! Prints objects, cool for debugging
println!("{:#?}", my_object);
//! Empty struct shorthand
struct MyStruct;