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

   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 |
   ```


-- 
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