4

As I know Expression trees is immutable so why compiler didn't use same object reference for a static expression, like string literals?

To clarify the question please see the example:

static void Main(string[] args)
{
    Test(p => true);//2637164
    Test(p => true);//3888474
    Test("true");//-292522067
    Test("true");//-292522067
    Console.ReadKey();
}

public static void Test(Expression<Func<string,bool>> exp)
{
    Console.WriteLine(exp.GetHashCode());
}

public static void Test(string str)
{
    Console.WriteLine(str.GetHashCode());
}
23
  • 1
    @PeterDuniho: Well it's not that miraculous. That was one of my features. The spec says that the compiler is permitted but not required to make identical lambdas reference equal. As you surmise: that feature costs effort and the compiler team has better things to do than to write "optimizations" for situations that (1) never happen, and (2) can be optimized "by hand" easily enough if the developer so desires. Commented Nov 13, 2017 at 6:41
  • 2
    @PeterDuniho: What I meant was: it is not really miraculous that the compiler developer answers the question. C# compiler developers answer questions all the time on SO. You said "if by some miracle the author of that feature answers" and then the author of that feature wrote an answer, so really, not a miracle. Commented Nov 13, 2017 at 6:47
  • 1
    @EricLippert: ah, yes...I see. I did use the word "miracle" there, didn't I? In my defense, my understanding is that the compiler team has a number of members; but you're the only person from that team I've seen regularly answer questions (I've only seen others on rare occasions), and statistically speaking I figured the odds were low that you would have been the person who did that feature. So, it seems the miracle was granted! Commented Nov 13, 2017 at 6:49
  • 4
    @PeterDuniho: You make a convincing point. I hadn't realized his posting rate had dropped off so dramatically compared to the early days. I'll give him crap about it next time I see him. :-) Commented Nov 13, 2017 at 7:08
  • 2
    @RezaArabQaeni: And if you have an actual problem that you need help with, then why did you phrase it as a question about my implementation choices twelve years ago? Ask about the problem you actually have if you want help with that problem! Knowing the rationale for my choices in 2005 doesn't help you solve your real problem today. Commented Nov 13, 2017 at 17:37

2 Answers 2

8

As I know Expression trees is immutable so why compiler didn't use same object reference for a static expression, like string literals?

The spec says that the compiler is permitted but not required to intern identical lambdas.

String literals are interned by the runtime for free; there's no cost to the compiler developer to do it.

The reason why I didn't intern expression trees is because every day we spent working on pointless unnecessary "optimizations" for unrealistic scenarios that actually save no valuable resource is a day that Visual Studio would have slipped its schedule. We spent that time on actual optimizations.

Sign up to request clarification or add additional context in comments.

Comments

0

Blindly guessing of what your actual issue is: instead of reference equality, use the answer from this question as the IEqualityComparer for your dictionary. It is obviously not as fast as Object.ReferenceEquals, but is definately faster than compiling the expression tree I would imagine.

5 Comments

i use this approach but not exactly, because if key of dictionary be a Expression may we keep a useless object in memory and GC can't clean taht. So i use a unique HashCode method that translate Expression to a string one to one.
How do you do the tostring on the constants? Especially for not serializable objects?
In my case expressions was property descriptor, so I get expression and translate that to entity full name plus property name.
You mean it is not a generic solution, just for your usecase, right?
Yes, the link that you introduced also is not generic solution it's just for property descriptor Expressions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.