Skip to content

Instantly share code, notes, and snippets.

View JosephTremoulet's full-sized avatar

Joseph Tremoulet JosephTremoulet

View GitHub Profile
@JosephTremoulet
JosephTremoulet / LoopWithExit.cs
Created September 28, 2017 19:03
Benchmark showing loop block placement optimization, adapted from https://github.com/dotnet/coreclr/issues/9692
[Config(typeof(PreviewVs20Config))]
public unsafe class LoopWithExit
{
public int length = 100;
private string test1;
private string test2;
[Setup]
public void Setup()
@JosephTremoulet
JosephTremoulet / HasFlagBench.cs
Last active September 27, 2017 22:20
Benchmark showing Enum.HasFlag optimization
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Toolchains.CsProj;
using BenchmarkDotNet.Toolchains.DotNetCli;
@JosephTremoulet
JosephTremoulet / IndexerBench-snippets.cs
Created September 26, 2017 17:05
Span benchmark improvements fromhttps://github.com/dotnet/coreclr/pull/10453
// Improved by 14%
static byte TestIndexer5(Span<byte> data, out int z)
{
byte x = 0;
z = -1;
// Write to z here should not be able to modify
// the span.
for (var idx = 0; idx < data.Length; idx++)
{
// Improved by 19%
static byte TestIndexer1(Span<byte> data)
{
int length = data.Length;
byte x = 0;
for (var idx = 0; idx < length; idx++)
{
x ^= data[idx]; // <---- bounds check here removed
}
// Improved by 18%
static byte TestKnownSizeArray(int innerIterationCount)
{
byte[] a = new byte[1024];
SetData(a);
Span<byte> data = new Span<byte>(a);
byte x = 0;
for (int i = 0; i < innerIterationCount; i++)
{
@JosephTremoulet
JosephTremoulet / Example.cs
Last active September 26, 2017 17:11
Example of ref aliasing
using System;
using System.Diagnostics;
class InvariantExample
{
int LoadLengths(int[] a, ref int[] b, Action doThings)
{
int a_len = a.Length;
int b_len = b.Length;
static byte TestIndexer1(Span<byte> data)
{
int length = data.Length;
byte x = 0;
for (var idx = 0; idx < length; idx++)
{
x ^= data[idx]; // Bounds check here now completely removed
}
@JosephTremoulet
JosephTremoulet / KnownSizeArray.cs
Created September 25, 2017 19:29
Constant-bound span tests
static byte TestKnownSizeArray(int innerIterationCount)
{
byte[] a = new byte[1024];
SetData(a);
Span<byte> data = new Span<byte>(a);
byte x = 0;
for (int i = 0; i < innerIterationCount; i++)
{
@JosephTremoulet
JosephTremoulet / NextTest.cs
Last active September 25, 2017 19:00
Testing source quoting
static byte TestKnownSizeCtor2(byte[] a, int innerIterationCount)
{
Span<byte> data1 = new Span<byte>(a, 0, 512);
Span<byte> data2 = new Span<byte>(a, 512, 512);
byte x = 0;
for (int i = 0; i < innerIterationCount; i++)
{
x = 0;
@JosephTremoulet
JosephTremoulet / NuGet.config
Created August 31, 2017 15:39
Repro for missing `ret`
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="dotnet core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" />
<add key="appveyor-bdn" value="https://ci.appveyor.com/nuget/benchmarkdotnet" />
</packageSources>
</configuration>