Skip to content

Instantly share code, notes, and snippets.

View GrabYourPitchforks's full-sized avatar
😵
On other projects, not checking GitHub notifications - ping via Teams if urgent.

Levi Broderick GrabYourPitchforks

😵
On other projects, not checking GitHub notifications - ping via Teams if urgent.
View GitHub Profile
@GrabYourPitchforks
GrabYourPitchforks / ReflectionServices.cs
Last active October 1, 2017 00:59
ReflectionServices proposal
using System;
using System.Security;
namespace System.Reflection
{
public delegate ref TField FieldAccessor<TField>(object target);
public delegate ref TField FieldAccessor<TTarget, TField>(ref TTarget target);
/// <summary>
// Input: a 32-bit integer of the form [ 00wwwwww 00xxxxxx 00yyyyyy 00zzzzzz ]
// Output: a 32-bit integer where each input byte is mapped to its corresponding BASE64 ASCII byte
private static uint ByteToBase64(uint b)
{
b += 0x41414141U; // 'A'
return b
+ (0x06U * (((b + 0x25252525U) & 0x80808080U) >> 7))
- (0x4BU * (((b + 0x0B0B0B0BU) & 0x80808080U) >> 7))
- (0x0FU * (((b + 0x01010101U) & 0x80808080U) >> 7))
+ (0x03U * ((b & 0x80808080U) >> 7));
; System_Private_CoreLib!System.Buffer.Memmove(Byte*, Byte*, UInt64):
00007ff8`1f759440 0f1f440000 nop dword ptr [rax+rax]
00007ff8`1f759445 488bc1 mov rax,rcx
00007ff8`1f759448 482bc2 sub rax,rdx
00007ff8`1f75944b 493bc0 cmp rax,r8
00007ff8`1f75944e 0f822f010000 jb System_Private_CoreLib!System.Buffer.Memmove(Byte*, Byte*, UInt64)+0x143 (00007ff8`1f759583)
00007ff8`1f759454 488bc2 mov rax,rdx
00007ff8`1f759457 482bc1 sub rax,rcx
00007ff8`1f75945a 493bc0 cmp rax,r8
00007ff8`1f75945d 0f8220010000 jb System_Private_CoreLib!System.Buffer.Memmove(Byte*, Byte*, UInt64)+0x143 (00007ff8`1f759583)
; System_Private_CoreLib!System.Buffer.Memmove(System.ByReference`1<Byte>, System.ByReference`1<Byte>, UInt64):
00007ff8`2c697e30 4883ec28 sub rsp,28h
00007ff8`2c697e34 90 nop
00007ff8`2c697e35 488bc1 mov rax,rcx
00007ff8`2c697e38 482bc2 sub rax,rdx
00007ff8`2c697e3b 493bc0 cmp rax,r8
00007ff8`2c697e3e 0f8233010000 jb System_Private_CoreLib!System.Buffer.Memmove(System.ByReference`1<Byte>, System.ByReference`1<Byte>, UInt64)+0x147 (00007ff8`2c697f77)
00007ff8`2c697e44 488bc2 mov rax,rdx
00007ff8`2c697e47 482bc1 sub rax,rcx
00007ff8`2c697e4a 493bc0 cmp rax,r8
; System_Private_CoreLib!System.Buffer.Memmove(Byte*, Byte*, UInt32):
53c15f10 55 push ebp
53c15f11 8bec mov ebp,esp
53c15f13 57 push edi
53c15f14 56 push esi
53c15f15 53 push ebx
53c15f16 83ec08 sub esp,8
53c15f19 8b4508 mov eax,dword ptr [ebp+8]
53c15f1c 8bf1 mov esi,ecx
53c15f1e 2bf2 sub esi,edx
; System_Private_CoreLib!System.Buffer.Memmove(System.ByReference`1<Byte>, System.ByReference`1<Byte>, UInt32):
53b9caec 55 push ebp
53b9caed 8bec mov ebp,esp
53b9caef 57 push edi
53b9caf0 56 push esi
53b9caf1 53 push ebx
53b9caf2 8b550c mov edx,dword ptr [ebp+0Ch]
53b9caf5 2b5508 sub edx,dword ptr [ebp+8]
53b9caf8 8b4508 mov eax,dword ptr [ebp+8]
53b9cafb 2b450c sub eax,dword ptr [ebp+0Ch]
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
// 'valueIfTrue' and 'valueIfFalse' really should be compile-time constants for maximum throughput.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int ConditionalSelect(bool condition, int valueIfTrue, int valueIfFalse)
{
// From https://software.intel.com/sites/default/files/managed/9e/bc/64-ia-32-architectures-optimization-manual.pdf
// Example 3.2. We're taking advantage of the fact that bools are passed by value as 32-bit integers,
// so we'll blit it directly into a 1 or a 0 without a jump.
// Codegen will emit a movzx, but on Ivy Bridge (and later) the CPU itself elides the instruction.
return ((-Unsafe.As<bool, int>(ref condition)) & (valueIfTrue - valueIfFalse)) + valueIfFalse;
}
@GrabYourPitchforks
GrabYourPitchforks / validating_pool.cs
Created April 2, 2018 20:21
Validating MemoryPool<T>
/*
* !! WARNING !!
*
* COMPLETELY UNTESTED CODE
*/
using Microsoft.Win32.SafeHandles;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.ConstrainedExecution;
@GrabYourPitchforks
GrabYourPitchforks / string_comp.md
Last active August 15, 2018 01:01
String performance optimizations

This tests the performance of MemoryExtensions.ToUpperInvariant(this ReadOnlySpan<char>, Span<char>), String.GetHashCode(), and String.GetHashCode(StringComparison.OrdinalIgnoreCase).

In below table:

  • baseline coreclr = 3.0.0-preview1-26808-05
  • local build (6) = local build from private dev Utf8String branch, 6th rev.
  • local build (7) = local build from private dev Utf8String branch, 7th rev.
Method Toolchain StringLength Mean Error StdDev Scaled ScaledSD
ToUpperInvariant baseline coreclr 0 27.112 ns 0.7416 ns 1.1763 ns 1.00 0.00