Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why ASP.NET MVC default Model Binder is slow? It's taking a long time to do its work

In a current project the client asked for the possibility of answering a questionnaire in two ways: using a Wizard (one question at a time) and Listing (all questions at once) in a single form. Both ways are already implemented.

The questions are loaded from the database per Manual's chapter using AJAX (this is super fast). The biggest chapter at the moment has 230 questions (each with 4 HTML input fields - input/text, select, etc). If the user selects such Chapter to answer in the Listing format, the <form> will contain at about 920 fields to be posted to the server.

I'm doing an AJAX POST request passing the data with jQuery's serialize method:

data: $("#questions :input").serialize()

This serialization takes 207.143ms to complete. I got this value debugging with Firebug in Firefox:

console.profile();
$("#questions :input").serialize();
console.profileEnd();

Again this is super fast...

The problem comes when hydrating the data received on the following action method:

public async Task<ActionResult> ListSaveAsync(IEnumerable<AnswerViewModel> questions)

As you see, the posted data is data bound to an IEnumerable<AnswerViewModel> questions. AnswerViewModel has only 4 fields to store each answer.

The thing is that it takes a considerable amount of time (precisely 10 seconds) after clicking the Save button to hit a breakpoint on this action method, that is, those 10 seconds are being spent in the model binder presumably.

An important thing to mention is that I'm using Steve Sanderson's @Html.BeginCollectionItem helper to help when materializing the ViewModel collection properties from the HTTP POST. See how the data gets in the ViewModel (Keys):

enter image description here

Do you know what I can try to do to optimize this?

I thought about 4 workarounds:

  1. Save back only the modified questions. To do this I'd need to store each answer value in a data-attribute when loading the listing and compare it with the actual value when submitting the <form> as this guy suggests here.

  2. Create AnswerViewModel JavaScript objects on the client side and pass them to the action method. Would this alleviate the Model Binder?

  3. Roll my own model binder... but I really don't know if it would be faster than the default one that comes with ASP.NET MVC. From what I've read the default model binder does a lot of reflection to set the values/hydrate the action's model parameter and this could be the bottleneck.

  4. Use FormCollection and enumerate through the posted data getting each value by key and performing validation manually as shown here.

What else do you suggest?


Update 1

I went with option 3 and implemented a custom Model Binder: AnswerModelBinder : IModelBinder and used it in that specific action method:

public async Task<ActionResult> ListSaveAsync(
             [ModelBinder(typeof(AnswerModelBinder))]List<AnswerViewModel> questions)

Now what took 10 seconds to complete takes only 2 seconds.

  • Looks like the default model binder validation checks [ ModelState ] has a big impact on performance.

Update 2

I just experienced it once again: having a List<Guid> as an action parameter and passing only 59 strings through a $.getJson call was taking ~3 seconds to hit a breakpoint in the 1st line of the action method. Changing the parameter type to List<string> made the whole thingy work in the blink of an eye.

An interesting fact is that inside the action method I did this:

List<Guid> userIds = resources.Select(Guid.Parse).ToList();

and it transforms the resources List<string> to a List<Guid> instantaneously.

For sure there's something buggy with ASP.NET model binder. I just would like to know what it is... :)

like image 506
Leniel Maccaferri Avatar asked Feb 19 '13 15:02

Leniel Maccaferri


People also ask

Why are ASP Net Applications slightly slower on first load?

Typically an application will always take a little extra time to load as the application domain starts up. Things helping exacerbate this could be anything from poorly written code (IE: Application_Start) to certain libraries you may be using (ORMs for example). How many modules do you have loaded?

What are the features of ASP.NET MVC use to reduce the time taken in developing a controller?

Scaffolding reduces the time taken to develop a controller, view, etc. in the MVC framework.


2 Answers

You can use ServiceStack JsonSerializer which is pretty fast in benchmark results here is documentation http://mono.servicestack.net/docs/text-serializers/json-serializer and here is the benchmarks http://mono.servicestack.net/benchmarks/

like image 119
Raj Avatar answered Oct 02 '22 08:10

Raj


This may not be the answer you are looking for, but it may help. Instead of using the FormCollection, try having a controller method accept a model in the signature and use a Ajax.BeginForm(). This will remove the need for the serialization, and allow MVC to do it's work. Also, having a model with List of type Question may be worth looking into. This approach will also seemingly remove the need to iterate through the values on the post as they will already be in the model.

like image 44
Anthony Mason Avatar answered Oct 02 '22 09:10

Anthony Mason