<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Fast Late-Bound Invocation with Expression Trees</title>
	<atom:link href="http://kohari.org/2009/03/06/fast-late-bound-invocation-with-expression-trees/feed/" rel="self" type="application/rss+xml" />
	<link>http://kohari.org/2009/03/06/fast-late-bound-invocation-with-expression-trees/</link>
	<description>Rambling and occasional wisdom from Nate Kohari</description>
	<lastBuildDate>Tue, 09 Mar 2010 12:47:18 -0500</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Ameesh</title>
		<link>http://kohari.org/2009/03/06/fast-late-bound-invocation-with-expression-trees/comment-page-1/#comment-2779</link>
		<dc:creator>Ameesh</dc:creator>
		<pubDate>Mon, 01 Mar 2010 18:43:37 +0000</pubDate>
		<guid isPermaLink="false">http://kohari.org/2009/03/06/fast-late-bound-invocation-with-expression-trees/#comment-2779</guid>
		<description>Great work !

However while I can get this to work with most method calls, it doesnt seem to work with methods that dont have a return value. (void methods).. ?</description>
		<content:encoded><![CDATA[<p>Great work !</p>
<p>However while I can get this to work with most method calls, it doesnt seem to work with methods that dont have a return value. (void methods).. ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AGS SOE’s without the SOAP &#8211; v2 &#171; MapWrecker 2.0</title>
		<link>http://kohari.org/2009/03/06/fast-late-bound-invocation-with-expression-trees/comment-page-1/#comment-2745</link>
		<dc:creator>AGS SOE’s without the SOAP &#8211; v2 &#171; MapWrecker 2.0</dc:creator>
		<pubDate>Thu, 14 Jan 2010 09:18:57 +0000</pubDate>
		<guid isPermaLink="false">http://kohari.org/2009/03/06/fast-late-bound-invocation-with-expression-trees/#comment-2745</guid>
		<description>[...] however, is hardly the fastest way to dynamically invoke methods. Nate Kohari shows how to use LINQ expressions to create fast invokers for late-bound methods. Rick Strahl [...]</description>
		<content:encoded><![CDATA[<p>[...] however, is hardly the fastest way to dynamically invoke methods. Nate Kohari shows how to use LINQ expressions to create fast invokers for late-bound methods. Rick Strahl [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JP</title>
		<link>http://kohari.org/2009/03/06/fast-late-bound-invocation-with-expression-trees/comment-page-1/#comment-282</link>
		<dc:creator>JP</dc:creator>
		<pubDate>Sat, 20 Jun 2009 09:59:53 +0000</pubDate>
		<guid isPermaLink="false">http://kohari.org/2009/03/06/fast-late-bound-invocation-with-expression-trees/#comment-282</guid>
		<description>I think you&#039;re idea is great. I&#039;ve faced the same problem in the past and I completely missed the possibility of using lambda expressions.

Although I have no doubts that your solution works and is fast enough, I see some room for improvement.

First thing, you can add the &#039;params&#039; keyword to the LateBoundMethod delegate like that:
public delegate object LateBoundMethod(object target, params object[] arguments);

With this, it&#039;s not necessary to explicitly create an array when calling the delegate. For example, you could write:
bool result = (bool) callback(foo, &quot;this&quot;);
instead of:
bool result = (bool) callback(foo, new[] { &quot;this&quot; });

My second point is more relevant. I was worried about argument validation. For example, what would happen if someone calls a LateBoundMethod passing an incorrect number of parameters? Or if arguments = null?

Better than simply asking, I tested it. The asnwers were IndexOutOfRangeException and NullReferenceException, respectively. Both exceptions are somewhat ugly for a grownup component to throw. IMHO, the more &quot;correct&quot; exceptions would be TargetParameterCountException and ArgumentNullException.

I went ahead and tried to implement this change. I think I&#039;ve reached a reasonable solution. Of course, argument validation will slow down the execution a bit for both generation and calling. Anyway, I believe any mature code should do this.

Nate, if you don&#039;t mind, I&#039;ll post my solution on my blog. Naturally, with a very big reference to the original author. In the meantime, if anyone is interested in seeing my code, send me an email (jpbochi at gmail dot com).</description>
		<content:encoded><![CDATA[<p>I think you&#8217;re idea is great. I&#8217;ve faced the same problem in the past and I completely missed the possibility of using lambda expressions.</p>
<p>Although I have no doubts that your solution works and is fast enough, I see some room for improvement.</p>
<p>First thing, you can add the &#8216;params&#8217; keyword to the LateBoundMethod delegate like that:<br />
public delegate object LateBoundMethod(object target, params object[] arguments);</p>
<p>With this, it&#8217;s not necessary to explicitly create an array when calling the delegate. For example, you could write:<br />
bool result = (bool) callback(foo, &#8220;this&#8221;);<br />
instead of:<br />
bool result = (bool) callback(foo, new[] { &#8220;this&#8221; });</p>
<p>My second point is more relevant. I was worried about argument validation. For example, what would happen if someone calls a LateBoundMethod passing an incorrect number of parameters? Or if arguments = null?</p>
<p>Better than simply asking, I tested it. The asnwers were IndexOutOfRangeException and NullReferenceException, respectively. Both exceptions are somewhat ugly for a grownup component to throw. IMHO, the more &#8220;correct&#8221; exceptions would be TargetParameterCountException and ArgumentNullException.</p>
<p>I went ahead and tried to implement this change. I think I&#8217;ve reached a reasonable solution. Of course, argument validation will slow down the execution a bit for both generation and calling. Anyway, I believe any mature code should do this.</p>
<p>Nate, if you don&#8217;t mind, I&#8217;ll post my solution on my blog. Naturally, with a very big reference to the original author. In the meantime, if anyone is interested in seeing my code, send me an email (jpbochi at gmail dot com).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: More on Late-Bound Invocations with Expression Trees - Jimmy Bogard -</title>
		<link>http://kohari.org/2009/03/06/fast-late-bound-invocation-with-expression-trees/comment-page-1/#comment-281</link>
		<dc:creator>More on Late-Bound Invocations with Expression Trees - Jimmy Bogard -</dc:creator>
		<pubDate>Thu, 18 Jun 2009 01:04:50 +0000</pubDate>
		<guid isPermaLink="false">http://kohari.org/2009/03/06/fast-late-bound-invocation-with-expression-trees/#comment-281</guid>
		<description>[...] it all looks quite ugly and hard to maintain.&#160; Until I stumbled upon Nate Kohari’s post on late-bound invocations via expression trees, I was avoiding that piece.&#160; The basic idea is that expression trees, when compiled, are able [...]</description>
		<content:encoded><![CDATA[<p>[...] it all looks quite ugly and hard to maintain.&#160; Until I stumbled upon Nate Kohari’s post on late-bound invocations via expression trees, I was avoiding that piece.&#160; The basic idea is that expression trees, when compiled, are able [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nate Kohari</title>
		<link>http://kohari.org/2009/03/06/fast-late-bound-invocation-with-expression-trees/comment-page-1/#comment-280</link>
		<dc:creator>Nate Kohari</dc:creator>
		<pubDate>Wed, 11 Mar 2009 13:16:57 +0000</pubDate>
		<guid isPermaLink="false">http://kohari.org/2009/03/06/fast-late-bound-invocation-with-expression-trees/#comment-280</guid>
		<description>@Klaus: That&#039;s true, with a language that supports dynamic dispatch, this would be simpler. However, the whole concept is to *reduce* the cost of late-bound invocation. Using Ruby, you pay a much higher price on *every* invocation, since the compiler can&#039;t determine the method bindings at compile-time.

This isn&#039;t to say Ruby isn&#039;t a great language, but it&#039;s also not the &quot;next step&quot; in programming -- it&#039;s just a different way of thinking that happens to have become more popular in recent years.</description>
		<content:encoded><![CDATA[<p>@Klaus: That&#8217;s true, with a language that supports dynamic dispatch, this would be simpler. However, the whole concept is to *reduce* the cost of late-bound invocation. Using Ruby, you pay a much higher price on *every* invocation, since the compiler can&#8217;t determine the method bindings at compile-time.</p>
<p>This isn&#8217;t to say Ruby isn&#8217;t a great language, but it&#8217;s also not the &#8220;next step&#8221; in programming &#8212; it&#8217;s just a different way of thinking that happens to have become more popular in recent years.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Klaus Hebsgaard</title>
		<link>http://kohari.org/2009/03/06/fast-late-bound-invocation-with-expression-trees/comment-page-1/#comment-279</link>
		<dc:creator>Klaus Hebsgaard</dc:creator>
		<pubDate>Wed, 11 Mar 2009 09:13:36 +0000</pubDate>
		<guid isPermaLink="false">http://kohari.org/2009/03/06/fast-late-bound-invocation-with-expression-trees/#comment-279</guid>
		<description>I remember when I coded C++ how memory management was a big issue, and I found it very exciting, and very nerdy.
In C++ people would do big (I mean BIG) work to make automatic memory management instead of running new() and delete() all the time.
And I remember thinking when I started coding C# that all this garbage collection was just for people who didn&#039;t understand to code.
However I dit grow wiser and now understand how C# raised the bar (abstraction) for me so I didn&#039;t have to do technically challenging and nerdy stuff, but simply use the right tool (.Net) for the right job.
I see the same thing here, what you are trying to do would be very easy and simple to do in a dynamic language like Ruby...</description>
		<content:encoded><![CDATA[<p>I remember when I coded C++ how memory management was a big issue, and I found it very exciting, and very nerdy.<br />
In C++ people would do big (I mean BIG) work to make automatic memory management instead of running new() and delete() all the time.<br />
And I remember thinking when I started coding C# that all this garbage collection was just for people who didn&#8217;t understand to code.<br />
However I dit grow wiser and now understand how C# raised the bar (abstraction) for me so I didn&#8217;t have to do technically challenging and nerdy stuff, but simply use the right tool (.Net) for the right job.<br />
I see the same thing here, what you are trying to do would be very easy and simple to do in a dynamic language like Ruby&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rick Strahl</title>
		<link>http://kohari.org/2009/03/06/fast-late-bound-invocation-with-expression-trees/comment-page-1/#comment-284</link>
		<dc:creator>Rick Strahl</dc:creator>
		<pubDate>Tue, 10 Mar 2009 22:05:24 +0000</pubDate>
		<guid isPermaLink="false">http://kohari.org/2009/03/06/fast-late-bound-invocation-with-expression-trees/#comment-284</guid>
		<description>Nate - after running some perf tests on this I&#039;m not sure if it&#039;s worth it to do this, unless you make a boat load of calls against your dynamic delegate (and this might make good sense in nInject).

The compilation of the delegate is wickedly expensive - in my tests you&#039;d have to make somewhere around 2500 Reflection calls to make up for the compilation overhead. Perf is super fast once you have the compiled delegate, but it&#039;s heavy price to pay up front.

Here&#039;s what I played with:
http://www.west-wind.com/weblog/posts/653034.aspx</description>
		<content:encoded><![CDATA[<p>Nate &#8211; after running some perf tests on this I&#8217;m not sure if it&#8217;s worth it to do this, unless you make a boat load of calls against your dynamic delegate (and this might make good sense in nInject).</p>
<p>The compilation of the delegate is wickedly expensive &#8211; in my tests you&#8217;d have to make somewhere around 2500 Reflection calls to make up for the compilation overhead. Perf is super fast once you have the compiled delegate, but it&#8217;s heavy price to pay up front.</p>
<p>Here&#8217;s what I played with:<br />
<a href="http://www.west-wind.com/weblog/posts/653034.aspx" rel="nofollow">http://www.west-wind.com/weblog/posts/653034.aspx</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Simone Busoli</title>
		<link>http://kohari.org/2009/03/06/fast-late-bound-invocation-with-expression-trees/comment-page-1/#comment-278</link>
		<dc:creator>Simone Busoli</dc:creator>
		<pubDate>Mon, 09 Mar 2009 17:26:00 +0000</pubDate>
		<guid isPermaLink="false">http://kohari.org/2009/03/06/fast-late-bound-invocation-with-expression-trees/#comment-278</guid>
		<description>I won&#039;t go over your reply. Let me just say that you look like a skilled verbalist, and your replies to my comments were mostly out of context.
I know what you&#039;re talking about, it&#039;s just very badly exposed.</description>
		<content:encoded><![CDATA[<p>I won&#8217;t go over your reply. Let me just say that you look like a skilled verbalist, and your replies to my comments were mostly out of context.<br />
I know what you&#8217;re talking about, it&#8217;s just very badly exposed.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nate Kohari</title>
		<link>http://kohari.org/2009/03/06/fast-late-bound-invocation-with-expression-trees/comment-page-1/#comment-277</link>
		<dc:creator>Nate Kohari</dc:creator>
		<pubDate>Mon, 09 Mar 2009 16:56:23 +0000</pubDate>
		<guid isPermaLink="false">http://kohari.org/2009/03/06/fast-late-bound-invocation-with-expression-trees/#comment-277</guid>
		<description>Here are my points:

1. Delegate.CreateDelegate() will create a new delegate at runtime, but to invoke it you have to use DynamicInvoke(), which is slow.

2. In order to avoid the cost of DynamicInvoke(), you have to call the Invoke() method on a delegate, which requires that you know (at compile time) the method&#039;s signature.

3. The expression tree technique allows you to create a weak-typed open delegate, which casts the arguments passed in to the correct types, and invokes the original method.

4. You can call the Invoke() method of the delegate generated by the expression tree technique without knowing (at compile time) the types of arguments you&#039;re passing in.

I don&#039;t understand why you think that I&#039;m incorrect, but I&#039;m getting the impression that you either missed the point or that you just want to be argumentative. If it&#039;s the former, I&#039;ll ask you to re-read the original post; if it&#039;s the latter, I&#039;ll ask you to go elsewhere.</description>
		<content:encoded><![CDATA[<p>Here are my points:</p>
<p>1. Delegate.CreateDelegate() will create a new delegate at runtime, but to invoke it you have to use DynamicInvoke(), which is slow.</p>
<p>2. In order to avoid the cost of DynamicInvoke(), you have to call the Invoke() method on a delegate, which requires that you know (at compile time) the method&#8217;s signature.</p>
<p>3. The expression tree technique allows you to create a weak-typed open delegate, which casts the arguments passed in to the correct types, and invokes the original method.</p>
<p>4. You can call the Invoke() method of the delegate generated by the expression tree technique without knowing (at compile time) the types of arguments you&#8217;re passing in.</p>
<p>I don&#8217;t understand why you think that I&#8217;m incorrect, but I&#8217;m getting the impression that you either missed the point or that you just want to be argumentative. If it&#8217;s the former, I&#8217;ll ask you to re-read the original post; if it&#8217;s the latter, I&#8217;ll ask you to go elsewhere.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Simone Busoli</title>
		<link>http://kohari.org/2009/03/06/fast-late-bound-invocation-with-expression-trees/comment-page-1/#comment-283</link>
		<dc:creator>Simone Busoli</dc:creator>
		<pubDate>Mon, 09 Mar 2009 16:37:06 +0000</pubDate>
		<guid isPermaLink="false">http://kohari.org/2009/03/06/fast-late-bound-invocation-with-expression-trees/#comment-283</guid>
		<description>I don&#039;t know either, you just keep saying the following

&lt;i&gt;
You still need to have objects of the proper types in order to pass them to the strong-typed Invoke() method of a delegate. The expression trees allow you to interface with the delegate with only a collection of weak-typed System.Objects
&lt;/i&gt;

Which is wrong. What&#039;s right, instead, is that the solution based on expressions is faster than other solutions.</description>
		<content:encoded><![CDATA[<p>I don&#8217;t know either, you just keep saying the following</p>
<p><i><br />
You still need to have objects of the proper types in order to pass them to the strong-typed Invoke() method of a delegate. The expression trees allow you to interface with the delegate with only a collection of weak-typed System.Objects<br />
</i></p>
<p>Which is wrong. What&#8217;s right, instead, is that the solution based on expressions is faster than other solutions.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
