Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON serialization performance issue on WP7

I have a .JSON file which is approx. 1.5MB in size containing around 1500 JSON objects that I want to convert into domain objects at the start-up of my app.

Currently my process on the Phone (not on my development PC) takes around 23 seconds which is far too slow for me and is forcing me to write the list of objects into ApplicationSettings so that I dont have to do it each time the app loads (just first off), but even that takes 15-odd seconds to write to, and 16 seconds to read from, all of which is not really good enough.

I have not had a lot of serialization experience and I dont really know the fastest way to get it done.

Currently, I am using the System.Runtime.Serialization namespace with DataContract and DataMember approach.

Any ideas on performance with this type of data loading?

like image 482
Mark Avatar asked Feb 23 '11 23:02

Mark


People also ask

Should I use a reader or writer for JSON serialization?

Using a reader or writer directly skips any of the overhead from a serializer, such as reflection. If performance is important and you don't mind writing more code to get it, then this is your best choice. You can read more about using JsonReader/JsonWriter here: Basic Reading and Writing JSON

How can I test the performance of a JSON application?

For basic testing, you can use Visual Studio’s built in performance analyzer with a simple console app. Grab a good sample of your JSON and do various serialize/deserialize tests tens of thousands of times in a loop and watch how long it takes, CPU, and memory usage.

Why read and write JSON a piece at a time?

Reading or writing JSON a piece at a time, instead of having the entire JSON string loaded into memory, is especially important when working with JSON documents greater than 85kb in size to avoid the JSON string ending up in the large object heap.

Is jsonserializer faster than javascriptserializer?

Out of the box Json.NET is faster than DataContractJsonSerializer and JavaScriptSerializer. Here are some tips to make it go even faster. The IContractResolver resolves .NET types to contracts that are used during serialization inside JsonSerializer.


2 Answers

I found the Json.NET library to be more performant and to have better options that the standard json serializer.

One performance issue I encountered in my app was that my domain objects implemented INotifyPropertyChanged with code to support dispatching the event back to the UI thread. Since the deserialization code populated those properties I was doing a lot of thread marshalling that didn't need to be there. Cutting out the notifications during deserialization substantially increased performance.

Update: I was using Caliburn Micro which has a property on PropertyChangedBase that can turn off property changed notifications. I then added the following:

[OnDeserializing]
public void OnDeserializing(StreamingContext context)
{
    IsNotifying = false;
}

[OnDeserialized]
public void OnDeserialized(StreamingContext context)
{
    IsNotifying = true;
}
like image 151
Nigel Sampson Avatar answered Sep 24 '22 06:09

Nigel Sampson


Try profiling your app with the free EQATEC Profiler for WP7. The real issue could be something completely unexpected and easy to fix, like the INotifyPropertyChanged-example Nigel mentions.

like image 21
Richard Flamsholt Avatar answered Sep 24 '22 06:09

Richard Flamsholt