Is it possible to have too many anonymous delegates?

Recently, I switched to a lot of diagnostic logging template in parts of my code that uses lambda expressions / anonymous delegates:

MyEventManager.LogVerbose( LogCategory.SomeCategory, () => String.Format(msg_string, GetParam1(), GetParam2(), GetParam3() );

Note that the second argument to LogVerbose is a lambda expression that evaluates to a string. The reason for this is that if slave logging is not actually allowed, LogVerbose should exit with as little work as possible to minimize performance impact. Building an error message line may in some cases take time or resources, and if the lambda expression is never evaluated, this performance penalty will not be incurred.

I am wondering if a type system with as many anonymous delegates as this can get clogged will have some unforeseen consequences for application performance or if there are any other strategies that I should consider.

+3
source share
3 answers

Everything should be fine. In particular, if your anonymous function does not capture anything, it is cached as a static field (because it could be). If you commit " this", you will create new delegate instances, but they are not expensive.

, , , , . , , , , , .

+2

, , , # , - , - .

+2

I have a solution that has received thousands of delegates of announcements, and it still works. Visual Studio is sometimes a little awkward, but that is because we have hundreds of projects or this or some other factor is unknown. Application performance does not seem to have suffered much (with a fairly small number of perfection tests).

+2
source

Source: https://habr.com/ru/post/1711500/


All Articles