Skip to content

Commit 41a3fbb

Browse files
authored
StaticLogMessage isn't pooling correctly (#78652)
Noticed this when looking at a profile and was curious why there were 3 MB of allocations of the StaticLogMessage type. LogMessage.Free sets _message to null before calling FreeCore, thus StaticLogMessage.FreeCore was never adding items back to it's pool. The ordering of LogMessage setting _message to null and calling FreeCore is important, so that can't be changed. Instead, add a flag to StaticLogMessage to indicate whether it's in a constructed state or not. ![image](https://github.com/user-attachments/assets/de113a29-fa39-489c-8a9f-54fc1de53534)
2 parents 0501e49 + 9fb1979 commit 41a3fbb

File tree

1 file changed

+5
-1
lines changed
  • src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Log

1 file changed

+5
-1
lines changed

src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Log/LogMessage.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,13 @@ private sealed class StaticLogMessage : LogMessage
6060
{
6161
private static readonly ObjectPool<StaticLogMessage> s_pool = SharedPools.Default<StaticLogMessage>();
6262

63+
private bool _isConstructed;
64+
6365
public static LogMessage Construct(string message, LogLevel logLevel)
6466
{
6567
var logMessage = s_pool.Allocate();
6668
logMessage._message = message;
69+
logMessage._isConstructed = true;
6770
logMessage.LogLevel = logLevel;
6871

6972
return logMessage;
@@ -74,11 +77,12 @@ protected override string CreateMessage()
7477

7578
protected override void FreeCore()
7679
{
77-
if (_message == null)
80+
if (!_isConstructed)
7881
{
7982
return;
8083
}
8184

85+
_isConstructed = false;
8286
_message = null;
8387
s_pool.Free(this);
8488
}

0 commit comments

Comments
 (0)