-
Notifications
You must be signed in to change notification settings - Fork 102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid boxing of TState #241
Comments
Another cheap win in .net < 9.0: In .net 9+ there's a fast path in |
BTW: it would be nice to have similar fast logging when using Serilog natively!
|
Thanks for all the thoughts and suggestions @jods4! Anyone keen to investigate/PR any of these? 😎 |
@nblumhardt The first changes are so trivial... here you go: #242 I should have opened a different issue for having performant code generator that works with |
Nothing is ever trivial 🤣 Could have gone creative with |
Thanks again, keen to keep chipping away at these kinds of things, nice to see small gains add up over time :-) |
[LoggerMessage]
generate astruct
state, in an attempt to reduce allocations.For this to be meaningful, state should not be boxed, but it is in
AsLoggableValue
:https://github.com/serilog/serilog-extensions-logging/blob/dev/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs#L168-L174
Note that in the case of
[LoggerMessage]
aformatter
is always provided (to create the formatted string without boxing of its parameters), sostateObj
will never be null andstate
will not be boxed.For the curious mind, in
PrepareWrite()
the codeif (state is IEnumerable<..> structure)
does not result in boxing because JIT compiles each structTState
separately and statically optimizes these conditions and interfaces, as can be seen in this disassembly (call S.GetEnumerator
):https://sharplab.io/#v2:EYLgtghglgdgNAFxBAzmAPgAQEwEYCwAUJgAwAEmuAdAMID2ANgwKYDGCUdMKA3EaRVwAWPoSIoEAJwCu7MgGUyIQQGYAPAGlmATwBqEBtOYAFaJLWUScMnWAArNggB8TogG8iZLxRVkAkgCiMNJgzJIQCHTmWnoGRqZQ5pbWtg7sLmQA4swIQSFhEVEAFACUnt4ehN7VZAgAFpJ0AO5kMMwtAHJ0CH5gAA4soTAIzAAmAQAerMx9HFylotUAvkTlXoHBoeGRkqpU2bmbBTula2SVNd71jS1tnd29A8xDI+NTM3MwC2crhL/8vhwZBo5zOAG15HUIJI+gAZCDAKgAJWkwygoSoACkoAhsm1JFBWEUENo+sw6AAzIryEolAC6Z0wgNwADYKEIyABZNQAFScRR5ZAmJVBVRqUApZCKEzIUBQqk0On0hhMZgsuCsNnsjgyzDKYuqF0u3gpUWYEFYdSlADdoWQANbWvqymBkPVnS5G401SgATiKjr6VBiJUW3rIv0uke8vyWQA==
The text was updated successfully, but these errors were encountered: