3

We use Newtonsoft.Json all over the place in our App and love it! However, we recently wanted to optimize our App startup time and found out by measuring various things that whenever we deserialize some JSON for the first time, there's a huge lag of about 800ms to 1s. It doesn't really matter if we use JsonConver.DeserializeObject() or a new JsonSerializer(), the lag is always noticeable on the first time.

I made a small repo case here: https://github.com/monostefan/json.net_android_slow_start

Does anybody know why this is so slow on Xamarin.Android? And is there maybe a workaround?

  • Have you read the answer [here](http://stackoverflow.com/questions/10089347/json-net-slow-serialization-and-deserialization)? Have a try to compile in Release mode. – Grace Feng Feb 17 '17 at 06:54
  • Not yet, but it doesn't matter if I compile in Release Mode. First time deserializing something still takes way longer than subsequent calls. – Stefan Reinhard Feb 17 '17 at 11:00
  • @StefanReinhard Did you ever fix this? I am having the same issue. Thanks. – Kyle May 25 '17 at 20:44

4 Answers4

1

One of our users was running into this issue and managed to solve it by doing the deserialization in a separate thread: https://forums.xamarin.com/discussion/comment/272208/#Comment_272208

E.Z. Hart
  • 5,717
  • 1
  • 30
  • 24
1

I highly suspect this is an issue with thread synchronization.

I had a glimpse of your code. With await Task.Run() may be ContextSynchronization is not happening perfectly. I would try the good old Task.Factory.StartNew.

Can you please try the following.

await Task.Factory.StartNew(
                   () =>
                   {
                        **/* Perform the service/network call + deserialization here */**
                   }

               ).ContinueWith(
                   t =>
                   {
                       //Any thing that you were trying to do in main thread context
                   }, TaskScheduler.FromCurrentSynchronizationContext()
               );
Subhamoy
  • 132
  • 3
0

Try downgrading the Json.NET version to 8.0.3.

I tried version 8.0.3 in your project https://github.com/monostefan/json.net_android_slow_start and it didn't help at all there. But it's worth spending those few minutes to try it out because in my project that downgraded version solved the performance issues with first use of Json.NET.

To me it seems that the Xamarin.Android performance of Json.NET sucks since version 9.0.1 but Xamarin.iOS seems not to be affected. This applies also to the latest Json.NET version 10.0.2. First use on any object serialization/deserialization in Xamarin.Android is unacceptably slow but subsequent operations for similar types of objects are fast. Version 8.0.3 doesn't have this first time performance penalty on my current Xamarin.Android project.

lirkki
  • 221
  • 1
  • 8
0

To me it looks like something is not right in Xamarin.Android. Downgrading to 8.0.3 as suggested by lirkki helps reduce the delay a bit, but does not solve the problem.

What I have done to workaround this problem is immediately call a dummy DeserializeObject call when the program is loaded in a separate thread (as suggested by others), and then by the time I get around to using it for real, the first dummy call is finished and the delays are gone. e.g.:

Task.Factory.StartNew(() => { var o = JsonConvert.DeserializeObject&ltDummyJsonObject&gt(dummyJson); });

But this may not be the best way to workaround this problem.

Aidan
  • 5,346
  • 2
  • 19
  • 18