December 2nd, 2019

When should I use delayed-marshaling when creating an agile reference?

The Ro­Get­Agile­Reference function lets you specify whether you want the marshaling of the wrapped object to take place eagerly or lazily.

Flag Behavior
AGILE­REFERENCE_DEFAULT Eager marshaling
AGILE­REFERENCE_DELAYED­MARSHAL Lazy marshaling

Why should you choose one over the other?

It’s a question of whether you want to do a little work now in the hope of saving more work later.

If you marshal eagerly, then at the point that the agile reference is created, it also gathers the information necessary to create a proxy later. Later, if you use the agile reference from another thread, the agile reference uses that captured information to produce a proxy right then and there.

If you marshal lazily, then at the point that the agile reference is created, it merely remembers the COM context that the agile reference was created in, which is a relatively fast operation. Later, if you use the agile reference from another thread, the agile reference first goes back to the COM context to capture the information necessary to create the proxy, and then it returns to the requesting thread and generates the proxy from that information.

Action Eager marshal Lazy marshal
Create agile reference Create information for proxy Capture current COM context (fast)
Use from same context Use original object (fast) Use original object (fast)
Use from other context
(first time)
Create proxy Call into captured context
Create information for proxy
Return to original context
Create proxy
Use from other context
(second and subsequent times)
Create proxy Create proxy

Observe that if your intended operations are limited to the first two rows, then you’re better off doing lazy marshaling, since you avoid the Create information for proxy step. But the penalty for guessing wrong is that when you use the agile reference from another context for the first time, you need to do extra work to get back to the original context in order to perform the Create information for proxy step.

On the other hand, if you know that you’re going to explore rows three and four, then you should do eager marshaling, because it’s less expensive to create the information for the proxy up front than on demand. The penalty for guessing wrong is that you went through the Create information for proxy step unnecessarily.

Notice that in both cases, it’s okay if you guess wrong. The operations will all still succeed. It’ll just be less efficient.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

2 comments

Discussion is closed. Login to edit/delete existing comments.

  • Alex Cohn

    I believe that the dilemma with lazy marshalling is not only that it's somewhat less efficient to capture the necessary info by switching context, but also that this may happen at time when the caller is more time constrained. On the other hand, if you create the agile references in a tight loop, you may wish to pass the burden of preparing proxy info to another thread, even if it's likely you will have to...

    Read more
  • Ivan Genov

    I am sorry to be totally off-topic, but how can I report a bug to Microsoft? What I am seeing is that the clipping region of the HDC given by WM_ERASEBKGND/WM_PAINT sometimes does not exclude the child controls, even though the parent has the WS_CLIPCHILDREN style. This happens only when the parent is a child control itself.
    I like your blog a lot. Thanks for doing this and I am sorry for my intrusion again.

    Read more