In this post, you will learn what is a single instance codeunit in Microsoft Dynamics 365 Business Central. Where can we apply it, and how you can boost performance in certain situations. Finally, you will find the limitations of this approach.
- What Is A Single Instance Codeunit?
- How Can We Use It?
- Can It Boost Performance?
- Additional Considerations
What Is A SingleInstance Codeunit?
A single instance codeunit is a codeunit that has the ‘SingleInstance’ property set to ‘true’. This property only applies to codeunits, not tables, pages, etc. The default value is ‘false’, so to enable it, we need to set it to ‘true’. Let´s see how this property changes the behavior of the codeunit.
Microsoft defines it as the following:
When you set this property to true on a codeunit, all codeunit variables that use this codeunit use the same instance. That is, all codeunit variables of this codeunit use the same set of internal variables when the code is running on the same client. The codeunit remains instantiated until you close the company.
So, single instance means one instance. But, what is an instance or to instantiate a codeunit in the first place?
In programming, instantiation is the creation of a real instance or particular realization of an abstraction or template, such as a class of objects or a computer process. In Business Central an object or codeunit in this case, is instantiated every time it´s used. This means we have to invoke the object every time. When the object is used, the instance is closed.
With a single instance codeunit the instance remains opened. In other words, it doesn´t close and keep the values of the variables in memory. So every time it´s called later the values can be accessed.
Note that the opened instance runs only on each session of a user, and every user would have their own instance. So, there won´t be any possible problem of cross-talking between users or mixing the same variables.
Finally, only when closing the company you will close the instance.
How Can We Use It?
Erik Hougaard shows in this video one way to apply it. Which is setting the single instance property to store a value that needs to be used later. The important thing is that we need to know very well the order of execution. So the code will always execute in the desired order.
Here it is the video demo:
Can It Boost Performance?
Waldo talks briefly about a specific scenario when using global variables. You can see it in his presentation at min. 52.
He uses a developed application called ‘BCPerfTool’. This tool is open source so let´s try it ourselves. You can find it in his repository at:
We will test 2 codeunits.
A ‘normal’ codeunit, with no single instance:
And the same code in other codeunit with ‘SingleInstance’ = true.
Then, both codeunits are called 10000 times each.
To get an average of times, the process is run 10 times for both codeunits. The following graph represents the average from the 10 runs.
Without single instance it lasted on average 299 ms.
With single instance 185 ms.
A difference of 114 ms. Almost 40% faster.
We can say that for this specific case you can boost performance considerably. But this property should be used carefully, let´s see.
Having not many experience with this property I asked on Twitter the following question:
There were some interesting comments, you can follow the thread on the link. The conclusion is that you should restrict the use of this property for specific tasks (as in Erik Hougaard video). If the scenario is controlled and you are aware of the variables involved then you can save some time.
Keep in mind that issues can happen if not properly controlled. So don´t risk data integrity, if you are not sure, for saving some time.
If you want to learn more about boosting performance in Business Central follow the article below:
That´s all. Hope you find it useful.