Event subscription and performance

When we design and write our code we need to think about performance.

We have been used to thinking about database performance, using FindFirst(), FindSet(), IsEmpty() where appropriate.

We also need to think about performance when we create our subscriber Codeunits.

Let’s consider this Codeunit.

codeunit 50100 MySubscriberCodeunit
{
    trigger OnRun()
    begin
        
    end;
    

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post", 'OnBeforePostSalesDoc', '', true, true)] 
    local procedure MyProcedure(var SalesHeader: Record "Sales Header")
    begin
        Message('I am pleased that you called.');
    end;


}

Every time any user posts a sales document this subscriber will be executed.

Executing this subscriber will need to load an instance of this Codeunit into the server memory. After execution the Codeunit instance is trashed.

The resources needed to initiate an instance of this Codeunit and trash it again, and doing that for every sales document being posted are a waste of resources.

If we change the Codeunit and make it a “Single Instance”.

codeunit 50100 MySubscriberCodeunit
{
    SingleInstance = true;
    
    trigger OnRun()
    begin
        
    end;
    

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post", 'OnBeforePostSalesDoc', '', true, true)] 
    local procedure MyProcedure(var SalesHeader: Record "Sales Header")
    begin
        Message('I am pleased that you called.');
    end;


}

What happens now is that Codeunit only has one instance for each session. When the first sales document is posted then the an instance of the Codeunit is created and kept in memory on the server as long as the session is alive.

This will save the resources needed to initialize an instance and tear it down again.

Making sure that our subscriber Codeunits are set to single instance is even more important for subscribers to system events that are frequently executed.

Note that a single instance Codeunit used for subscription should not have any global variables, since the global variables are also kept in memory though out the session lifetime.

Make sure that whatever is executed inside a single instance subscriber Codeunit is executed in a local procedure. The variables inside a local procedure are cleared between every execution, also in a single instance Codeunit.

codeunit 50100 MySubscriberCodeunit
{
    SingleInstance = true;

    trigger OnRun()
    begin
        
    end;
    

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post", 'OnBeforePostSalesDoc', '', true, true)] 
    local procedure MyProcedure(var SalesHeader: Record "Sales Header")
    begin
        ExecuteBusinessLogic(SalesHeader);

    end;

    local procedure ExecuteBusinessLogic(SalesHeader: Record "Sales Header")
    var
        Customer: Record Customer;
    begin
        Message('I am pleased that you called.');    
    end;

}

If your custom code executes every time that the subscriber is executed then I am fine with having that code in a local procedure inside the single instance Codeunit.

Still, I would suggest putting the code in another Codeunit, and keeping the subscriber Codeunit as small as possible.

This is even more important if the custom code only executes on a given condition.

An example of a Codeunit that you call from the subscriber Codeunit could be like this.

codeunit 50001 MyCodeCalledFromSubscriber
{
    TableNo = "Sales Header";
    
    trigger OnRun()
    begin
        ExecuteBusinessLogic(Rec);
    end;
    local procedure ExecuteBusinessLogic(SalesHeader: Record "Sales Header")
    var
        Customer: Record Customer;
    begin
        Message('I am pleased that you called.');    
    end;
}

And I change my subscriber Codeunit to only execute this code on a given condition.

codeunit 50100 MySubscriberCodeunit
{
    SingleInstance = true;

    trigger OnRun()
    begin
        
    end;
    

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post", 'OnBeforePostSalesDoc', '', true, true)] 
    local procedure MyProcedure(var SalesHeader: Record "Sales Header")
    begin
        ExecuteBusinessLogic(SalesHeader);

    end;

    local procedure ExecuteBusinessLogic(SalesHeader: Record "Sales Header")
    var
        MyCodeCalledFromSubscriber: Codeunit MyCodeCalledFromSubscriber;
    begin
        if SalesHeader."Document Type" = SalesHeader."Document Type"::Order then
            MyCodeCalledFromSubscriber.Run(SalesHeader);
    end;

}

This pattern makes sure that the execution is a fast as possible and no unneeded variables are populating the server memory.

Comment List
Related
Recommended