Early and Late binding
Many times users have mentioned performance issues when co-simulating using OpenDSS. The interface proposed in OpenDSS to perform co-simulations is the COM interface, which can be accessed from almost every programming language; however, not necessarily in the faster way. Because of this, many users blame the COM interface for their performance issues. The aim of this document is to clarify why users could experience a low performance in terms of co-simulation speed and to give a guide about how to improve it.
Early Binding and Late Binding
Early and Late bindings are two computer programming mechanisms for accessing COM servers/ActiveX controls considering the features of the interface objects and classes. Late binding is focused on giving flexibility in case the objects/classes contained within the interface are polymorphic. For doing this, the late binding mechanism uses a Virtual table (vtable) that includes the memory address of each allocated class and its features. Every time the external program accesses an object/class using the COM interface it must go to the vtable first (if something changes it will be updated into the vtable), then look for the object to obtain the memory address and its features, which adds significant overhead on each iteration.
In contrast, early binding considers that every object is static in time and will be the same during the connection with the external software. For doing this, the vtable is eliminated and the index to each object/class is made by specifying the DispID memory address directly during the compilation phase. This eliminates the overhead generated by accessing vtables and the access to the COM interface objects is drastically faster.
To get connected to the COM interface using early binding, first check to see if your programming language supports early binding connection. Normally, all programming languages support late bindings but not necessarily early binding. However, in the next sections, this article presents an alternative for programming languages that do not support early binding. If your programming software language supports early binding the activation of this connection mechanism consists of adding a couple of code lines when establishing the connection with the COM server.
Not all programming languages support early binding for connecting to COM servers/ActiveX Controls. However, to cover this issue we have developed the Direct-connection Shared Library (DCSL), which is a standard DLL with stdcall functions that reproduces the same objects and functionality as the COM interface. This is not as flexible as the COM interface but it offers the same performance as achieved by early binding to the COM interface.
Examples
Early binding with Python
import win32com.client
from win32com.client import makepy
import sys
DSSObj = win32com.client.gencache.EnsureDispatch("OpenDSSEngine.DSS")
Early binding with VBA
Public DSSObj as OpenDSSEngine.DSS
…
Set DSSObj = New OpenDSSEngine.DSS
Early binding with Delphi
Var
DSSObject: IDSS; // DSS Interface
DSSText: IText;
DSSCircuit: ICircuit;
DSSSolution:ISolution;
DSSProgressfrm:IDSSProgress;
TRY
DSSObject := coDSS.Create; // Creates early binding
DSSObject.Start(0);
EXCEPT
On E:Exception Do Begin
MessageDlg('Did not Start.', mtConfirmation, [mbYes, mbNo], 0, mbYes);
Exit;
End;
END;
References
[1] D. Rogerson, Inside COM: Microsoft Press, 1997. (https://support.microsoft.com/en-us/kb/245115)
[2] https://sourceforge.net/p/electricdss/code/HEAD/tree/trunk/Version8/Distrib/Doc/COM%20Speed%20Comparison.pdf