Skip to content

Instantly share code, notes, and snippets.

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

Antão Almada aalmada

🏠
Working from home
View GitHub Profile
@EgorBo
EgorBo / Dynamic PGO in .NET 6.0.md
Last active January 25, 2024 15:15
Dynamic PGO in .NET 6.0.md

Dynamic PGO in .NET 6.0

Dynamic PGO (Profile-guided optimization) is a JIT-compiler optimization technique that allows JIT to collect additional information about surroundings (aka profile) in tier0 codegen in order to rely on it later during promotion from tier0 to tier1 for hot methods to make them even more efficient.

What exactly PGO can optimize for us?

  1. Profile-driving inlining - inliner relies on PGO data and can be very aggressive for hot paths and care less about cold ones, see dotnet/runtime#52708 and dotnet/runtime#55478. A good example where it has visible effects is this StringBuilder benchmark:

  2. Guarded devirtualization - most monomorphic virtual/interface calls can be devirtualized using PGO data, e.g.:

void DisposeMe(IDisposable d)
@richlander
richlander / modernizing-csharp9.md
Last active April 26, 2024 17:14
Modernizing a codebase for C# 9

Modernizing a codebase for C# 9

There are lots of cases that you can improve. The examples use nullable reference types, but only the WhenNotNull example requires it.

Use the property pattern to replace IsNullorEmpty

Consider adopting the new property pattern, wherever you use IsNullOrEmpty.

string? hello = "hello world";
[StructLayout(LayoutKind.Sequential, Size = 256)]
public unsafe struct Bitset
{
public void Set(int bitIndex) => ((int*) Unsafe.AsPointer(ref Unsafe.AsRef(this)))[(bitIndex & ~7) >> 5] |= 1 << (bitIndex & 7);
public void Unset(int bitIndex) => ((int*)Unsafe.AsPointer(ref Unsafe.AsRef(this)))[(bitIndex & ~7) >> 5] ^= 1 << (bitIndex & 7);
public void SetAll() => Unsafe.InitBlock(Unsafe.AsPointer(ref Unsafe.AsRef(this)), 0xff, (uint) Unsafe.SizeOf<Bitset>());
public void UnsetAll() => Unsafe.InitBlock(Unsafe.AsPointer(ref Unsafe.AsRef(this)), 0x00, (uint) Unsafe.SizeOf<Bitset>());
}
using System;
using System.Threading;
namespace Cysharp.Threading.Tasks
{
public interface IUniTaskAsyncEnumerable<out T>
{
IUniTaskAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default);
}
@keijiro
keijiro / 00_particle_textures.md
Last active October 6, 2019 19:54
Processing sketches used to generate VFX particle textures

These are Processing sketches that I used to create VFX particle textures in a Unity project.

Usage example:

gif

Disk.pde

Generates a simple disk particle.

@garuma
garuma / Program.cs
Created January 15, 2018 23:51
Netduino 3 Wi-Fi OneWire temperature sensor sent over MQTT
using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System.Threading;
using System.IO;
using System.Net;
using SecretLabs.NETMF.Hardware.Netduino;
using Microsoft.SPOT.Net.NetworkInformation;
using uPLibrary.Networking.M2Mqtt;
// based on https://github.com/dotnet/corefx/blob/389d7ee/src/System.Memory/tests/Memory/CustomMemoryForTest.cs
unsafe class HGlobalMemory : OwnedMemory<byte>
{
private bool _disposed;
private int _referenceCount;
private byte* _buffer;
private int _length;
public HGlobalMemory(int length)
{
public struct AsciiString : IEquatable<AsciiString>
{
private readonly byte[] _data;
public AsciiString(byte[] bytes) => _data = bytes;
public AsciiString(string s) => _data = Encoding.ASCII.GetBytes(s);
public int Length => _data.Length;
@ljw1004
ljw1004 / tuple_perf.cs
Created April 20, 2017 15:02
Perf comparison ValueTuple vs Tuple vs KeyValuePair
// TUPLE MICRO-BENCHMARKS, based on https://www.dotnetperls.com/tuple-keyvaluepair
//
// Tuples are generally fastest.
// ValueTuple is fastest in the particular case of GetHashCode.
// KeyValuePair is always worst.
//
//
// RAW RESULTS
// Numbers in milliseconds (lower is better)
//
namespace InMemoreCompilation
{
public static class CodeGenerator
{
public static string GenerateCalculator()
{
var calculator = @"namespace Calculator
{
public class Calculator
{