Skip to content

Conversation

SimonCropp
Copy link
Contributor

@SimonCropp SimonCropp commented Jan 27, 2023

  • try add. leverages a custom path since the internal implementation combines existence check and the insert. so avoids one of the two lookups. and eventually less code when we burn only target runtimes
  • use the items constructor. less code and leverages an internal AddRange...
// It is likely that the passed-in dictionary is Dictionary<TKey,TValue>. When this is the case,
// avoid the enumerator allocation and overhead by looping through the entries array directly.
// We only do this when dictionary is Dictionary<TKey,TValue> and not a subclass, to maintain
// back-compat with subclasses that may have overridden the enumerator behavior.

it effectively does an array copy of the internal dictionary entries. this should speed up the hot path since the copy is done in Emit(LogEvent)

also avoids enumeration and the value lookup in the source

foreach (var key in _properties.Keys)
            properties.Add(key, _properties[key]);

@SimonCropp SimonCropp added this to the 3.0.0 milestone Jan 27, 2023
@SimonCropp
Copy link
Contributor Author

@sungam3r thoughts?

@SimonCropp SimonCropp changed the title leverage dictionary TryAdd and items constructor Leverage dictionary TryAdd and items constructor Jan 27, 2023
Copy link
Contributor

@sungam3r sungam3r left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just run dotnet test -c Release -f net7.0 --filter "FullyQualifiedName=Serilog.PerformanceTests.Harness.Pipeline" before and after changes to verify intents. Optimization is nothing without speedup confirmation.

@sungam3r
Copy link
Contributor

Also you may have to copy assets folder here
изображение
if you have issue with signing in BenchmarksDotNet.

@SimonCropp
Copy link
Contributor Author

@sungam3r thanks

profiling just the copy

before

Method Mean Error StdDev Gen0 Allocated
EmitLogEvent 382.7 ns 0.46 ns 0.38 ns 0.0601 504 B

after

Method Mean Error StdDev Gen0 Allocated
EmitLogEvent 128.5 ns 0.66 ns 0.52 ns 0.0601 504 B

@nblumhardt nblumhardt merged commit 1162daa into serilog:dev Jan 28, 2023
@SimonCropp SimonCropp deleted the leverage-dictionary-tryadd-and-constructor branch January 30, 2023 03:10
@nblumhardt nblumhardt mentioned this pull request Jun 19, 2023
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.

3 participants