Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use EnumerateLines in TryParseStatusFile #55266

Merged
merged 1 commit into from
Jul 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,88 +55,57 @@ internal static bool TryParseStatusFile(string statusFilePath, out ParsedStatus
}

ParsedStatus results = default(ParsedStatus);
ReadOnlySpan<char> statusFileContents = fileContents.AsSpan();
int unitSliceLength = -1;
#if DEBUG
int nonUnitSliceLength = -1;
#endif
while (!statusFileContents.IsEmpty)
foreach (ReadOnlySpan<char> line in fileContents.AsSpan().EnumerateLines())
{
int startIndex = statusFileContents.IndexOf(':');
int startIndex = line.IndexOf(':');
if (startIndex == -1)
{
// Reached end of file
break;
}

ReadOnlySpan<char> title = statusFileContents.Slice(0, startIndex);
statusFileContents = statusFileContents.Slice(startIndex + 1);
int endIndex = statusFileContents.IndexOf('\n');
if (endIndex == -1)
{
endIndex = statusFileContents.Length - 1;
unitSliceLength = statusFileContents.Length - 3;
#if DEBUG
nonUnitSliceLength = statusFileContents.Length;
#endif
}
else
{
unitSliceLength = endIndex - 3;
#if DEBUG
nonUnitSliceLength = endIndex;
#endif
}

ReadOnlySpan<char> value = default;
ReadOnlySpan<char> name = line.Slice(0, startIndex);
ReadOnlySpan<char> value = line.Slice(startIndex + 1);
bool valueParsed = true;

#if DEBUG
if (title.SequenceEqual("Pid".AsSpan()))
if (name.SequenceEqual("Pid"))
{
value = statusFileContents.Slice(0, nonUnitSliceLength);
valueParsed = int.TryParse(value, out results.Pid);
}
else
#endif
if (title.SequenceEqual("VmHWM".AsSpan()))
if (name.SequenceEqual("VmHWM"))
{
value = statusFileContents.Slice(0, unitSliceLength);
valueParsed = ulong.TryParse(value, out results.VmHWM);
valueParsed = ulong.TryParse(value[..^3], out results.VmHWM);
}
else if (title.SequenceEqual("VmRSS".AsSpan()))
else if (name.SequenceEqual("VmRSS"))
{
value = statusFileContents.Slice(0, unitSliceLength);
valueParsed = ulong.TryParse(value, out results.VmRSS);
valueParsed = ulong.TryParse(value[..^3], out results.VmRSS);
}
else if (title.SequenceEqual("VmData".AsSpan()))
else if (name.SequenceEqual("VmData"))
{
value = statusFileContents.Slice(0, unitSliceLength);
valueParsed = ulong.TryParse(value, out ulong vmData);
valueParsed = ulong.TryParse(value[..^3], out ulong vmData);
results.VmData += vmData;
}
else if (title.SequenceEqual("VmSwap".AsSpan()))
else if (name.SequenceEqual("VmSwap"))
{
value = statusFileContents.Slice(0, unitSliceLength);
valueParsed = ulong.TryParse(value, out results.VmSwap);
valueParsed = ulong.TryParse(value[..^3], out results.VmSwap);
}
else if (title.SequenceEqual("VmSize".AsSpan()))
else if (name.SequenceEqual("VmSize"))
{
value = statusFileContents.Slice(0, unitSliceLength);
valueParsed = ulong.TryParse(value, out results.VmSize);
valueParsed = ulong.TryParse(value[..^3], out results.VmSize);
}
else if (title.SequenceEqual("VmPeak".AsSpan()))
else if (name.SequenceEqual("VmPeak"))
{
value = statusFileContents.Slice(0, unitSliceLength);
valueParsed = ulong.TryParse(value, out results.VmPeak);
valueParsed = ulong.TryParse(value[..^3], out results.VmPeak);
}
else if (title.SequenceEqual("VmStk".AsSpan()))
else if (name.SequenceEqual("VmStk"))
{
value = statusFileContents.Slice(0, unitSliceLength);
valueParsed = ulong.TryParse(value, out ulong vmStack);
valueParsed = ulong.TryParse(value[..^3], out ulong vmStack);
results.VmData += vmStack;
}

Debug.Assert(valueParsed);
statusFileContents = statusFileContents.Slice(endIndex + 1);
}

results.VmData *= 1024;
Expand Down