Skip to content
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

PropertyValueConverter optimization: avoiding hashset lookup for primitive types #1959

Merged
merged 1 commit into from
Oct 2, 2023

Conversation

epeshk
Copy link
Contributor

@epeshk epeshk commented Sep 30, 2023

Optimization for PropertyValueConverter. Skips hashset lookup in SimpleScalarConversionPolicy for primitive types. The primitive types are Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, and Single.

It additionally handles sbyte, nint, nuint, but these types could be excluded with additional check without performance penalty. I'm not sure if sbyte was originally excluded on purpose.

type.IsPrimitive && type != typeof(sbyte) && type != typeof(IntPtr) && type != typeof(UIntPtr)
// or
type.IsPrimitive && Type.GetTypeCode(type) is not TypeCode.Object and not TypeCode.SByte

Before:

Method Mean Error StdDev Gen0 Allocated
IntProperties 268.5 ns 1.74 ns 1.63 ns 0.0296 496 B

After:

Method Mean Error StdDev Gen0 Allocated
IntProperties 195.1 ns 3.42 ns 3.20 ns 0.0296 496 B

@nblumhardt nblumhardt merged commit db7900d into serilog:dev Oct 2, 2023
@nblumhardt
Copy link
Member

Looks like a nice easy win 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants