ASP.NET Session - Large Object and Many Small Objects

I have a script to optimize how my web application stores data in a session and retrieves it. I must indicate that I am using SQL Server as the session repository.

My scenario - I need to save a list of unique identifiers mapped to string values ​​in a user session for later use. The current code I have inherited uses List<T> with a custom object, but I can already see that some kind of dictionary is much better for performance.

I tested two ideas for alternatives:

  • Saving a Dictionary<int, string> in a session. When I need to return the strings, I will get the dictionary from the session once and can check each identifier on the dictionary object.

  • Since the session is basically similar to the dictionary itself, store the string directly in the session using a unique session key, for example. Session["MyString_<id>"] = stringValue" . Retrieving a value from a session would basically be a reverse operation.

The results of my tests show the following based on the operation I need to perform, and using 100 lines:

  • Dictionary - 4552 bytes, 0.1071 seconds to complete the operation.
  • Session Direct - 4441 bytes, 0.0845 seconds to complete the operation

From these results, I see that I save some space in the session (probably because I don't have the overhead of serializing the dictionary object), and it seems to get the values ​​back from the session faster, maybe because that strings are deserialized faster than objects.

So my question is: is it better for performance to store many smaller objects in a session, rather than one large? Is there a drawback in storing a large number of smaller objects against one larger object that I have not seen?

+7
source share
2 answers

There are penalties for serializing and searching for large objects (they take up more space and processor time due to the need to present a more complex structure).

And why 2 search queries when you can only do one.

In addition, all the documentation regarding caching / storage solutions suggests that it is much more efficient to serialize a single value from a list based on the computed key, rather than storing the entire dictionary and retrieving and looking for it in it.

+1
source

I think you almost answered your own question, indicating that yes, there is overhead with deserializing the objects, but I think that the real reason should be one of manageability and maintainability.

The size of the difference in storage will be minimal if you talk about 100 objects, but when you scale to 1000 objects, the differences will increase, especially if you use complex custom objects. If you have an application that has many users using 1000 sessions, you can imagine how it simply does not scale.

In addition, having many session objects, you will undoubtedly have to write more code to process each changing object. It may not be much more, but certainly more. It also potentially makes it difficult for the developer to collect code to understand your reasoning, etc. And therefore expand your code.

If you can handle a session in one barebones format, such as IEnumerable or IDictionary, then this, in my opinion, is preferable, even if there is a little overhead.

0
source

All Articles