<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Linq Expressions &#8211; Calculating with generics</title>
	<atom:link href="http://rogeralsing.com/2008/02/27/linq-expressions-calculating-with-generics/feed/" rel="self" type="application/rss+xml" />
	<link>http://rogeralsing.com/2008/02/27/linq-expressions-calculating-with-generics/</link>
	<description></description>
	<lastBuildDate>Sat, 28 Jan 2012 14:35:30 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Tech, food and spirit. / C# constraints on the operators (Reloaded)</title>
		<link>http://rogeralsing.com/2008/02/27/linq-expressions-calculating-with-generics/#comment-1644</link>
		<dc:creator><![CDATA[Tech, food and spirit. / C# constraints on the operators (Reloaded)]]></dc:creator>
		<pubDate>Sun, 30 May 2010 09:37:59 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=76#comment-1644</guid>
		<description><![CDATA[[...] found a solution that is close to my needs in Roger Alsing&#8217;s blog (here). Anyhow, until I&#8217;ve got to this approach, I realised that my requirements should change due [...]]]></description>
		<content:encoded><![CDATA[<p>[...] found a solution that is close to my needs in Roger Alsing&#8217;s blog (here). Anyhow, until I&#8217;ve got to this approach, I realised that my requirements should change due [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Keith J. Farmer</title>
		<link>http://rogeralsing.com/2008/02/27/linq-expressions-calculating-with-generics/#comment-1114</link>
		<dc:creator><![CDATA[Keith J. Farmer]]></dc:creator>
		<pubDate>Tue, 14 Apr 2009 07:17:11 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=76#comment-1114</guid>
		<description><![CDATA[As &quot;the guy&quot; who wrote the LCG-based article, I&#039;d suggest reading Pobar&#039;s article I reference early on, which outlines the invocation speedups in .NET 2.  I don&#039;t know the specifics of any experiments that have been run since, particularly on .NET 4&#039;s CLR, but I can pretty much guarantee that both delegates and expression tree manipulations would be things to count on receiving attention.  They are, after all, the basis for very important technologies (LINQ, DLR, etc).

It&#039;ll never be as fast as the bare code, but the runtime folks have motivation to do what they can to make it fast enough.]]></description>
		<content:encoded><![CDATA[<p>As &#8220;the guy&#8221; who wrote the LCG-based article, I&#8217;d suggest reading Pobar&#8217;s article I reference early on, which outlines the invocation speedups in .NET 2.  I don&#8217;t know the specifics of any experiments that have been run since, particularly on .NET 4&#8242;s CLR, but I can pretty much guarantee that both delegates and expression tree manipulations would be things to count on receiving attention.  They are, after all, the basis for very important technologies (LINQ, DLR, etc).</p>
<p>It&#8217;ll never be as fast as the bare code, but the runtime folks have motivation to do what they can to make it fast enough.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jimit</title>
		<link>http://rogeralsing.com/2008/02/27/linq-expressions-calculating-with-generics/#comment-115</link>
		<dc:creator><![CDATA[Jimit]]></dc:creator>
		<pubDate>Tue, 18 Mar 2008 17:55:31 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=76#comment-115</guid>
		<description><![CDATA[Hi,
My problem is rather simplistic compared to those being discussed above. However it is nonetheless frustrating. It illustrates how what I thought was a very simple matter in C# could prove to be not so simple in VB. Essentially I&#039;ve got a generic LINQ to SQL query that doesn&#039;t do anything but test for equality of one of the properties of a generic type (which is itself a generic type) to a supplied value.

The query is of the form:

Public Function GetbyID(ID as IdT) as EntityIT
Dim q = From e In ContextEntities Where e.ID.Equals(ID) Select e
Return DirectCast(q.Single(), EntityIT)
End Sub

Here &#039;e&#039; is a generic type with the following constraints {Entitybase(of IdT),EntityIT,New} and EntityIT is an interface that inherits IEntity(of IdT)

IEntity(of IdT) is an interface with one member relevant to this discussion, i.e. ID which is of type IdT.

In my test environment, I&#039;ve defined a concrete class called Customer which inherits from EntityBase(of Integer) (thus setting IdT to Integer) and implements ICustomer which inherits from IEntity(of Integer). EntityIT above becomes ICustomer.

I then attempt to retrieve a single customer entity by Id using the above method. The VB code above fails with the following error:

System.InvalidOperationException: {&quot;No coercion operator is defined between types &#039;Data.Tests.Customer&#039; and &#039;Domain.Entities.EntityBase(of IdT)&#039;.&quot;}

And yet, the C# equivalent of the above VB code runs perfectly:

public GetByID(ID as IdT) as EntityIT
{
            var q = from e in ContextEntities where e.PrimaryKey.Equals(id) select e;
            return q.Single() as EntityIT;
}

As you can see the two methods are exactly the same and yet one works and the other doesn&#039;t. Anyone have any ideas why this is the case, &#039;cause I&#039;m really stumped.]]></description>
		<content:encoded><![CDATA[<p>Hi,<br />
My problem is rather simplistic compared to those being discussed above. However it is nonetheless frustrating. It illustrates how what I thought was a very simple matter in C# could prove to be not so simple in VB. Essentially I&#8217;ve got a generic LINQ to SQL query that doesn&#8217;t do anything but test for equality of one of the properties of a generic type (which is itself a generic type) to a supplied value.</p>
<p>The query is of the form:</p>
<p>Public Function GetbyID(ID as IdT) as EntityIT<br />
Dim q = From e In ContextEntities Where e.ID.Equals(ID) Select e<br />
Return DirectCast(q.Single(), EntityIT)<br />
End Sub</p>
<p>Here &#8216;e&#8217; is a generic type with the following constraints {Entitybase(of IdT),EntityIT,New} and EntityIT is an interface that inherits IEntity(of IdT)</p>
<p>IEntity(of IdT) is an interface with one member relevant to this discussion, i.e. ID which is of type IdT.</p>
<p>In my test environment, I&#8217;ve defined a concrete class called Customer which inherits from EntityBase(of Integer) (thus setting IdT to Integer) and implements ICustomer which inherits from IEntity(of Integer). EntityIT above becomes ICustomer.</p>
<p>I then attempt to retrieve a single customer entity by Id using the above method. The VB code above fails with the following error:</p>
<p>System.InvalidOperationException: {&#8220;No coercion operator is defined between types &#8216;Data.Tests.Customer&#8217; and &#8216;Domain.Entities.EntityBase(of IdT)&#8217;.&#8221;}</p>
<p>And yet, the C# equivalent of the above VB code runs perfectly:</p>
<p>public GetByID(ID as IdT) as EntityIT<br />
{<br />
            var q = from e in ContextEntities where e.PrimaryKey.Equals(id) select e;<br />
            return q.Single() as EntityIT;<br />
}</p>
<p>As you can see the two methods are exactly the same and yet one works and the other doesn&#8217;t. Anyone have any ideas why this is the case, &#8217;cause I&#8217;m really stumped.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Piepgrass</title>
		<link>http://rogeralsing.com/2008/02/27/linq-expressions-calculating-with-generics/#comment-113</link>
		<dc:creator><![CDATA[David Piepgrass]]></dc:creator>
		<pubDate>Thu, 13 Mar 2008 17:15:40 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=76#comment-113</guid>
		<description><![CDATA[A factor of two? Hard to believe, since a delegate call is the same kind of thing (at the machine language level) as a virtual method call. In fact it may be slower (I heard a vague statement somewhere that LCG delegate calls were slower than &quot;normal&quot; delegate calls.) The guy who wrote this article...

http://www.codeproject.com/KB/cs/genericoperators.aspx

...used LCG, which is the same thing lambda.Compile() does, but he got a 7x slowdown compared to directly adding numbers.

Keep in mind when you measure the speed of a loop like

for (int i = 0; i &lt; 1000000000; i++)
   a = a + b;

Where a and b are integers/doubles, the CPU is not well utilized. It spends more time managing the loop than adding b to a. So if this loop is, for example, 4x (or 2x or 7x) faster than calling the delegate...

for (int i = 0; i &lt; 1000000000; i++)
   a += LCG_Number_Adder (a, b);

...actually the delegate is much more than 4x slower because the loop overhead is polluting your measurement.

At least using LCG directly has the advantage that it will work in .NET Framework 2.0; lambda.Compile() requires .NET Framework 3.5.]]></description>
		<content:encoded><![CDATA[<p>A factor of two? Hard to believe, since a delegate call is the same kind of thing (at the machine language level) as a virtual method call. In fact it may be slower (I heard a vague statement somewhere that LCG delegate calls were slower than &#8220;normal&#8221; delegate calls.) The guy who wrote this article&#8230;</p>
<p><a href="http://www.codeproject.com/KB/cs/genericoperators.aspx" rel="nofollow">http://www.codeproject.com/KB/cs/genericoperators.aspx</a></p>
<p>&#8230;used LCG, which is the same thing lambda.Compile() does, but he got a 7x slowdown compared to directly adding numbers.</p>
<p>Keep in mind when you measure the speed of a loop like</p>
<p>for (int i = 0; i &lt; 1000000000; i++)<br />
   a = a + b;</p>
<p>Where a and b are integers/doubles, the CPU is not well utilized. It spends more time managing the loop than adding b to a. So if this loop is, for example, 4x (or 2x or 7x) faster than calling the delegate&#8230;</p>
<p>for (int i = 0; i &lt; 1000000000; i++)<br />
   a += LCG_Number_Adder (a, b);</p>
<p>&#8230;actually the delegate is much more than 4x slower because the loop overhead is polluting your measurement.</p>
<p>At least using LCG directly has the advantage that it will work in .NET Framework 2.0; lambda.Compile() requires .NET Framework 3.5.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roger Alsing</title>
		<link>http://rogeralsing.com/2008/02/27/linq-expressions-calculating-with-generics/#comment-91</link>
		<dc:creator><![CDATA[Roger Alsing]]></dc:creator>
		<pubDate>Thu, 28 Feb 2008 12:13:15 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=76#comment-91</guid>
		<description><![CDATA[&gt;&gt;I’m amazed how similar our code is, actually!

Yeah, I&#039;ve just downloaded your code and checked it out.
But I see that yours is way more complete and advanced.
(btw. brilliant idea to throw exceptions from delegates on types that don&#039;t support math operations)

I also added a link to your project in my post, there are quite a bit of nifty stuff in there :-)]]></description>
		<content:encoded><![CDATA[<p>&gt;&gt;I’m amazed how similar our code is, actually!</p>
<p>Yeah, I&#8217;ve just downloaded your code and checked it out.<br />
But I see that yours is way more complete and advanced.<br />
(btw. brilliant idea to throw exceptions from delegates on types that don&#8217;t support math operations)</p>
<p>I also added a link to your project in my post, there are quite a bit of nifty stuff in there :-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marc Gravell</title>
		<link>http://rogeralsing.com/2008/02/27/linq-expressions-calculating-with-generics/#comment-90</link>
		<dc:creator><![CDATA[Marc Gravell]]></dc:creator>
		<pubDate>Thu, 28 Feb 2008 10:43:30 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=76#comment-90</guid>
		<description><![CDATA[&gt; Why aren’t you guys storing the compiled delegate
&gt; in static vars like I do in my version?

We are; in the MiscUtil code they are in the static Operator class; I&#039;m amazed how similar our code is, actually!

&gt; (Rüdiger Klaehn)
&gt; a factor of 10 or more
No; a factor of roughly 2 if used correctly.

&gt; (Jon Skeet)
&gt; Marc is currently working on a .NET 2.0 version - I’m &gt; not entirely sure how it’s going to work though…
it uses Delegate.CreateDelegate on the MethodInfo; it will use the necessary &quot;op_Foo&quot; methods if they exist (also had to provide the missing operators for int, float, etc), also with support for Nullable (lifted operators).
Fully working now for all primatives, all inside 2.0 ;-p
(but like yourself, I started with 3.5 Expressions)

Marc]]></description>
		<content:encoded><![CDATA[<p>&gt; Why aren’t you guys storing the compiled delegate<br />
&gt; in static vars like I do in my version?</p>
<p>We are; in the MiscUtil code they are in the static Operator class; I&#8217;m amazed how similar our code is, actually!</p>
<p>&gt; (Rüdiger Klaehn)<br />
&gt; a factor of 10 or more<br />
No; a factor of roughly 2 if used correctly.</p>
<p>&gt; (Jon Skeet)<br />
&gt; Marc is currently working on a .NET 2.0 version &#8211; I’m &gt; not entirely sure how it’s going to work though…<br />
it uses Delegate.CreateDelegate on the MethodInfo; it will use the necessary &#8220;op_Foo&#8221; methods if they exist (also had to provide the missing operators for int, float, etc), also with support for Nullable (lifted operators).<br />
Fully working now for all primatives, all inside 2.0 ;-p<br />
(but like yourself, I started with 3.5 Expressions)</p>
<p>Marc</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roger Alsing</title>
		<link>http://rogeralsing.com/2008/02/27/linq-expressions-calculating-with-generics/#comment-89</link>
		<dc:creator><![CDATA[Roger Alsing]]></dc:creator>
		<pubDate>Thu, 28 Feb 2008 10:26:00 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=76#comment-89</guid>
		<description><![CDATA[@Jon 

Why aren&#039;t you guys storing the compiled delegate in static vars like I do in my version?

Just initialize the delegates in the static ctor (or directly on the static fields)

and then simply call the delegates from your Add/Subtract/Div/Mul functions.

[edit]
Apparently you do, I didnt see that the code on the page was just sample code..]]></description>
		<content:encoded><![CDATA[<p>@Jon </p>
<p>Why aren&#8217;t you guys storing the compiled delegate in static vars like I do in my version?</p>
<p>Just initialize the delegates in the static ctor (or directly on the static fields)</p>
<p>and then simply call the delegates from your Add/Subtract/Div/Mul functions.</p>
<p>[edit]<br />
Apparently you do, I didnt see that the code on the page was just sample code..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jon Skeet</title>
		<link>http://rogeralsing.com/2008/02/27/linq-expressions-calculating-with-generics/#comment-88</link>
		<dc:creator><![CDATA[Jon Skeet]]></dc:creator>
		<pubDate>Thu, 28 Feb 2008 10:12:01 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=76#comment-88</guid>
		<description><![CDATA[Thanks to Marc Gravell, we&#039;ve got an implementation of generic maths using expression trees in MiscUtil:

http://pobox.com/~skeet/csharp/miscutil/usage/genericoperators.html

These are used in Push LINQ and my Range classes.

Marc is currently working on a .NET 2.0 version - I&#039;m not entirely sure how it&#039;s going to work though...]]></description>
		<content:encoded><![CDATA[<p>Thanks to Marc Gravell, we&#8217;ve got an implementation of generic maths using expression trees in MiscUtil:</p>
<p><a href="http://pobox.com/~skeet/csharp/miscutil/usage/genericoperators.html" rel="nofollow">http://pobox.com/~skeet/csharp/miscutil/usage/genericoperators.html</a></p>
<p>These are used in Push LINQ and my Range classes.</p>
<p>Marc is currently working on a .NET 2.0 version &#8211; I&#8217;m not entirely sure how it&#8217;s going to work though&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mats Helander</title>
		<link>http://rogeralsing.com/2008/02/27/linq-expressions-calculating-with-generics/#comment-82</link>
		<dc:creator><![CDATA[Mats Helander]]></dc:creator>
		<pubDate>Wed, 27 Feb 2008 12:58:26 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=76#comment-82</guid>
		<description><![CDATA[Rüdiger,

Doh, please disregard that brain fart :-P I shouldn&#039;t post on my vacation, I&#039;m probably still drunk from yesterday ! ;-)

I was thinking about how the CLR doesn&#039;t dynamically _re-JIT_ stuff depending on context (as far as I know) but that doesn&#039;t really bear on this case. Of course the CLR will JIT to native the first time hitherto uncompiled code is called. 

/Mats]]></description>
		<content:encoded><![CDATA[<p>Rüdiger,</p>
<p>Doh, please disregard that brain fart :-P I shouldn&#8217;t post on my vacation, I&#8217;m probably still drunk from yesterday ! ;-)</p>
<p>I was thinking about how the CLR doesn&#8217;t dynamically _re-JIT_ stuff depending on context (as far as I know) but that doesn&#8217;t really bear on this case. Of course the CLR will JIT to native the first time hitherto uncompiled code is called. </p>
<p>/Mats</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mats Helander</title>
		<link>http://rogeralsing.com/2008/02/27/linq-expressions-calculating-with-generics/#comment-81</link>
		<dc:creator><![CDATA[Mats Helander]]></dc:creator>
		<pubDate>Wed, 27 Feb 2008 12:51:00 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=76#comment-81</guid>
		<description><![CDATA[Rüdiger,

&quot;Since delegate invocations are never inlined by the CLR&quot;,

Do you mean inlined by csc? Because I thought the CLR didn&#039;t do JIT compilation (actually I thought it did until recently when someone told me it didn&#039;t :-P )

/Mats]]></description>
		<content:encoded><![CDATA[<p>Rüdiger,</p>
<p>&#8220;Since delegate invocations are never inlined by the CLR&#8221;,</p>
<p>Do you mean inlined by csc? Because I thought the CLR didn&#8217;t do JIT compilation (actually I thought it did until recently when someone told me it didn&#8217;t :-P )</p>
<p>/Mats</p>
]]></content:encoded>
	</item>
</channel>
</rss>

