Skip to content
Closed
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
14 changes: 6 additions & 8 deletions src/libraries/System.Private.CoreLib/src/System/Convert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2901,15 +2901,13 @@ private static unsafe int FromBase64_ComputeResultLength(char* inputPtr, int inp

// Perf: reuse the variable that stored the number of '=' to store the number of bytes encoded by the
// last group that contains the '=':
if (padding != 0)
padding = padding switch
{
if (padding == 1)
padding = 2;
else if (padding == 2)
padding = 1;
else
throw new FormatException(SR.Format_BadBase64Char);
}
0 => 0,
1 => 2,
2 => 1,
_ => throw new FormatException(SR.Format_BadBase64Char)
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The title of the PR is "reduce branches", but this doesn't actually reduce branches today, does it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this: "Improve readability of Base64 padding adjustment logic"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, the change seems too minor to have a meaningful impact. It might be more worthwhile to focus on removing "unsafe" in FromBase64_ComputeResultLength and related methods. Here's the commit for that: rameel@fe9f28b

Unfortunately due to the current JIT's inability to eliminate unnecessary bounds checks in cases like this (see #115091):

while (chars.Length != 0)
{
    if (chars[^1] is not (' ' or '\n' or '\r' or '\t'))
        break;

    chars = chars.Slice(0, chars.Length - 1); // Unnecessary bounds checks
}

and a performance regression compared to NET9 (#115090) I decided to postpone creating PR for this until later.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, anyway


// Done:
return (usefulInputLength / 4) * 3 + padding;
Expand Down
Loading