VB COM Performance Tip (part 2): Bind it Early

July 23, 2004

Early versus Late binding is a topic that has just about been beaten to death. There are countless articles all over the Internet dealing with the subject and describing it in-depth. I’m going to sum them all up in four paragraphs. For the sake of brevity, I must assume you know what a COM object is, and what a COM interface is. If you don’t, try this reference on for size.

What is Early Binding?
Early binding is the term used to describe what happens when the compiler KNOWS all about the interface of the COM object you are attempting to use, and compiles this information into the executable/dll. Since all this information is already known, it is faster to load and call objects that are early bound.

What is Late Binding?
Late binding is the term used to describe when not much is known about the object or its interfaces at compile time. Detection and calling of the object’s properties and methods therefore occurs through the Idispatch interface at the time the object is created or a method on it is called. Late binding is slow because every single method or property call involves multiple steps to discover the id of the method, what parameters it takes, perform type checking on those parameters, and then finally calling the method at the proper address. All of this happens every time the method is called, no matter how many times the same method is called. Furthermore, if the wrong parameter type is passed, an error is raised at run time, instead of at design time where such errors should be detected.

When to use Early Binding
As long as you have the ability to pre-define all the properties and methods of an interface that an application will need to call on a COM object, there is no reason not to use Early Binding. This also holds true for objects that are “implementing” additional interfaces. In almost all circumstances, the application that will be performing the instantiation of the object can dictate a bare minimum of properties and methods that it will need to call on an object. At the very least, define a generic interface that the application expects and implement this interface in the objects it will use.

When to use Late Binding
There are very few circumstances when late binding is useful or beneficial. Here is a list of those circumstances:

  • When using a scripting language that does not support Early Binding (e.g., ASP, Windows Scripting Host, etc.).
  • When dealing with an application that dynamically browses objects, interfaces and methods (i.e., OLE2View)
  • When references to objects stored in a control. For instance, a list-box control might have an object or variant property that contains a reference to some object that is associated with that list item. This is risky, however, and should be avoided in favor of keeping an ID or key in the control instead and using this to lookup the object in an array, collection, database, etc.

Those are the only examples that must make use of Late Binding. All other applications should define a generic interface that can be used. This increases performance and enforces type checking.

Overall, using Early Binding makes the most sense. If you have an example that you believe is a good use for Late Binding, I’d like to hear more about it.

Pass it on!

Facebooktwittermail