Skip to content

Instantly share code, notes, and snippets.

@pCYSl5EDgo
Created April 1, 2020 14:38
Show Gist options
  • Save pCYSl5EDgo/caf1f8cd65a66eba5f3aa1ff9969c3e7 to your computer and use it in GitHub Desktop.
Save pCYSl5EDgo/caf1f8cd65a66eba5f3aa1ff9969c3e7 to your computer and use it in GitHub Desktop.
#pragma warning disable 618
#pragma warning disable 612
#pragma warning disable 414
#pragma warning disable 168
namespace Utf8Json.Resolvers
{
using System;
using Utf8Json;
public class GeneratedResolver : global::Utf8Json.IJsonFormatterResolver
{
public static readonly global::Utf8Json.IJsonFormatterResolver Instance = new GeneratedResolver();
GeneratedResolver()
{
}
public global::Utf8Json.IJsonFormatter<T> GetFormatter<T>()
{
return FormatterCache<T>.formatter;
}
static class FormatterCache<T>
{
public static readonly global::Utf8Json.IJsonFormatter<T> formatter;
static FormatterCache()
{
var f = GeneratedResolverGetFormatterHelper.GetFormatter(typeof(T));
if (f != null)
{
formatter = (global::Utf8Json.IJsonFormatter<T>)f;
}
}
}
}
internal static class GeneratedResolverGetFormatterHelper
{
static readonly global::System.Collections.Generic.Dictionary<Type, int> lookup;
static GeneratedResolverGetFormatterHelper()
{
lookup = new global::System.Collections.Generic.Dictionary<Type, int>(2)
{
{typeof(global::TestType2), 0 },
{typeof(global::TestType), 1 },
};
}
internal static object GetFormatter(Type t)
{
int key;
if (!lookup.TryGetValue(t, out key)) return null;
switch (key)
{
case 0: return new Utf8Json.Formatters.TestType2Formatter();
case 1: return new Utf8Json.Formatters.TestTypeFormatter();
default: return null;
}
}
}
}
#pragma warning disable 168
#pragma warning restore 414
#pragma warning restore 618
#pragma warning restore 612
#pragma warning disable 618
#pragma warning disable 612
#pragma warning disable 414
#pragma warning disable 219
#pragma warning disable 168
namespace Utf8Json.Formatters
{
using System;
using Utf8Json;
public sealed class TestType2Formatter : global::Utf8Json.IJsonFormatter<global::TestType2>
{
readonly global::Utf8Json.Internal.AutomataDictionary ____keyMapping;
readonly byte[][] ____stringByteKeys;
public TestType2Formatter()
{
this.____keyMapping = new global::Utf8Json.Internal.AutomataDictionary()
{
{ JsonWriter.GetEncodedPropertyNameWithoutQuotation("Value"), 0},
{ JsonWriter.GetEncodedPropertyNameWithoutQuotation("Value0"), 1},
{ JsonWriter.GetEncodedPropertyNameWithoutQuotation("Value1"), 2},
{ JsonWriter.GetEncodedPropertyNameWithoutQuotation("Value2"), 3},
{ JsonWriter.GetEncodedPropertyNameWithoutQuotation("EightSensesFifth0"), 4},
{ JsonWriter.GetEncodedPropertyNameWithoutQuotation("EightSensesFifth1"), 5},
{ JsonWriter.GetEncodedPropertyNameWithoutQuotation("EightSensesFifth2"), 6},
{ JsonWriter.GetEncodedPropertyNameWithoutQuotation("EightSensesFifth3"), 7},
{ JsonWriter.GetEncodedPropertyNameWithoutQuotation("EightSensesFifth4"), 8},
{ JsonWriter.GetEncodedPropertyNameWithoutQuotation("_0123456_0123456"), 9},
};
this.____stringByteKeys = new byte[][]
{
JsonWriter.GetEncodedPropertyNameWithBeginObject("Value"),
JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator("Value0"),
JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator("Value1"),
JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator("Value2"),
JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator("EightSensesFifth0"),
JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator("EightSensesFifth1"),
JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator("EightSensesFifth2"),
JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator("EightSensesFifth3"),
JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator("EightSensesFifth4"),
JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator("_0123456_0123456"),
};
}
public void Serialize(ref JsonWriter writer, global::TestType2 value, global::Utf8Json.IJsonFormatterResolver formatterResolver)
{
if (value == null)
{
writer.WriteNull();
return;
}
writer.WriteRaw(this.____stringByteKeys[0]);
writer.WriteInt32(value.Value);
writer.WriteRaw(this.____stringByteKeys[1]);
writer.WriteInt32(value.Value0);
writer.WriteRaw(this.____stringByteKeys[2]);
writer.WriteInt32(value.Value1);
writer.WriteRaw(this.____stringByteKeys[3]);
writer.WriteInt32(value.Value2);
writer.WriteRaw(this.____stringByteKeys[4]);
writer.WriteInt32(value.EightSensesFifth0);
writer.WriteRaw(this.____stringByteKeys[5]);
writer.WriteInt32(value.EightSensesFifth1);
writer.WriteRaw(this.____stringByteKeys[6]);
writer.WriteInt32(value.EightSensesFifth2);
writer.WriteRaw(this.____stringByteKeys[7]);
writer.WriteInt32(value.EightSensesFifth3);
writer.WriteRaw(this.____stringByteKeys[8]);
writer.WriteInt32(value.EightSensesFifth4);
writer.WriteRaw(this.____stringByteKeys[9]);
writer.WriteString(value._0123456_0123456);
writer.WriteEndObject();
}
public global::TestType2 Deserialize(ref JsonReader reader, global::Utf8Json.IJsonFormatterResolver formatterResolver)
{
if (reader.ReadIsNull())
{
return null;
}
var __Value__ = default(int);
var __Value__b__ = false;
var __Value0__ = default(int);
var __Value0__b__ = false;
var __Value1__ = default(int);
var __Value1__b__ = false;
var __Value2__ = default(int);
var __Value2__b__ = false;
var __EightSensesFifth0__ = default(int);
var __EightSensesFifth0__b__ = false;
var __EightSensesFifth1__ = default(int);
var __EightSensesFifth1__b__ = false;
var __EightSensesFifth2__ = default(int);
var __EightSensesFifth2__b__ = false;
var __EightSensesFifth3__ = default(int);
var __EightSensesFifth3__b__ = false;
var __EightSensesFifth4__ = default(int);
var __EightSensesFifth4__b__ = false;
var ___0123456_0123456__ = default(string);
var ___0123456_0123456__b__ = false;
var ____count = 0;
reader.ReadIsBeginObjectWithVerify();
while (!reader.ReadIsEndObjectWithSkipValueSeparator(ref ____count))
{
var stringKey = reader.ReadPropertyNameSegmentRaw();
int key;
if (!____keyMapping.TryGetValueSafe(stringKey, out key))
{
reader.ReadNextBlock();
goto NEXT_LOOP;
}
switch (key)
{
case 0:
__Value__ = reader.ReadInt32();
__Value__b__ = true;
break;
case 1:
__Value0__ = reader.ReadInt32();
__Value0__b__ = true;
break;
case 2:
__Value1__ = reader.ReadInt32();
__Value1__b__ = true;
break;
case 3:
__Value2__ = reader.ReadInt32();
__Value2__b__ = true;
break;
case 4:
__EightSensesFifth0__ = reader.ReadInt32();
__EightSensesFifth0__b__ = true;
break;
case 5:
__EightSensesFifth1__ = reader.ReadInt32();
__EightSensesFifth1__b__ = true;
break;
case 6:
__EightSensesFifth2__ = reader.ReadInt32();
__EightSensesFifth2__b__ = true;
break;
case 7:
__EightSensesFifth3__ = reader.ReadInt32();
__EightSensesFifth3__b__ = true;
break;
case 8:
__EightSensesFifth4__ = reader.ReadInt32();
__EightSensesFifth4__b__ = true;
break;
case 9:
___0123456_0123456__ = reader.ReadString();
___0123456_0123456__b__ = true;
break;
default:
reader.ReadNextBlock();
break;
}
NEXT_LOOP:
continue;
}
var ____result = new global::TestType2();
if(__Value__b__) ____result.Value = __Value__;
if(__Value0__b__) ____result.Value0 = __Value0__;
if(__Value1__b__) ____result.Value1 = __Value1__;
if(__Value2__b__) ____result.Value2 = __Value2__;
if(__EightSensesFifth0__b__) ____result.EightSensesFifth0 = __EightSensesFifth0__;
if(__EightSensesFifth1__b__) ____result.EightSensesFifth1 = __EightSensesFifth1__;
if(__EightSensesFifth2__b__) ____result.EightSensesFifth2 = __EightSensesFifth2__;
if(__EightSensesFifth3__b__) ____result.EightSensesFifth3 = __EightSensesFifth3__;
if(__EightSensesFifth4__b__) ____result.EightSensesFifth4 = __EightSensesFifth4__;
if(___0123456_0123456__b__) ____result._0123456_0123456 = ___0123456_0123456__;
return ____result;
}
}
}
#pragma warning disable 168
#pragma warning restore 219
#pragma warning restore 414
#pragma warning restore 618
#pragma warning restore 612
using Utf8Json;
using Utf8Json.Resolvers;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
namespace test
{
[SimpleJob(RuntimeMoniker.NetCoreApp31)]
public class Program
{
public static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<Program>();
}
[Params("このベンチマークを取るのにあたりネタに振れすぎず、良い感じに読んだ人を笑わせる文章を考えるのにぬわあああん疲れたもおおおん!", "", "shorter", "a")]
public string ConstantString;
private byte[] testBytes;
private byte[] test2Bytes;
private byte[] test3Bytes;
[GlobalSetup]
public void SetUp()
{
RegisterResolver();
var test = new TestType
{
_0123456_0123456 = ConstantString,
EightSensesFifth0 = -114,
EightSensesFifth1 = -514,
EightSensesFifth2 = int.MinValue,
EightSensesFifth3 = int.MaxValue,
EightSensesFifth4 = short.MaxValue,
Value = 31,
Value0 = 0,
Value1 = 1,
Value2 = 255,
};
testBytes = JsonSerializer.Serialize(test);
var test2 = new TestType2
{
_0123456_0123456 = test._0123456_0123456,
EightSensesFifth0 = test.EightSensesFifth0,
EightSensesFifth1 = test.EightSensesFifth1,
EightSensesFifth2 = test.EightSensesFifth2,
EightSensesFifth3 = test.EightSensesFifth3,
EightSensesFifth4 = test.EightSensesFifth4,
Value = test.Value,
Value0 = test.Value0,
Value1 = test.Value1,
Value2 = test.Value2,
};
test2Bytes = JsonSerializer.Serialize(test2);
var test3 = new TestType3
{
_0123456_0123456 = test._0123456_0123456,
EightSensesFifth0 = test.EightSensesFifth0,
EightSensesFifth1 = test.EightSensesFifth1,
EightSensesFifth2 = test.EightSensesFifth2,
EightSensesFifth3 = test.EightSensesFifth3,
EightSensesFifth4 = test.EightSensesFifth4,
Value = test.Value,
Value0 = test.Value0,
Value1 = test.Value1,
Value2 = test.Value2,
};
test3Bytes = JsonSerializer.Serialize(test3);
}
private static void RegisterResolver()
{
CompositeResolver.RegisterAndSetAsDefault(
GeneratedResolver.Instance,
StandardResolver.Default
);
}
[Benchmark]
public TestType DeserializeTest_NewEmbeddedFormatter() => JsonSerializer.Deserialize<TestType>(testBytes);
[Benchmark]
public TestType2 DeserializeTest2_OriginalGeneratedFormatter() => JsonSerializer.Deserialize<TestType2>(test2Bytes);
[Benchmark]
public TestType3 DeserializeTest3_DynamicILGenerator_Formatter() => JsonSerializer.Deserialize<TestType3>(test3Bytes);
}
}
using MessagePack;
[MessagePackObject(true)]
public class TestType3
{
public int Value { get; set; }
public int Value0 { get; set; }
public int Value1 { get; set; }
public int Value2 { get; set; }
public int EightSensesFifth0 { get; set; }
public int EightSensesFifth1 { get; set; }
public int EightSensesFifth2 { get; set; }
public int EightSensesFifth3 { get; set; }
public int EightSensesFifth4 { get; set; }
public string _0123456_0123456 { get; set; }
}
[MessagePackObject(true)]
public class TestType2
{
public int Value { get; set; }
public int Value0 { get; set; }
public int Value1 { get; set; }
public int Value2 { get; set; }
public int EightSensesFifth0 { get; set; }
public int EightSensesFifth1 { get; set; }
public int EightSensesFifth2 { get; set; }
public int EightSensesFifth3 { get; set; }
public int EightSensesFifth4 { get; set; }
public string _0123456_0123456 { get; set; }
}
[MessagePackObject(true)]
public class TestType
{
public int Value { get; set; }
public int Value0 { get; set; }
public int Value1 { get; set; }
public int Value2 { get; set; }
public int EightSensesFifth0 { get; set; }
public int EightSensesFifth1 { get; set; }
public int EightSensesFifth2 { get; set; }
public int EightSensesFifth3 { get; set; }
public int EightSensesFifth4 { get; set; }
public string _0123456_0123456 { get; set; }
}
namespace Utf8Json.Formatters
{
public sealed class TestTypeFormatter : global::Utf8Json.IJsonFormatter<global::TestType>
{
private readonly byte[][] ____stringByteKeys;
public TestTypeFormatter()
{
this.____stringByteKeys = new byte[][]
{
// Value
new byte[] { 0x7B, 0x22, 0x56, 0x61, 0x6C, 0x75, 0x65, 0x22, 0x3A, },
// Value0
new byte[] { 0x2C, 0x22, 0x56, 0x61, 0x6C, 0x75, 0x65, 0x30, 0x22, 0x3A, },
// Value1
new byte[] { 0x2C, 0x22, 0x56, 0x61, 0x6C, 0x75, 0x65, 0x31, 0x22, 0x3A, },
// Value2
new byte[] { 0x2C, 0x22, 0x56, 0x61, 0x6C, 0x75, 0x65, 0x32, 0x22, 0x3A, },
// EightSensesFifth0
new byte[] { 0x2C, 0x22, 0x45, 0x69, 0x67, 0x68, 0x74, 0x53, 0x65, 0x6E, 0x73, 0x65, 0x73, 0x46, 0x69, 0x66, 0x74, 0x68, 0x30, 0x22, 0x3A, },
// EightSensesFifth1
new byte[] { 0x2C, 0x22, 0x45, 0x69, 0x67, 0x68, 0x74, 0x53, 0x65, 0x6E, 0x73, 0x65, 0x73, 0x46, 0x69, 0x66, 0x74, 0x68, 0x31, 0x22, 0x3A, },
// EightSensesFifth2
new byte[] { 0x2C, 0x22, 0x45, 0x69, 0x67, 0x68, 0x74, 0x53, 0x65, 0x6E, 0x73, 0x65, 0x73, 0x46, 0x69, 0x66, 0x74, 0x68, 0x32, 0x22, 0x3A, },
// EightSensesFifth3
new byte[] { 0x2C, 0x22, 0x45, 0x69, 0x67, 0x68, 0x74, 0x53, 0x65, 0x6E, 0x73, 0x65, 0x73, 0x46, 0x69, 0x66, 0x74, 0x68, 0x33, 0x22, 0x3A, },
// EightSensesFifth4
new byte[] { 0x2C, 0x22, 0x45, 0x69, 0x67, 0x68, 0x74, 0x53, 0x65, 0x6E, 0x73, 0x65, 0x73, 0x46, 0x69, 0x66, 0x74, 0x68, 0x34, 0x22, 0x3A, },
// _0123456_0123456
new byte[] { 0x2C, 0x22, 0x5F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x5F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x22, 0x3A, },
};
}
public void Serialize(ref JsonWriter writer, global::TestType value, global::Utf8Json.IJsonFormatterResolver formatterResolver)
{
if (value == null)
{
writer.WriteNull();
return;
}
writer.WriteRaw(this.____stringByteKeys[0]);
writer.WriteInt32(value.Value);
writer.WriteRaw(this.____stringByteKeys[1]);
writer.WriteInt32(value.Value0);
writer.WriteRaw(this.____stringByteKeys[2]);
writer.WriteInt32(value.Value1);
writer.WriteRaw(this.____stringByteKeys[3]);
writer.WriteInt32(value.Value2);
writer.WriteRaw(this.____stringByteKeys[4]);
writer.WriteInt32(value.EightSensesFifth0);
writer.WriteRaw(this.____stringByteKeys[5]);
writer.WriteInt32(value.EightSensesFifth1);
writer.WriteRaw(this.____stringByteKeys[6]);
writer.WriteInt32(value.EightSensesFifth2);
writer.WriteRaw(this.____stringByteKeys[7]);
writer.WriteInt32(value.EightSensesFifth3);
writer.WriteRaw(this.____stringByteKeys[8]);
writer.WriteInt32(value.EightSensesFifth4);
writer.WriteRaw(this.____stringByteKeys[9]);
writer.WriteString(value._0123456_0123456);
writer.WriteEndObject();
}
public global::TestType Deserialize(ref JsonReader reader, global::Utf8Json.IJsonFormatterResolver formatterResolver)
{
if (reader.ReadIsNull())
{
return null;
}
var __Value__ = default(int);
var __Value__b__ = false;
var __Value0__ = default(int);
var __Value0__b__ = false;
var __Value1__ = default(int);
var __Value1__b__ = false;
var __Value2__ = default(int);
var __Value2__b__ = false;
var __EightSensesFifth0__ = default(int);
var __EightSensesFifth0__b__ = false;
var __EightSensesFifth1__ = default(int);
var __EightSensesFifth1__b__ = false;
var __EightSensesFifth2__ = default(int);
var __EightSensesFifth2__b__ = false;
var __EightSensesFifth3__ = default(int);
var __EightSensesFifth3__b__ = false;
var __EightSensesFifth4__ = default(int);
var __EightSensesFifth4__b__ = false;
var ___0123456_0123456__ = default(string);
var ___0123456_0123456__b__ = false;
var ____count = 0;
reader.ReadIsBeginObjectWithVerify();
while (!reader.ReadIsEndObjectWithSkipValueSeparator(ref ____count))
{
var stringKey = reader.ReadPropertyNameSegmentRaw();
var array = stringKey.Array;
var offset = stringKey.Offset;
switch(stringKey.Count)
{
default:
FAIL:
reader.ReadNextBlock();
continue;
case 5:
switch ((ulong)array[offset++] | ((ulong)array[offset++] << 8) | ((ulong)array[offset++] << 16) | ((ulong)array[offset++] << 24) | ((ulong)array[offset++] << 32))
{
default: goto FAIL;
case 0x65756C6156UL:
__Value__ = reader.ReadInt32(); __Value__b__ = true;
continue;
}
case 6:
switch ((ulong)array[offset++] | ((ulong)array[offset++] << 8) | ((ulong)array[offset++] << 16) | ((ulong)array[offset++] << 24) | ((ulong)array[offset++] << 32) | ((ulong)array[offset++] << 40))
{
default: goto FAIL;
case 0x3065756C6156UL:
__Value0__ = reader.ReadInt32(); __Value0__b__ = true;
continue;
case 0x3165756C6156UL:
__Value1__ = reader.ReadInt32(); __Value1__b__ = true;
continue;
case 0x3265756C6156UL:
__Value2__ = reader.ReadInt32(); __Value2__b__ = true;
continue;
}
case 16:
if (
((ulong)array[offset++] | ((ulong)array[offset++] << 8) | ((ulong)array[offset++] << 16) | ((ulong)array[offset++] << 24) | ((ulong)array[offset++] << 32) | ((ulong)array[offset++] << 40) | ((ulong)array[offset++] << 48) | ((ulong)array[offset++] << 56)) != 0x363534333231305FUL || // _0123456
((ulong)array[offset++] | ((ulong)array[offset++] << 8) | ((ulong)array[offset++] << 16) | ((ulong)array[offset++] << 24) | ((ulong)array[offset++] << 32) | ((ulong)array[offset++] << 40) | ((ulong)array[offset++] << 48) | ((ulong)array[offset++] << 56)) != 0x363534333231305FUL // _0123456
) goto FAIL;
___0123456_0123456__ = reader.ReadString(); ___0123456_0123456__b__ = true;
continue;
case 17:
if (((ulong)array[offset++] | ((ulong)array[offset++] << 8) | ((ulong)array[offset++] << 16) | ((ulong)array[offset++] << 24) | ((ulong)array[offset++] << 32) | ((ulong)array[offset++] << 40) | ((ulong)array[offset++] << 48) | ((ulong)array[offset++] << 56)) != 0x6E65537468676945UL) goto FAIL; // EightSen
if (((ulong)array[offset++] | ((ulong)array[offset++] << 8) | ((ulong)array[offset++] << 16) | ((ulong)array[offset++] << 24) | ((ulong)array[offset++] << 32) | ((ulong)array[offset++] << 40) | ((ulong)array[offset++] << 48) | ((ulong)array[offset++] << 56)) != 0x6874666946736573UL) goto FAIL; // sesFifth
switch ((uint)array[offset++])
{
default: goto FAIL;
case 0x30U:
__EightSensesFifth0__ = reader.ReadInt32(); __EightSensesFifth0__b__ = true;
continue;
case 0x31U:
__EightSensesFifth1__ = reader.ReadInt32(); __EightSensesFifth1__b__ = true;
continue;
case 0x32U:
__EightSensesFifth2__ = reader.ReadInt32(); __EightSensesFifth2__b__ = true;
continue;
case 0x33U:
__EightSensesFifth3__ = reader.ReadInt32(); __EightSensesFifth3__b__ = true;
continue;
case 0x34U:
__EightSensesFifth4__ = reader.ReadInt32(); __EightSensesFifth4__b__ = true;
continue;
}
}
}
var ____result = new global::TestType();
if(__Value__b__) ____result.Value = __Value__;
if(__Value0__b__) ____result.Value0 = __Value0__;
if(__Value1__b__) ____result.Value1 = __Value1__;
if(__Value2__b__) ____result.Value2 = __Value2__;
if(__EightSensesFifth0__b__) ____result.EightSensesFifth0 = __EightSensesFifth0__;
if(__EightSensesFifth1__b__) ____result.EightSensesFifth1 = __EightSensesFifth1__;
if(__EightSensesFifth2__b__) ____result.EightSensesFifth2 = __EightSensesFifth2__;
if(__EightSensesFifth3__b__) ____result.EightSensesFifth3 = __EightSensesFifth3__;
if(__EightSensesFifth4__b__) ____result.EightSensesFifth4 = __EightSensesFifth4__;
if(___0123456_0123456__b__) ____result._0123456_0123456 = ___0123456_0123456__;
return ____result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment