Shazwazza commented on issue #1062:
URL: https://github.com/apache/lucenenet/issues/1062#issuecomment-2525247120

   Non static lambdas are just object instances and will allocate. NET does
   try to optimize this in many cases such as ConcurrentDictionary overloads
   like GetOrAdd allowing passing parameters in to the value builder to be
   used in the lambda as a parameters instead of a capture/closure.
   
   Anytime you can declare a lambda as static you absolutely should. This
   prevents allocations but means it cannot be a closure. See
   
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-9.0/static-anonymous-functions
   
   On Sat, Dec 7, 2024, 9:10 a.m. Paul Irwin ***@***.***> wrote:
   
   > After running some benchmarks, there is a slight performance and
   > allocation hit for using lambdas over "AnonymousClasses" that implement a
   > functional interface, when the lambda captures, and when this is done
   > one-shot and not many times. There is basically no difference if they do
   > not capture, or when done many times.
   >
   > The one-shot performance impact is worse if the lambda captures a variable
   > instead of a field, so the impact can be mitigated by storing it in a field
   > (or wrapping it in your own equivalent of an "anonymous class") if this is
   > a concern. It also allocates slightly more bytes.
   >
   > I could go either way on this. The performance difference basically
   > disappears at high enough usage, and I'd imagine Dynamic PGO would help
   > this further. I don't know if 9 nanoseconds and 64 bytes for a one-shot
   > call is enough to warrant harming the UX by making you create a custom
   > class instead of passing a lambda to these APIs. I'm leaning towards still
   > wanting this UX improvement.
   >
   > Microsoft, if you're listening, please improve the performance and reduce
   > the allocation of lambdas so that this is not a concern 😄
   >
   > 1 iteration:
   >
   > | Method                                 | Iterations | Mean        | 
Error     | StdDev    | Gen0   | Allocated |
   > |--------------------------------------- |----------- 
|------------:|----------:|----------:|-------:|----------:|
   > | PredicateLambdaNoCapture               | 1          |   0.9096 ns | 
0.0011 ns | 0.0009 ns |      - |         - |
   > | PredicateLambdaCaptureVariable         | 1          | 238.5972 ns | 
0.2876 ns | 0.2550 ns | 0.0253 |     160 B |
   > | PredicateLambdaCaptureField            | 1          |   9.0264 ns | 
0.0139 ns | 0.0123 ns | 0.0102 |      64 B |
   > | PredicateAnonymousClassNoCapture       | 1          |   0.0000 ns | 
0.0000 ns | 0.0000 ns |      - |         - |
   > | PredicateAnonymousClassCaptureVariable | 1          | 229.3647 ns | 
0.1341 ns | 0.1047 ns | 0.0153 |      96 B |
   > | PredicateAnonymousClassCaptureField    | 1          |   2.6220 ns | 
0.0054 ns | 0.0050 ns | 0.0038 |      24 B |
   >
   > 100k iterations:
   >
   > | Method                                 | Iterations | Mean     | Error   
 | StdDev   | Allocated |
   > |--------------------------------------- |----------- 
|---------:|---------:|---------:|----------:|
   > | PredicateLambdaNoCapture               | 100000     | 31.36 us | 0.030 
us | 0.027 us |         - |
   > | PredicateLambdaCaptureVariable         | 100000     | 63.06 us | 0.046 
us | 0.043 us |     160 B |
   > | PredicateLambdaCaptureField            | 100000     | 62.70 us | 0.034 
us | 0.032 us |      64 B |
   > | PredicateAnonymousClassNoCapture       | 100000     | 31.33 us | 0.015 
us | 0.012 us |         - |
   > | PredicateAnonymousClassCaptureVariable | 100000     | 63.03 us | 0.041 
us | 0.036 us |      96 B |
   > | PredicateAnonymousClassCaptureField    | 100000     | 62.89 us | 0.348 
us | 0.308 us |      24 B |
   >
   > —
   > Reply to this email directly, view it on GitHub
   > <https://github.com/apache/lucenenet/issues/1062#issuecomment-2525228669>,
   > or unsubscribe
   > 
<https://github.com/notifications/unsubscribe-auth/AANJOXLZSPM6SPITEDTKYWD2EMMX3AVCNFSM6AAAAABTFCDAWWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDKMRVGIZDQNRWHE>
   > .
   > You are receiving this because you are subscribed to this thread.Message
   > ID: ***@***.***>
   >
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@lucenenet.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to