Skip to content

Commit 1ecfd45

Browse files
Saqooshaclaude
andcommitted
perf: Optimize log type mapping performance in ConsoleLogsService
Move log type mapping logic outside the loop to avoid repeated processing. Added static Dictionary for MCP-to-Unity log type mappings with case-insensitive comparison. This improves performance and provides better extensibility for future log type mappings. Addresses CodeRabbit review feedback for performance optimization. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent cc2cb1a commit 1ecfd45

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

Editor/Services/ConsoleLogsService.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ namespace McpUnity.Services
1212
/// </summary>
1313
public class ConsoleLogsService : IConsoleLogsService
1414
{
15+
// Static mapping for MCP log types to Unity log types
16+
private static readonly Dictionary<string, string> LogTypeMapping = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
17+
{
18+
{ "info", "Log" }
19+
};
20+
1521
// Structure to store log information
1622
private class LogEntry
1723
{
@@ -76,15 +82,13 @@ public JArray GetAllLogsAsJson(string logType = "")
7682
JArray logsArray = new JArray();
7783
bool filter = !string.IsNullOrEmpty(logType);
7884

85+
// Map MCP log types to Unity log types outside the loop for better performance
86+
string unityLogType = filter && LogTypeMapping.TryGetValue(logType, out string mapped) ? mapped : logType;
87+
7988
lock (_logEntries)
8089
{
8190
foreach (var entry in _logEntries)
8291
{
83-
// Map MCP log types to Unity log types
84-
string unityLogType = logType;
85-
if (logType.Equals("info", System.StringComparison.OrdinalIgnoreCase))
86-
unityLogType = "Log";
87-
8892
if (filter && !entry.Type.ToString().Equals(unityLogType, System.StringComparison.OrdinalIgnoreCase))
8993
continue;
9094
logsArray.Add(new JObject

0 commit comments

Comments
 (0)