Skip to content

Commit

Permalink
Improved deserialization performance when using classes.
Browse files Browse the repository at this point in the history
Signed-off-by: rpgmaker <olamide.bakre@gmail.com>
  • Loading branch information
rpgmaker committed Nov 16, 2014
1 parent 96c30e8 commit c90e6e0
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
Binary file modified NetJSON.v12.suo
Binary file not shown.
66 changes: 66 additions & 0 deletions NetJSON/NetJSON.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3103,6 +3103,7 @@ private static MethodInfo GenerateGetClassOrDictFor(TypeBuilder typeBuilder, Typ
type, new[] { _charPtrType, _intType.MakeByRefType() });
_readMethodBuilders[key] = method;


var il = method.GetILGenerator();

var dict = il.DeclareLocal(_dictType);
Expand Down Expand Up @@ -3293,6 +3294,71 @@ private static MethodInfo GenerateGetClassOrDictFor(TypeBuilder typeBuilder, Typ
il.Emit(OpCodes.Add);
il.Emit(OpCodes.Stloc, startIndex);


//String Skipping Optimization
var charCounts = new Dictionary<char, HashSet<int>>();

var typeProps = type.GetTypeProperties();
foreach (var prop in typeProps) {
HashSet<int> charSet;
var propName = prop.Name;
char propChar = propName[0];
if (!charCounts.TryGetValue(propChar, out charSet)) {
charSet = charCounts[propChar] = new HashSet<int>();
}
charSet.Add(propName.Length);
}

var nextChar = il.DeclareLocal(_charType);
var nextLabel = il.DefineLabel();

il.Emit(OpCodes.Ldloc, ptr);
il.Emit(OpCodes.Ldloc, startIndex);
il.Emit(OpCodes.Ldc_I4_2);
il.Emit(OpCodes.Mul);
il.Emit(OpCodes.Conv_I);
il.Emit(OpCodes.Add);
il.Emit(OpCodes.Ldind_U2);
il.Emit(OpCodes.Stloc, nextChar);

foreach (var @char in charCounts) {
var charKey = @char.Key;
var hashSet = @char.Value;
var checkChar = il.DefineLabel();


il.Emit(OpCodes.Ldloc, nextChar);
il.Emit(OpCodes.Ldc_I4, (int)charKey);
il.Emit(OpCodes.Bne_Un, checkChar);


foreach (var set in hashSet) {

var checkCharByIndexLabel = il.DefineLabel();

il.Emit(OpCodes.Ldloc, ptr);
il.Emit(OpCodes.Ldloc, startIndex);
il.Emit(OpCodes.Ldc_I4, set);
il.Emit(OpCodes.Add);
il.Emit(OpCodes.Ldc_I4_2);
il.Emit(OpCodes.Mul);
il.Emit(OpCodes.Conv_I);
il.Emit(OpCodes.Add);
il.Emit(OpCodes.Ldind_U2);
il.Emit(OpCodes.Ldc_I4, (int)'"');
il.Emit(OpCodes.Bne_Un, checkCharByIndexLabel);

IncrementIndexRef(il, count: set);
il.Emit(OpCodes.Br, nextLabel);

il.MarkLabel(checkCharByIndexLabel);

}

il.MarkLabel(checkChar);
}

il.MarkLabel(nextLabel);
il.Emit(OpCodes.Br, currentQuotePrevNotLabel);
il.MarkLabel(currentQuoteLabel);
//else if(current == '"' && quotes > 0 && prev != '\\')
Expand Down
2 changes: 1 addition & 1 deletion NetJSON/NetJSON.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<id>NetJSON</id>
<title>NetJSON</title>
<tags>json json-serializer javascript JSON serializer binary</tags>
<version>1.0.5.2</version>
<version>1.0.5.3</version>
<authors>TJ Bakre</authors>
<description>Faster than Any Binary?</description>
<language>en-US</language>
Expand Down
4 changes: 2 additions & 2 deletions NetJSON/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.5.2")]
[assembly: AssemblyFileVersion("1.0.5.2")]
[assembly: AssemblyVersion("1.0.5.3")]
[assembly: AssemblyFileVersion("1.0.5.3")]
Binary file modified NetJSON/lib/net40/NetJSON.dll
Binary file not shown.

0 comments on commit c90e6e0

Please sign in to comment.