Skip to content

Instantly share code, notes, and snippets.

View ogxd's full-sized avatar
👋

Olivier Giniaux ogxd

👋
View GitHub Profile
using System;
public class C {
public static int GetUnrollCountWithDivision(ReadOnlySpan<byte> bytes)
{
return bytes.Length >> 7 << 3;
}
public static int GetUnrollCountWithBitshift(ReadOnlySpan<byte> bytes)

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.

@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)
{
using System;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
public class C {
public void M() {
lamba = (x) => A(x) && B(x);
@ogxd
ogxd / PreAllocatedCollectionsFactory.cs
Created March 20, 2025 20:41
Pre-Allocated Collections
#pragma warning disable CA2012
#pragma warning disable CA1816
using System;
using System.Collections;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
public class PreAllocatedCollectionsFactory<T> where T : class, ICollection