Permalink
Browse files

fast skip white space is no longer an optimization; best guess, remov…

…ing some allocation pressure makes not touching the cache line a second time less likely to matter, test perf was jumping around
  • Loading branch information...
1 parent 5fc392c commit de8d5d49722a0eb3b5f3791ee67f1d55c1d4e3a1 @kevin-montrose committed Aug 10, 2014
Showing with 6 additions and 107 deletions.
  1. +6 −15 Jil/Deserialize/InlineDeserializer.cs
  2. +0 −33 Jil/Deserialize/Methods.cs
  3. +0 −59 JilTests/SpeedProofTests.cs
@@ -15,8 +15,7 @@ class InlineDeserializer<ForType>
{
public static bool AlwaysUseCharBufferForStrings = true;
public static bool UseHashWhenMatchingMembers = true;
- public static bool UseHashWhenMatchingEnums = true;
- public static bool UseFastConsumeWhiteSpace = true;
+ public static bool UseHashWhenMatchingEnums = true;
const string CharBufferName = "char_buffer";
const string StringBuilderName = "string_builder";
@@ -478,20 +477,12 @@ void ReadPrimitive(Type primitiveType)
}
ReadNumber(primitiveType);
- }
-
- void ConsumeWhiteSpace()
- {
- Emit.LoadArgument(0); // TextReader
+ }
- if (UseFastConsumeWhiteSpace)
- {
- Emit.Call(Methods.ConsumeWhiteSpaceFast); // --empty --
- }
- else
- {
- Emit.Call(Methods.ConsumeWhiteSpace); // --empty--
- }
+ void ConsumeWhiteSpace()
+ {
+ Emit.LoadArgument(0); // TextReader
+ Emit.Call(Methods.ConsumeWhiteSpace); // --empty--
}
void ExpectEndOfStream()
@@ -394,19 +394,6 @@ static void SkipNumber(TextReader reader)
}
}
- public static readonly MethodInfo ConsumeWhiteSpaceFast = typeof(Methods).GetMethod("_ConsumeWhiteSpaceFast", BindingFlags.Static | BindingFlags.NonPublic);
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- static void _ConsumeWhiteSpaceFast(TextReader reader)
- {
- int c;
- while ((c = reader.Peek()) != -1)
- {
- if (!IsWhiteSpaceFast(c)) return;
-
- reader.Read();
- }
- }
-
public static readonly MethodInfo ConsumeWhiteSpace = typeof(Methods).GetMethod("_ConsumeWhiteSpace", BindingFlags.Static | BindingFlags.NonPublic);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static void _ConsumeWhiteSpace(TextReader reader)
@@ -437,26 +424,6 @@ static bool IsWhiteSpace(int c)
c == 0x0D;
}
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- static bool IsWhiteSpaceFast(int c)
- {
- // per http://www.ietf.org/rfc/rfc4627.txt
- // insignificant whitespace in JSON is defined as
- // \u0020 - space
- // \u0009 - tab
- // \u000A - new line
- // \u000D - carriage return
-
- return
- c < 0x21 &&
- (
- c == 0x20 ||
- c == 0x09 ||
- c == 0x0A ||
- c == 0x0D
- );
- }
-
public static readonly MethodInfo ReadEncodedString = typeof(Methods).GetMethod("_ReadEncodedString", BindingFlags.Static | BindingFlags.NonPublic);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static string _ReadEncodedString(TextReader reader, ref StringBuilder commonSb)
@@ -1219,65 +1219,6 @@ public void UseHashWhenMatchingEnums()
Assert.IsTrue(hashTime < methodTime, "hashTime = " + hashTime + ", methodTime = " + methodTime);
}
- class _UseFastConsumeWhiteSpace
- {
- public int A { get; set; }
- public string B { get; set; }
- public string C { get; set; }
- }
-
- [TestMethod]
- public void UseFastConsumeWhiteSpace()
- {
- Func<TextReader, _UseFastConsumeWhiteSpace> fast;
- Func<TextReader, _UseFastConsumeWhiteSpace> normal;
-
- try
- {
- {
- InlineDeserializer<_UseFastConsumeWhiteSpace>.UseFastConsumeWhiteSpace = true;
- Exception ignored;
-
- // Build the *actual* deserializer method
- fast = InlineDeserializerHelper.Build<_UseFastConsumeWhiteSpace>(typeof(Jil.Deserialize.NewtonsoftStyleTypeCache<_UseHashWhenMatchingMembers>), dateFormat: Jil.DateTimeFormat.NewtonsoftStyleMillisecondsSinceUnixEpoch, allowHashing: true, exceptionDuringBuild: out ignored);
- }
-
- {
- InlineDeserializer<_UseFastConsumeWhiteSpace>.UseFastConsumeWhiteSpace = false;
- Exception ignored;
-
- // Build the *actual* deserializer method
- normal = InlineDeserializerHelper.Build<_UseFastConsumeWhiteSpace>(typeof(Jil.Deserialize.NewtonsoftStyleTypeCache<_UseHashWhenMatchingMembers>), dateFormat: Jil.DateTimeFormat.NewtonsoftStyleMillisecondsSinceUnixEpoch, allowHashing: true, exceptionDuringBuild: out ignored);
- }
- }
- finally
- {
- InlineDeserializer<_UseFastConsumeWhiteSpace>.UseFastConsumeWhiteSpace = true;
- }
-
- var rand = new Random(47419928);
-
- var toSerialize = new List<_UseFastConsumeWhiteSpace>();
- for (var i = 0; i < 100000; i++)
- {
- toSerialize.Add(
- new _UseFastConsumeWhiteSpace
- {
- A = rand.Next(),
- B = _RandString(rand),
- C = _RandString(rand)
- }
- );
- }
-
- toSerialize = toSerialize.Select(_ => new { _ = _, Order = rand.Next() }).OrderBy(o => o.Order).Select(o => o._).Where((o, ix) => ix % 2 == 0).ToList();
-
- double fastTime, normalTime;
- CompareTimes(toSerialize, Jil.Options.Default, fast, normal, out fastTime, out normalTime);
-
- Assert.IsTrue(fastTime < normalTime, "fastTime = " + fastTime + ", normalTime = " + normalTime);
- }
-
class _UseCustomWriteIntUnrolled
{
public List<int> A { get; set; }

0 comments on commit de8d5d4

Please sign in to comment.