Tuesday, June 01, 2010

Measuring performance: Dictionary Vs If Else

Mark Needham from Thought Works has recently blogged about using Dictionary instead of If Else statements. The usage of If Else is very common in similar situations however as number of scenarios grow things can become night mare. And at some point of time you have to think about some smart approach such as the one mentioned by Mark.

At first, I thought making entire logic dictionary based may impact performance because of key lookup calls. And that motivated me to write a sample script to verify my understanding. Along with the two approaches, I was also interested to know performance implications using Switch statement. For such quick performance tests, I really love playing with
Stopwatch class.

I run the test 1k times and am very happy with the results I got. Along with better readability and maintainability, you get little bit of performance improvement as well. It’s win-win from all the sides. Such tests may seem trivial but it implant solid craftsmanship towards clean code.

Results (in ms):
Using If Else > 1130
Using Switch > 1057
Using Dictionary > 1062


Thursday, May 20, 2010

Smart Data Transfer Object

Too often it happens when you work on distributed application that you communicate too many times to fulfill a single request. As in any network communication actual data transfer (serialization, compression, transfer over wire, decompression, deserialization, etc) is just one part and other network protocol activities also expense you nicely. You always want to minimize number of calls to the server by fetching all data that you require at once and in result better overall responsiveness.

In one of the web project, we had to present navigation tree on the left side. The tree nodes were populated based on authorization and node attributes such as Hidden, etc. Initially, when we started with implementation we simply fetched nodes using SELECT N+1 method. That was easy and simple. But as no. of nodes got increased, we started experiencing delayed response.

So then we come across
DTO pattern and using my own efficient n-level hierarchy traversal method we fetched all tree nodes at once to be sent to the client. But then this approach has contradicted with Split the initial payload guideline (its written for web UI for good for any presentation layer) and lazy loading (loading only if required).

As to solve second round of problem, we limited traversal depth level to 3. That not just reduced number of calls to server (and conforming with DTO) but also increased initial response. At max, however it is not limited, we had depth level of 5 so once the application is loaded client only had to make two more calls to reach to the node at fifth level. Well, I think that is acceptable.

To further optimize, may be we can think of expanding entire sub-tree when client explicitly ask to drill down for a given node. It just works for us and thought to share with you.


Monday, May 10, 2010

Do you buy Abstraction?

If your answer is NO, go and read below paragraph from The Gu. He answered people questioning ASP.NET MVC on the ground of abstraction. Check his complete post.

I always find it somewhat ironic when I hear people complain about programming abstractions not being good. Especially when these complaints are published via blogs – whose content is displayed using HTML, is styled with CSS, made interactive with JavaScript, transported over the wire using HTTP, and implemented on the server with apps written in higher-level languages, using object oriented garbage collected frameworks, running on top of either interpreted or JIT-compiled byte code runtimes, and which ultimately store the blog content and comments in relational databases ultimately accessed via SQL query strings. All of this running within a VM on a hosted server – with the OS within the VM partitioning memory across kernel and user mode process boundaries, scheduling work using threads, raising device events using signals, and using an abstract storage API fo disk persistence. It is worth keeping all of that in mind the next time you are reading a “ORM vs Stored Procedures” or “server controls – good/bad?” post. The more interesting debates are about what the best abstractions are for a particular problem.


Thursday, February 04, 2010

SQL Object Search made easy!

Ever since I developed DB Search Util back in 2006, it has made my life lot easier. I was trying to find a string instance in different tables and was literally clueless. Out of frustration for those two days, I decided to play with SQL system tables and figured out how tables and their schemas are stored. Next thing was quite simple as I just had to query the table to see if particular string instance exist in the row set or not. Obviously there were technical challenges but were trivial, to be honest.

Since then this tiny utility become popular among Altiris (now Symantec) people at Cybage and there is nothing more satisfying to know that the utility is being introduced in SMP sessions for new comers.

Even though, I am consciously aware about weaknesses of the tool, I have never stretched myself to developed those extra bits. Biggest among them is an ability to search for SQL Object names itself rather than data. I don’t know if I would do that but recent announcement from red-gate has filled the gap. Give it a try and please yourself when you try to find objects out of 500+.


Note: For legal purposes I can’t show code snippet of the DB Search Util. However the first paragraph is enough and won’t take more than 6 hours to develop.