Skip to content

Commit 6a78f11

Browse files
committed
Optimize Go CLI output parsing by converting to utf-8 only once
1 parent f096423 commit 6a78f11

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/Microsoft.ComponentDetection.Detectors/go/Parsers/GoCLIParser.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace Microsoft.ComponentDetection.Detectors.Go;
55
using System.Collections.Generic;
66
using System.Diagnostics.CodeAnalysis;
77
using System.IO;
8+
using System.Text;
89
using System.Text.Json;
910
using System.Threading.Tasks;
1011
using Microsoft.ComponentDetection.Common.Telemetry.Records;
@@ -78,10 +79,11 @@ private void RecordBuildDependencies(string goListOutput, ISingleFileComponentRe
7879
using var record = new GoReplaceTelemetryRecord();
7980

8081
// Go CLI outputs multiple JSON objects (not an array), so we need to parse them one by one
81-
var remaining = goListOutput.AsMemory();
82+
var utf8Bytes = Encoding.UTF8.GetBytes(goListOutput);
83+
var remaining = utf8Bytes.AsSpan();
8284
while (!remaining.IsEmpty)
8385
{
84-
var reader = new Utf8JsonReader(System.Text.Encoding.UTF8.GetBytes(remaining.ToString()));
86+
var reader = new Utf8JsonReader(remaining);
8587
try
8688
{
8789
var buildModule = JsonSerializer.Deserialize<GoBuildModule>(ref reader);
@@ -95,7 +97,7 @@ private void RecordBuildDependencies(string goListOutput, ISingleFileComponentRe
9597
remaining = remaining[consumed..];
9698

9799
// Skip whitespace between JSON objects
98-
while (!remaining.IsEmpty && char.IsWhiteSpace(remaining.Span[0]))
100+
while (!remaining.IsEmpty && char.IsWhiteSpace((char)remaining[0]))
99101
{
100102
remaining = remaining[1..];
101103
}

0 commit comments

Comments
 (0)