Skip to content

Commit a2cac59

Browse files
authored
using virtual fuction instead of reflection (#11513)
###Part of #11160 Building on top of Eric's IPC pr - his work allowed for other bottlenecks to reveal themselves. ### Context ![read_from_stream_reflection](https://github.com/user-attachments/assets/aa0bd4b9-cb39-4351-9c51-e4bbbfa01c37) ![read_from_stream_virtual](https://github.com/user-attachments/assets/7fe81c55-1aaf-4e79-97a5-648c6a68cfe5) Currently, the ReadFromStream function uses ``` ArgsReaderDelegate readerMethod = (ArgsReaderDelegate)CreateDelegateRobust(typeof(ArgsReaderDelegate), _buildEvent, methodInfo); ``` for a large portion of it's packet deserialization. This is large enough to be seen in the performance profiler as shown above more than 3% of CPU an it's on a critical path. When I checked, the function was already virtual so the change is minimal. Unfortunately I had to make an allowance to the task host since it wasn't compatible. ### Changes Made Exposed a convenience public endpoint for the CreateFromStream function, that calls the virtual method Create from stream. Used this endpoint instead of delegate creation. ### Testing As long as nothing breaks, I consider that a win. I did local profiling.
2 parents a835199 + 8f5fe7f commit a2cac59

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/Framework/BuildEventArgs.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;

src/Shared/LogMessagePacketBase.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
@@ -270,11 +270,12 @@ internal abstract class LogMessagePacketBase : INodePacket
270270
/// </summary>
271271
private static readonly int s_defaultPacketVersion = (Environment.Version.Major * 10) + Environment.Version.Minor;
272272

273+
#if TASKHOST
273274
/// <summary>
274275
/// Dictionary of methods used to read BuildEventArgs.
275276
/// </summary>
276277
private static Dictionary<LoggingEventType, MethodInfo> s_readMethodCache = new Dictionary<LoggingEventType, MethodInfo>();
277-
278+
#endif
278279
/// <summary>
279280
/// Dictionary of methods used to write BuildEventArgs.
280281
/// </summary>
@@ -478,6 +479,8 @@ internal void ReadFromStream(ITranslator translator)
478479

479480
if (eventCanSerializeItself)
480481
{
482+
483+
#if TASKHOST
481484
MethodInfo methodInfo = null;
482485
lock (s_readMethodCache)
483486
{
@@ -492,6 +495,11 @@ internal void ReadFromStream(ITranslator translator)
492495
ArgsReaderDelegate readerMethod = (ArgsReaderDelegate)CreateDelegateRobust(typeof(ArgsReaderDelegate), _buildEvent, methodInfo);
493496

494497
readerMethod(translator.Reader, packetVersion);
498+
499+
#else
500+
_buildEvent.CreateFromStream(translator.Reader, packetVersion);
501+
#endif
502+
495503
if (_eventType == LoggingEventType.TargetFinishedEvent && _targetFinishedTranslator != null)
496504
{
497505
_targetFinishedTranslator(translator, (TargetFinishedEventArgs)_buildEvent);

0 commit comments

Comments
 (0)