JSON de-serialization issue


The code below shows how we are using a View to fetch results.

IView list = _cache.GetView(entityIndex.DesignDoc, entityIndex.View, true).Keys(keys);

if (list != null && list.Count() > 0)
results = list.ToList();

The issue is that the first call: _cache.GetView() takes hardly 2ms to 4ms to execute, but the
last statement: results = list.ToList() is taking ~ 600ms for around 500 objects.

Each object has around 30 attributes, and we are storing the objects as JSON in order to use Views.

So is the time being taken due JSON De-serialization?

Sunil Singh

Not exactly, the time taken is not due to json-deserialization (which obviously plays a part, though) it’s because the first line is merely building a query to execute and the third is actually executing the query - it’s a two-step process:

IView list = _cache.GetView(entityIndex.DesignDoc, entityIndex.View, true).Keys(keys);

Builds up the query expression to execute - the actual execution is deferred until this is invoked during the call to Count():

if (list != null && list.Count() > 0)

Since this requires a trip to the server to get the results and perform the count the time you are seeing here is actually that overhead along with json-deserialization and any other processing that occurs.

Again, when this line is executed:

results = list.ToList();

The query expression is executed again, this time bringing back the entire results (documents) from the server and then going through the deserialization process and finally creating and returning them as a List.

So in short, in the GetView(…) call is merely building a query to execute which is then executed twice during the call to Count() and again on ToList(), which where the 600 milliseconds is coming from.