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

Avoid calling Encoding.GetBytes when Vector128 is not hardware accelerated #85267

Merged
merged 1 commit into from
May 23, 2023
Merged
Show file tree
Hide file tree
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 @@ -162,6 +162,7 @@
<Reference Include="System.Reflection.Emit.Lightweight" />
<Reference Include="System.Reflection.Primitives" />
<Reference Include="System.Runtime" />
<Reference Include="System.Runtime.Intrinsics" />
<Reference Include="System.Runtime.Serialization.Formatters" />
<Reference Include="System.Runtime.Serialization.Primitives" />
<Reference Include="System.Text.Encoding.Extensions" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using System.Diagnostics;
Expand Down Expand Up @@ -361,7 +362,7 @@ protected unsafe int UnsafeGetUTF8Chars(char* chars, int charCount, byte[] buffe
fixed (byte* _bytes = &buffer[offset])
{
// Fast path for small strings, use Encoding.GetBytes for larger strings since it is faster when vectorization is possible
if ((uint)charCount < 32)
if (!Vector128.IsHardwareAccelerated || (uint)charCount < 32)
Copy link
Member

Choose a reason for hiding this comment

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

Where did the "32" come from? Vector128<ushort>.Count is 8 and Vector256<ushort>.Count is 16, so this is either 2x a 256-bit vector or 4x a 128-bit vector. Should it be if (!Vector128.IsHardwareAccelerated || charCount < Vector128<ushort>.Count?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The number is based on local benchmarks, it was originally 25 but was rounded up 32 after one of the datacontract microbenchmarks had a slight regression for me.

I hope to decrease the limit further after some more performance improvements to the ascii encoding.

It currently happens to be 2× sizeof(Vector128) (same as current limit of for hardware instrincts)

{
byte* bytes = _bytes;
char* charsMax = &chars[charCount];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
Microsoft Visual Studio Solution File, Format Version 12.00

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33530.505
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestUtilities", "..\Common\tests\TestUtilities\TestUtilities.csproj", "{CBA80130-6773-4DF9-995C-DC6CBED89CB5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Win32.Primitives", "..\Microsoft.Win32.Primitives\ref\Microsoft.Win32.Primitives.csproj", "{E5DB95E1-94AA-405C-9FFE-09B1E2498EE2}"
Expand Down Expand Up @@ -47,6 +51,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{DB29DBEF-FA4
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "gen", "gen", "{DE71D38E-4154-477C-9C27-3FA4ADB4098F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Runtime.Intrinsics", "..\System.Runtime.Intrinsics\ref\System.Runtime.Intrinsics.csproj", "{755CD0A1-7280-48C1-BE5B-ABE897F6657D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -133,31 +139,36 @@ Global
{DF2255F4-F671-4C15-9100-D8079992E19D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DF2255F4-F671-4C15-9100-D8079992E19D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DF2255F4-F671-4C15-9100-D8079992E19D}.Release|Any CPU.Build.0 = Release|Any CPU
{755CD0A1-7280-48C1-BE5B-ABE897F6657D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{755CD0A1-7280-48C1-BE5B-ABE897F6657D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{755CD0A1-7280-48C1-BE5B-ABE897F6657D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{755CD0A1-7280-48C1-BE5B-ABE897F6657D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{CBA80130-6773-4DF9-995C-DC6CBED89CB5} = {41101B02-36C9-476B-98D5-1A6E105BBF4A}
{8B069551-9B95-464E-BB40-C56817506FEC} = {41101B02-36C9-476B-98D5-1A6E105BBF4A}
{8FF5E841-29F6-4DB7-A4F8-9281FBDA0B9C} = {41101B02-36C9-476B-98D5-1A6E105BBF4A}
{45263D7D-249E-4810-8F7D-1DEF25515210} = {41101B02-36C9-476B-98D5-1A6E105BBF4A}
{E5DB95E1-94AA-405C-9FFE-09B1E2498EE2} = {18E62E91-73A2-48AE-BEFF-CE7C64DF759D}
{7DF41C40-FE5D-41DF-B106-3DD77BE4D4B5} = {18E62E91-73A2-48AE-BEFF-CE7C64DF759D}
{1392041A-E2CA-4553-BEAF-363974651B81} = {DB29DBEF-FA4E-4334-AFB8-BFB2DA82D1DE}
{E3347E75-EAE8-4E6B-98D1-7230B1EE5450} = {18E62E91-73A2-48AE-BEFF-CE7C64DF759D}
{5EE18CED-28AE-4415-B5A3-C31123BF57E1} = {18E62E91-73A2-48AE-BEFF-CE7C64DF759D}
{E813073E-07A7-4C88-A505-484CB33C9DC4} = {18E62E91-73A2-48AE-BEFF-CE7C64DF759D}
{76AC3DDD-2B38-489F-A8B0-8E43054595DB} = {DB29DBEF-FA4E-4334-AFB8-BFB2DA82D1DE}
{7D7457FD-B88C-4375-926D-7D46C71E34A7} = {DE71D38E-4154-477C-9C27-3FA4ADB4098F}
{D5FF2DBA-F304-4ACB-8F82-B8F9321E22A9} = {DE71D38E-4154-477C-9C27-3FA4ADB4098F}
{DAD8EBB8-A1D6-4E8F-A334-D7F0273280D1} = {18E62E91-73A2-48AE-BEFF-CE7C64DF759D}
{0C045A64-AE30-47CC-A931-5B5C6C9EF06D} = {18E62E91-73A2-48AE-BEFF-CE7C64DF759D}
{19F785D2-F7A4-41AB-9301-A6AD7E40B238} = {18E62E91-73A2-48AE-BEFF-CE7C64DF759D}
{9759BE1C-98A0-4319-AC82-D432002BD66B} = {18E62E91-73A2-48AE-BEFF-CE7C64DF759D}
{DF2255F4-F671-4C15-9100-D8079992E19D} = {18E62E91-73A2-48AE-BEFF-CE7C64DF759D}
{1392041A-E2CA-4553-BEAF-363974651B81} = {DB29DBEF-FA4E-4334-AFB8-BFB2DA82D1DE}
{76AC3DDD-2B38-489F-A8B0-8E43054595DB} = {DB29DBEF-FA4E-4334-AFB8-BFB2DA82D1DE}
{6FD10BE0-24C8-456E-8B9A-FD101C05C961} = {DB29DBEF-FA4E-4334-AFB8-BFB2DA82D1DE}
{7D7457FD-B88C-4375-926D-7D46C71E34A7} = {DE71D38E-4154-477C-9C27-3FA4ADB4098F}
{D5FF2DBA-F304-4ACB-8F82-B8F9321E22A9} = {DE71D38E-4154-477C-9C27-3FA4ADB4098F}
{8B069551-9B95-464E-BB40-C56817506FEC} = {41101B02-36C9-476B-98D5-1A6E105BBF4A}
{8FF5E841-29F6-4DB7-A4F8-9281FBDA0B9C} = {41101B02-36C9-476B-98D5-1A6E105BBF4A}
{45263D7D-249E-4810-8F7D-1DEF25515210} = {41101B02-36C9-476B-98D5-1A6E105BBF4A}
{9759BE1C-98A0-4319-AC82-D432002BD66B} = {18E62E91-73A2-48AE-BEFF-CE7C64DF759D}
{6E942A4A-405E-4AAD-89A7-006358A8A004} = {DE71D38E-4154-477C-9C27-3FA4ADB4098F}
{DF2255F4-F671-4C15-9100-D8079992E19D} = {18E62E91-73A2-48AE-BEFF-CE7C64DF759D}
{755CD0A1-7280-48C1-BE5B-ABE897F6657D} = {18E62E91-73A2-48AE-BEFF-CE7C64DF759D}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EE9FB522-4B73-4E3E-B63D-C21826BB7B5D}
Expand Down