dotnet / android

.NET for Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#
MIT License
1.93k stars 527 forks source link

[Newtonsoft.Json] Slow deserialization on first use on Android only #1147

Closed JamesNK closed 2 years ago

JamesNK commented 6 years ago

Copied from https://github.com/JamesNK/Newtonsoft.Json/issues/1326 @KyleTraynor


Using JSON.NET with Xamarin with .net standard 1.6, the first use of deserialization is very slow (10+ seconds), but subsequent deserialization of the exact same json is very fast, on Android only. On iOS it is always fast.

There are several reports by different users with the same issue, some reporting reverting to json.net 8.0.3 fixing the problem, some saying it does not.

See some users here: https://stackoverflow.com/questions/42285254/json-net-slow-on-first-use-on-xamarin-android https://forums.xamarin.com/discussion/95688/very-slow-in-xamarin-forms-android-app-while-doing-deserializeobject https://forums.xamarin.com/discussion/comment/272208/#Comment_272208

I have this problem on 10.02 and 9.0.1. I am unable to downgrade to 8.0.3 because it is not .net standard compatible.

migueldeicaza commented 6 years ago

There is a repro test case on stackoverflow, link:

https://github.com/monostefan/json.net_android_slow_start

jonpryor commented 6 years ago

@migueldeicaza: Indeed, and that's the one I ported to a desktop profile: https://github.com/JamesNK/Newtonsoft.Json/issues/1326#issuecomment-354872450

$ mono gxa-1147/bin/Debug/gxa-1147.exe gxa-1147/people.json gxa-1147/people.json
Parsing: gxa-1147/people.json
        Name: Santa Claus
        Name: The Grinch
        Name: Easter bunny
        Name: Tooth ferry
  Total time: 129ms
Parsing: gxa-1147/people.json
        Name: Santa Claus
        Name: The Grinch
        Name: Easter bunny
        Name: Tooth ferry
  Total time: 0ms

Desktop mono has similar "first parse is slower than second" performance characteristics.

The original report was for the first parse taking 10 seconds, but I have not yet seen an actual repro for the 10+second case. @JamesNK suggested it might be due to Bug #56240, but the original commenter claimed that didn't improve things.

Unless/until we get a repro for the 10+second case, I'm not sure what more we can do.

rodrmoya commented 6 years ago

Tried to profile the repro test case, and didn't get much out of the runtime profiler, as samples on Android seem to be dumped not often enough (@alexrp ?) for a small app like this one (it should be dumping every second, as it does on other platforms, right?).

But looking at the code and doing some manual tests, I tend to agree with @jonpryor . The delay I see is just around 1 second (on a quite old android device), which indeed is quite expected, as, indeed, methods need to be JIT'ed the 1st time.

So yes, we need to see the 10+ second repro and profile that.

Belorus commented 6 years ago

Json.NET builds contract on first deserialization, huge reflection parsing happens, but results are cached. My app deserialized 2.7mb of json server response ocasionally, and it takes 240ms first time and 19ms on subsequent (measured on .NET on Windows, on mobile it is up to 10x of that).

In order to reduce first deserialization time i've forked Newtonsoft and OPT_OUTed most of attributes and property deserialization (use public fields only). This reduced 240ms>90ms on first deserialization (subsequent haven't changed).

jonathanpeppers commented 2 years ago

Can someone comment if you are still experiencing the same in a Release-mode .NET 6 Android project? Then try AOT-ing everything with dotnet build -c Release -p:AndroidEnableProfiledAot=false -p:RunAOTCompilation=true.

I suspect that AOT'd applications in .NET 6 wouldn't hit the same issue. Thanks!

ghost commented 2 years ago

Hi @JamesNK. We have added the "need-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

ghost commented 2 years ago

Hi @JamesNK. Due to inactivity, we will be closing this issue. Please feel free to re-open this issue if the issue persists. For enhanced visibility, if over 7 days have passed, please open a new issue and link this issue there. Thank you.