Skip to content

Instantly share code, notes, and snippets.

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

Marc Gravell mgravell

🏠
Working from home
View GitHub Profile
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
class Program
{
static void Main()
{
var val = GetValue();
// DoubleConverter is from http://jonskeet.uk/csharp/DoubleConverter.cs
// context: http://stackoverflow.com/questions/11828780/lazyt-lazy-loading-error-a-field-initializer-cannot-reference-the-non-static/11828838?noredirect=1#comment72503092_11828838
using System;
class Program
{
static void Main()
{
var obj = new MyType();
for (int i = 0; i < 5; i++)
{
/// <summary>
/// Does a Step with the location of caller as the label.
/// </summary>
public static IDisposable StepHere(
this MiniProfiler profiler,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0) =>
profiler?.Step($"{memberName} - {Path.GetFileName(sourceFilePath)}:{sourceLineNumber.ToString()}");
[XmlType(Namespace = "urn:ebay:apis:eBLBaseComponents")]
public class Order
{
public string OrderID { get; set; }
public string OrderStatus { get; set; }
}
[XmlType(Namespace = "urn:ebay:apis:eBLBaseComponents")]
public class OrderArray
{
@mgravell
mgravell / Odd findings.txt
Last active April 19, 2017 10:39
async; ValueTask vs Task
Objective: compare Task<T> vs ValueTask<T> in a scenario that should
be ideal for ValueTask<T> - non-trivial results, but usually (always)
already completed.
Hypothesis: ValueTask<T> should be more efficient, as it does not (in
the "already completed" case) involve allocation.
Methodology: create a test rig that computes a number using multiple
nested roll-up operations, comparing sync vs async-Task<T> vs async-ValueTask<T>;
use "async"/"await" for the rollups in the async cases. Run all tests using
@mgravell
mgravell / more awaiting.md
Last active April 20, 2017 07:08
More comparisons of async/await implementations

Objective:

For Task<T> and ValueTask<T> (T=int as a common non-trivial but inlineable case), compare async performance in the synchronous scenario (i.e. where data happens to be buffered - common in deserialization etc code) for 3 implementations:

  • using await throughout
  • using synchronous code until incompleteness detected (via IsCompleted); switch via local async Awaited if needed
  • using synchronous code until incompleteness detected (via IsCompletedSuccessfully); switch via local async Awaited if needed

Note:

@mgravell
mgravell / WidenSlow.cs
Created April 22, 2017 10:35
Why is Vector.Widen slower than looping?
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace WidenYUSlow
{
/*
packages: (may need myget feeds)
<PackageReference Include="benchmarkdotnet" Version="0.10.4" />
// edit: hmmm, actually I can see cases where this would fail epicly, so
// probably best to ignore me here - for example:
// var span = GetSomeType().GetTags() - the SomeType value has now left the
// logical stack-frame, so our ref is undefined. It *could* perhaps
// work in the context of a "ref SomeType", but again: hard scoping
// so yeah, I'm mad - ignore me and ignore this!
===========================
@mgravell
mgravell / ParamsPerf.cs
Last active June 16, 2020 21:47
Compare Span vs params array performance
using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using MemoryDiagnoser = BenchmarkDotNet.Diagnosers.MemoryDiagnoser;
using BenchmarkDotNet.Validators;
using BenchmarkDotNet.Columns;
using System.Runtime.CompilerServices;
syntax = "proto3";
import "google/protobuf/timestamp.proto";
import "google/protobuf/duration.proto";
message HazWellKnownTypes {
google.protobuf.Timestamp x = 1;
google.protobuf.Duration y = 2;
}