0

Let us have this kind of code:

 Function<int,int> someFunc=(x)=>{
 //SomeCalc
 return y;
 }

than I want to use my function that way:

 int result;
 if(someFunc(k)!=0)
 {
 result=someFunc(k);
 }

The question is does the compiler caches the function result so it will be calculated only one time? Or it will be calculated two times? Tried to google the answer but with no luck.

And what about caching the closures?

1
  • I don't have a sure answer, but from where I see it the result shouldn't be cached as it may have changed between 2 calls. Also I don't think the compiler has anything to do with it as how I see it the execution of functions is done at runtime, not compilation... Commented Jun 17, 2013 at 14:16

3 Answers 3

2

The function will be executed twice. There is no way for the compiler/runtime to know if the result of the function will be the same the second time. You would need to cache the value yourself if that was the desired functionality of the function.

In this case though you're better off just storing the result of the function as a variable so that you can validate it and then use it, rather than having the function cache all results it generates.

The use of lambdas, closures, etc. doesn't change any of the above statements.

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

2 Comments

>In this case though you're better off just storing the result of the function as a variable.. Yes, it is obvious but I prefer to use more function-style code. So sad it won't cache itself :(
@gleb.kudr How will the language/runtime know that the result will be the same the second time? It doesn't. A large percentage of function won't be the same if called repeatedly. On top of that, it takes a lot of memory to cache all of the results of all functions. Perhaps the programmer doesn't want to consume all of that memory. If they do then they can explicitly take the time to cache values that are sufficiently expensive to generate. (It's not like it's particularly hard to do; it takes only a handful of lines of code.)
1

The C# compiler does no caching of the results here. I wouldn't expect the JIT to in general, either. You can do it yourself on a local basis very easily of course, just by using a local variable. You could also write a little memoization method to apply to the closure itself, so that the results were cached more globally - but you'd need to be careful about how you used that.

The C# compiler can cache the lambda expression itself in certain circumstances, but it's an implementation detail. In particular, I believe that any lambda expressions which don't capture any variables (including this) are cached by the Microsoft implementation.

Comments

0

The result is not cached. The calculation will be performed each time you call the delegate.

Comments

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.