{"id":103171,"date":"2019-12-02T07:00:00","date_gmt":"2019-12-02T15:00:00","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/oldnewthing\/?p=103171"},"modified":"2019-12-01T18:45:24","modified_gmt":"2019-12-02T02:45:24","slug":"20191202-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20191202-00\/?p=103171","title":{"rendered":"When should I use delayed-marshaling when creating an agile reference?"},"content":{"rendered":"<p>The <code>Ro\u00adGet\u00adAgile\u00adReference<\/code> function lets you specify whether you want the marshaling of the wrapped object to take place eagerly or lazily.<\/p>\n<table class=\"cp3\" style=\"border-collapse: collapse;\" border=\"1\" cellspacing=\"3\" cellpadding=\"3\">\n<tbody>\n<tr>\n<th>Flag<\/th>\n<th>Behavior<\/th>\n<\/tr>\n<tr>\n<td><code>AGILE\u00adREFERENCE_<\/code><code>DEFAULT<\/code><\/td>\n<td>Eager marshaling<\/td>\n<\/tr>\n<tr>\n<td><code>AGILE\u00adREFERENCE_<\/code><code>DELAYED\u00adMARSHAL<\/code><\/td>\n<td>Lazy marshaling<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Why should you choose one over the other?<\/p>\n<p>It&#8217;s a question of whether you want to do a little work now in the hope of saving more work later.<\/p>\n<p>If you marshal eagerly, then at the point that the agile reference is created, it also gathers <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20191129-00\/?p=103162\">the information necessary to create a proxy later<\/a>. 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.<\/p>\n<p>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.<\/p>\n<table class=\"cp3\" style=\"border-collapse: collapse;\" border=\"1\" cellspacing=\"3\" cellpadding=\"3\">\n<tbody>\n<tr>\n<th>Action<\/th>\n<th>Eager marshal<\/th>\n<th>Lazy marshal<\/th>\n<\/tr>\n<tr>\n<td>Create agile reference<\/td>\n<td>Create information for proxy<\/td>\n<td>Capture current COM context (fast)<\/td>\n<\/tr>\n<tr>\n<td>Use from same context<\/td>\n<td>Use original object (fast)<\/td>\n<td>Use original object (fast)<\/td>\n<\/tr>\n<tr>\n<td>Use from other context<br \/>\n(first time)<\/td>\n<td valign=\"bottom\">Create proxy<\/td>\n<td>Call into captured context<br \/>\nCreate information for proxy<br \/>\nReturn to original context<br \/>\nCreate proxy<\/td>\n<\/tr>\n<tr>\n<td>Use from other context<br \/>\n(second and subsequent times)<\/td>\n<td>Create proxy<\/td>\n<td>Create proxy<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Observe that if your intended operations are limited to the first two rows, then you&#8217;re better off doing lazy marshaling, since you avoid the <i>Create information for proxy<\/i> 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 <i>Create information for proxy<\/i> step.<\/p>\n<p>On the other hand, if you know that you&#8217;re going to explore rows three and four, then you should do eager marshaling, because it&#8217;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 <i>Create information for proxy<\/i> step unnecessarily.<\/p>\n<p>Notice that in both cases, it&#8217;s okay if you guess wrong. The operations will all still succeed. It&#8217;ll just be less efficient.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s a balance between doing more work now in anticipation of saving work later.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-103171","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>It&#8217;s a balance between doing more work now in anticipation of saving work later.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=103171"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103171\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=103171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=103171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=103171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}