Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View Sergio0694's full-sized avatar
🚀
Coding all day

Sergio Pedri Sergio0694

🚀
Coding all day
View GitHub Profile
@Sergio0694
Sergio0694 / EventAwaiter.cs
Last active November 25, 2020 04:56
A helper to await for events in an asynchronous manner.
/// <summary>
/// A helper to await for events in an asynchronous manner.
/// </summary>
public static class EventAwaiter
{
/// <summary>
/// Awaits a specific event to be raised and executes a callback when that happens.
/// </summary>
/// <typeparam name="THandler">The type of handler to await.</typeparam>
/// <param name="register">An <see cref="Action{T}"/> to register the event.</param>
@Sergio0694
Sergio0694 / BenchmarkRunner.cs
Last active March 19, 2020 10:16
A simple class that can run a benchmark and display its results with a markdown table
using System;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using System.Text;
#nullable enable
namespace Profiling
{
@Sergio0694
Sergio0694 / ItemClickBehavior.cs
Created January 14, 2020 11:31
A behavior that listens for the ListViewBase.ItemClick event on its source and executes a specified command when that event is fired
using System.Windows.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Microsoft.Xaml.Interactivity;
#nullable enable
namespace Microsoft.Xaml.Interactions.Core
{
/// <summary>
@Sergio0694
Sergio0694 / ReadOnlySpanEnumerator.cs
Last active January 6, 2020 00:19
A fast and allocation-free method to enumerate items in a collection
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace System
{
/// <summary>
/// A <see langword="ref"/> <see langword="struct"/> that enumerates the items in a given <see cref="ReadOnlySpan{T}"/> instance
/// </summary>
/// <typeparam name="T">The type of items to enumerate</typeparam>
@Sergio0694
Sergio0694 / ReadOnlySpanExtensions.cs
Last active January 10, 2020 15:04
A fast and allocation-free API to tokenize sequences of items
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
namespace System
{
/// <summary>
/// A <see langword="class"/> with some extension methods for the <see cref="ReadOnlySpan{T}"/> type
/// </summary>
public static class ReadOnlySpanExtensions
{
@Sergio0694
Sergio0694 / MaxBenchmark.cs
Created December 3, 2019 10:40
A benchmark of various no-branch methods to compute the max of two int values
using System;
using System.Buffers;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
namespace MaxBenchmark
{
class Program
| Method | Mean | Error | StdDev | Ratio |
|------------------------ |------------:|-----------:|-----------:|------:|
| ReflectionSmall | 954.36 ms | 11.8428 ms | 11.0778 ms | 1.00 |
| ILGetterSmall | 678.82 ms | 4.5664 ms | 3.8132 ms | 0.71 |
| UnwrappedILGetterSmall | 661.00 ms | 5.2797 ms | 4.6803 ms | 0.69 |
| SingleILGetterSmall | 65.24 ms | 0.3079 ms | 0.2880 ms | 0.07 |
| | | | | |
| ReflectionMedium | 1,427.80 ms | 9.2508 ms | 8.6532 ms | 1.00 |
| ILGetterMedium | 1,023.99 ms | 6.6233 ms | 6.1954 ms | 0.72 |
| UnwrappedILGetterMedium | 1,011.03 ms | 2.6657 ms | 2.3631 ms | 0.71 |
public static unsafe (object[] References, byte[] Bytes) GetData(
Delegate instance, DataLoader loader,
int referenceCount, int byteSize)
{
// Reference and byte array
object[] refs = ArrayPool<object>.Shared.Rent(referenceCount);
byte[] bytes = ArrayPool<byte>.Shared.Rent(byteSize);
ref object r0 = ref refs.Length > 0 ? ref refs[0] : ref Unsafe.AsRef<object>(null);
ref byte r1 = ref bytes.Length > 0 ? ref bytes[0] : ref Unsafe.AsRef<byte>(null);
// Custom delegate that takes the two ref parameters we need
public delegate void DataLoader(object instance, ref object r0, ref byte r1);
/* The method that takes a delegate and the list of discovered
* fields for the closure type and its nested closure types,
* and builds our dynamic IL method that loads all the fields sequentially */
public static DataLoader BuildDataLoader(
Delegate instance
IReadOnlyList<ClosureField> fields)
{
public static void EmitStoreToAddress(this ILGenerator il, Type type)
{
if (type.IsValueType)
{
// Pick the optimal opcode to set a value type
OpCode opcode = Marshal.SizeOf(type) switch
{
// Use the faster op codes for sizes <= 8
1 when type == typeof(byte) || type == typeof(sbyte) => OpCodes.Stind_I1,
2 when type == typeof(short) || type == typeof(ushort) => OpCodes.Stind_I2,