Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvement #239

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions Jil/Deserialize/InlineDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class InlineDeserializer<ForType>
const string CharBufferName = "char_buffer";
const string StringBuilderName = "string_builder";


readonly Type OptionsType;
readonly DateTimeFormat DateFormat;
readonly SerializationNameFormat SerializationNameFormat;
Expand Down Expand Up @@ -1435,7 +1436,8 @@ void SkipObjectMember()

void LoadRecursiveTypeDelegate(Type recursiveType)
{
var typeCache = typeof(TypeCache<,>).MakeGenericType(OptionsType, recursiveType);

var typeCache = typeof(TypeCache<>.InnerTypeCache<>).MakeGenericType(OptionsType, recursiveType);

FieldInfo thunk;
if(ReadingFromString)
Expand Down Expand Up @@ -2639,11 +2641,11 @@ HashSet<Type> FindAndPrimeRecursiveOrReusedTypes(Type forType)
MethodInfo loadMtd;
if(ReadingFromString)
{
loadMtd = typeof(TypeCache<,>).MakeGenericType(OptionsType, primeType).GetMethod("LoadFromString", BindingFlags.Public | BindingFlags.Static);
loadMtd = typeof(TypeCache<>.InnerTypeCache<>).MakeGenericType(OptionsType, primeType).GetMethod("LoadFromString", BindingFlags.Public | BindingFlags.Static);
}
else
{
loadMtd = typeof(TypeCache<,>).MakeGenericType(OptionsType, primeType).GetMethod("Load", BindingFlags.Public | BindingFlags.Static);
loadMtd = typeof(TypeCache<>.InnerTypeCache<>).MakeGenericType(OptionsType, primeType).GetMethod("Load", BindingFlags.Public | BindingFlags.Static);
}

loadMtd.Invoke(null, new object[0]);
Expand Down Expand Up @@ -2700,7 +2702,7 @@ static class InlineDeserializerHelper
{
static Func<TextReader, int, ReturnType> BuildAlwaysFailsWithFromStream<ReturnType>(Type optionsType)
{
var specificTypeCache = typeof(TypeCache<,>).MakeGenericType(optionsType, typeof(ReturnType));
var specificTypeCache = typeof(TypeCache<>.InnerTypeCache<>).MakeGenericType(optionsType, typeof(ReturnType));
var stashField = specificTypeCache.GetField("ExceptionDuringBuildFromStream", BindingFlags.Static | BindingFlags.Public);

var emit = Emit.NewDynamicMethod(typeof(ReturnType), new[] { typeof(TextReader), typeof(int) });
Expand All @@ -2715,7 +2717,7 @@ static Func<TextReader, int, ReturnType> BuildAlwaysFailsWithFromStream<ReturnTy

static StringThunkDelegate<ReturnType> BuildAlwaysFailsWithFromString<ReturnType>(Type optionsType)
{
var specificTypeCache = typeof(TypeCache<,>).MakeGenericType(optionsType, typeof(ReturnType));
var specificTypeCache = typeof(TypeCache<>.InnerTypeCache<>).MakeGenericType(optionsType, typeof(ReturnType));
var stashField = specificTypeCache.GetField("ExceptionDuringBuildFromString", BindingFlags.Static | BindingFlags.Public);

var emit = Emit.NewDynamicMethod(typeof(ReturnType), new[] { typeof(ThunkReader).MakeByRefType(), typeof(int) });
Expand Down
102 changes: 61 additions & 41 deletions Jil/Deserialize/TypeCaches.cs
Original file line number Diff line number Diff line change
@@ -1,74 +1,94 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Jil.Deserialize
{

interface IDeserializeOptions
{
DateTimeFormat DateFormat { get; }
SerializationNameFormat SerializationNameFormat { get; }
}

static class TypeCache<TOptions, T>

internal interface IDeserializeTypeCache
{
Func<TextReader, int, T> Get<T>();
StringThunkDelegate<T> GetFromString<T>();
}

class TypeCache<TOptions> : IDeserializeTypeCache
where TOptions : IDeserializeOptions, new()
{
static readonly object ThunkInitLock = new object();
static volatile bool ThunkBeingBuilt = false;
public static volatile Func<TextReader, int, T> Thunk;
public static Exception ExceptionDuringBuildFromStream;
internal static class InnerTypeCache<T>
{
static readonly object ThunkInitLock = new object();
static volatile bool ThunkBeingBuilt = false;
public static volatile Func<TextReader, int, T> Thunk;
public static Exception ExceptionDuringBuildFromStream;

static readonly object StringThunkInitLock = new object();
static volatile bool StringThunkBeingBuilt = false;
public static volatile StringThunkDelegate<T> StringThunk;
public static Exception ExceptionDuringBuildFromString;
static readonly object StringThunkInitLock = new object();
static volatile bool StringThunkBeingBuilt = false;
public static volatile StringThunkDelegate<T> StringThunk;
public static Exception ExceptionDuringBuildFromString;

public static Func<TextReader, int, T> Get()
{
Load();
return Thunk;
}
public static Func<TextReader, int, T> Get()
{
Load();
return Thunk;
}

public static void Load()
{
if (Thunk != null) return;
public static void Load()
{
if (Thunk != null) return;

lock (ThunkInitLock)
{
if (Thunk != null || ThunkBeingBuilt) return;
ThunkBeingBuilt = true;

lock (ThunkInitLock)
var options = new TOptions();

Thunk = InlineDeserializerHelper.BuildFromStream<T>(typeof(TOptions), options.DateFormat, options.SerializationNameFormat, exceptionDuringBuild: out ExceptionDuringBuildFromStream);
}
}

public static StringThunkDelegate<T> GetFromString()
{
if (Thunk != null || ThunkBeingBuilt) return;
ThunkBeingBuilt = true;
LoadFromString();
return StringThunk;
}

public static void LoadFromString()
{
if (StringThunk != null) return;

lock (StringThunkInitLock)
{
if (StringThunk != null || StringThunkBeingBuilt) return;
StringThunkBeingBuilt = true;

var options = new TOptions();
var options = new TOptions();

Thunk = InlineDeserializerHelper.BuildFromStream<T>(typeof(TOptions), options.DateFormat, options.SerializationNameFormat, exceptionDuringBuild: out ExceptionDuringBuildFromStream);
StringThunk = InlineDeserializerHelper.BuildFromString<T>(typeof(TOptions), options.DateFormat, options.SerializationNameFormat, exceptionDuringBuild: out ExceptionDuringBuildFromString);
}
}
}

public static StringThunkDelegate<T> GetFromString()
public Func<TextReader, int, T> Get<T>()
{
LoadFromString();
return StringThunk;
return InnerTypeCache<T>.Get();
}

public static void LoadFromString()
public StringThunkDelegate<T> GetFromString<T>()
{
if (StringThunk != null) return;

lock (StringThunkInitLock)
{
if (StringThunk != null || StringThunkBeingBuilt) return;
StringThunkBeingBuilt = true;

var options = new TOptions();

StringThunk = InlineDeserializerHelper.BuildFromString<T>(typeof(TOptions), options.DateFormat, options.SerializationNameFormat, exceptionDuringBuild: out ExceptionDuringBuildFromString);
}
return InnerTypeCache<T>.GetFromString();
}
}



class MicrosoftStyle : IDeserializeOptions
{
public DateTimeFormat DateFormat { get { return DateTimeFormat.MicrosoftStyleMillisecondsSinceUnixEpoch; } }
Expand Down
Loading