Geeks With Blogs
Suresh c.subramanian

This post contains low level detail and can be used for code reviews in C#. In case you are looking for high level Information only, please excuse the code snippets.

Sometime back before Christmas, I was helping one of my clients to understand and fix a system performance problem on an ecommerce platform. Vacation period is the time when most of the ecommerce systems are challenged for the performance and scalability capabilities, both are different.

Performance problems are taken very seriously by the business for obvious reasons and the engagements are always short time-bound and very intense with high escalations. At the same time these problems are complex to identify and fix. Complexity increases, if the candidate system is a live and fault tolerant system. Because, the system may not go down and depending on the use cases, the data velocity of the application will keep increasing, which can very quickly invalidate any benchmarks if ever recorded in the past. And of-course business pressure keeps mounting up by every passing hour. There are many-2 factors which affect the system performance and the problems may NOT always be the bad code. Enough! more on this topic in a different write up (may be). For now it’s important to understand that, good quality code and good code review practices followed during early stages of software development, can save and benefit lot of time and money to the business. Code Quality must be taken very seriously in Product Development.

With methodical investigation and careful eliminations of non-issue candidates, I identified the suspect for the problem was in platform service’s which was built on .NET platform, further investigation pointed to the bad code and I started to deep dive into the code.

Investigating further I came across lots of data containers with the definition & usage of lots of harmless appearing struct. Harmless because, structures are very useful datatypes for defining objects which are required to be immutable and inheritance capabilities as with classes are not desirable. You can refer more details on this from MSDN. The usage of structure is no way an example of bad code. Now, with a potential suspect identified I decided to investigate further with memory profiler and I got this interesting discovery on how the same structure written differently can dramatically increase the load on the memory…

Alright, I’ll explain more with this C# code snippet

Let’s us consider these two structures data types,


Even though both the structures are designed as similar datastructures the SizeOf  DataContainerMain is more than DataContainerMainNew

Yes, you read it right! In this case the

SizeOf(typeof(DataContainerMain)) is 16 Bytes and SizeOf(typeof(DataContainerMainNew) is 12 bytes

Imagine the size difference increasing exponentially and proportional increase of memory load on the system e.g. if you have x no of structures with n size difference. i.e. x * n = wasted memory!

The answer to this difference is in how the data structures are organized physically on the memory by the microprocessor. I will not get into the details of the data structures physical organization in this post. The key point is that the physical data structure layout does effect the overall load on the memory. If you are thinking on system performance or memory optimization, you must keep the physical layout in mind while designing any data structures, data contracts or service intermediaries.

Well! Sound’s good in theory. However, how do we fix this physical layout of the structure in my source code?

Right, you can walk-through each and every structure across the source code, review to validate for physical layout and refactor, of-course you can do this, if you think you are expected to do something like this. Or you can, use StructLayoutAttribute Class. Ref:

I have preferred the example of the type structure in this post. However, there are many other examples which can be taken with respective technologies. The code reviews are a very effective mechanism to account for any such issues, "upfront" and it is just not a process to be missed, if done genuinely the value proposition is self-explanatory. You may hear different opinion on code reviews from highly agile teams. However, I think it’s completely subjective and depends on the capabilities of developers working in the team.

Go ahead and save yours or someone’s Christmas!!!

C# Source Code:

Without applying Attribute, SizeOf(typeof(DataContainerMain)) is 16 Bytes and SizeOf(typeof(DataContainerMainNew) is 12 bytes 


applying Attribute, SizeOf(typeof(DataContainerMain)) is 10 Bytes and SizeOf(typeof(DataContainerMainNew) is 10 bytes.  a saving 8 bytes for this simple data structure.


Posted on Friday, December 11, 2015 8:38 AM | Back to top

Comments on this post: Product development - Code Quality and Importance of Code reviews.

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Suresh c.subramanian | Powered by: