Skip to content

Commit

Permalink
Merge pull request #281 from SixLabors/js/perf-tweaks
Browse files Browse the repository at this point in the history
Speed up Unicode Data lookups and remove reflection.
  • Loading branch information
JimBobSquarePants authored Jun 29, 2022
2 parents 1b80769 + 7438b74 commit fc97cb1
Show file tree
Hide file tree
Showing 34 changed files with 3,938 additions and 149 deletions.
17 changes: 12 additions & 5 deletions SixLabors.Fonts.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29512.175
# Visual Studio Version 17
VisualStudioVersion = 17.2.32519.379
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_root", "_root", "{C317F1B1-D75E-4C6D-83EB-80367343E0D7}"
ProjectSection(SolutionItems) = preProject
Expand Down Expand Up @@ -74,10 +74,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "UnicodeTestData", "UnicodeT
tests\UnicodeTestData\README.md = tests\UnicodeTestData\README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SixLabors.Fonts.Benchmarks", "tests\SixLabors.Fonts.Benchmarks\SixLabors.Fonts.Benchmarks\SixLabors.Fonts.Benchmarks.csproj", "{FB8FDC5F-7FEB-4132-9133-C25E05C0B3D9}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
shared-infrastructure\src\SharedInfrastructure\SharedInfrastructure.projitems*{09e744ec-4852-4fc7-be78-c1b399f17967}*SharedItemsImports = 5
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
Expand All @@ -103,6 +102,10 @@ Global
{ABB6E111-672F-4846-88D6-C49C6CD01606}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ABB6E111-672F-4846-88D6-C49C6CD01606}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ABB6E111-672F-4846-88D6-C49C6CD01606}.Release|Any CPU.Build.0 = Release|Any CPU
{FB8FDC5F-7FEB-4132-9133-C25E05C0B3D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FB8FDC5F-7FEB-4132-9133-C25E05C0B3D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FB8FDC5F-7FEB-4132-9133-C25E05C0B3D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FB8FDC5F-7FEB-4132-9133-C25E05C0B3D9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -117,8 +120,12 @@ Global
{CFCC940C-DEA3-42CC-9626-0B7D09289FF4} = {7CDD4908-7CCD-4945-860C-D2F1732D3AE6}
{ABB6E111-672F-4846-88D6-C49C6CD01606} = {249327CF-1415-428B-8EEA-8C7705B1DE8F}
{654DD381-B93D-4459-B669-296F5D9172ED} = {56801022-D71A-4FBE-BC5B-CBA08E2284EC}
{FB8FDC5F-7FEB-4132-9133-C25E05C0B3D9} = {56801022-D71A-4FBE-BC5B-CBA08E2284EC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {38F4B47F-4F74-40F5-8707-C0EF1D0BDF92}
EndGlobalSection
GlobalSection(SharedMSBuildProjectFiles) = preSolution
shared-infrastructure\src\SharedInfrastructure\SharedInfrastructure.projitems*{09e744ec-4852-4fc7-be78-c1b399f17967}*SharedItemsImports = 5
EndGlobalSection
EndGlobal
13 changes: 0 additions & 13 deletions src/SixLabors.Fonts/SixLabors.Fonts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@
<AssemblyVersion Condition="'$(IsContinuousIntegration)'==''">1.0.0.0</AssemblyVersion>
</PropertyGroup>

<ItemGroup>
<None Remove="Unicode\Resources\**" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Unicode\Resources\**" />
</ItemGroup>

<ItemGroup>
<None Include="..\..\shared-infrastructure\branding\icons\fonts\sixlabors.fonts.128.png" Pack="true" PackagePath="" />
</ItemGroup>
Expand All @@ -43,10 +35,5 @@
<PackageReference Include="System.Memory" Version="4.5.4" />
</ItemGroup>

<ItemGroup>
<Resource Include="Unicode\Resources\Grapheme.trie" />
<Resource Include="Unicode\Resources\UnicodeCategory.trie" />
</ItemGroup>

<Import Project="..\..\shared-infrastructure\src\SharedInfrastructure\SharedInfrastructure.projitems" Label="Shared" />
</Project>
2 changes: 1 addition & 1 deletion src/SixLabors.Fonts/Unicode/BidiClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public readonly struct BidiClass
/// </summary>
/// <param name="codePoint">The codepoint.</param>
public BidiClass(CodePoint codePoint)
=> this.bidiValue = UnicodeData.GetBidiData(codePoint.Value);
=> this.bidiValue = UnicodeData.GetBidiData((uint)codePoint.Value);

/// <summary>
/// Gets the Unicode Bidirectional character type.
Expand Down
38 changes: 17 additions & 21 deletions src/SixLabors.Fonts/Unicode/CodePoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -435,22 +435,18 @@ public static bool IsTabulation(CodePoint codePoint)
/// <param name="codePoint">The codepoint to evaluate.</param>
/// <returns><see langword="true"/> if <paramref name="codePoint"/> is a new line indicator; otherwise, <see langword="false"/></returns>
public static bool IsNewLine(CodePoint codePoint)
{
// See https://www.unicode.org/standard/reports/tr13/tr13-5.html
switch (codePoint.Value)
{
case 0x000A: // LINE FEED (LF)
case 0x000B: // LINE TABULATION (VT)
case 0x000C: // FORM FEED (FF)
case 0x000D: // CARRIAGE RETURN (CR)
case 0x0085: // NEXT LINE (NEL)
case 0x2028: // LINE SEPARATOR (LS)
case 0x2029: // PARAGRAPH SEPARATOR (PS)
return true;
default:
return false;
}
}
=> codePoint.Value switch
{
// See https://www.unicode.org/standard/reports/tr13/tr13-5.html
0x000A // LINE FEED (LF)
or 0x000B // LINE TABULATION (VT)
or 0x000C // FORM FEED (FF)
or 0x000D // CARRIAGE RETURN (CR)
or 0x0085 // NEXT LINE (NEL)
or 0x2028 // LINE SEPARATOR (LS)
or 0x2029 => true, // PARAGRAPH SEPARATOR (PS)
_ => false,
};

/// <summary>
/// Returns the number of codepoints in a given string buffer.
Expand Down Expand Up @@ -518,7 +514,7 @@ public static BidiClass GetBidiClass(CodePoint codePoint)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryGetBidiMirror(CodePoint codePoint, out CodePoint mirror)
{
uint value = UnicodeData.GetBidiMirror(codePoint.Value);
uint value = UnicodeData.GetBidiMirror(codePoint.value);

if (value == 0u)
{
Expand Down Expand Up @@ -561,15 +557,15 @@ public static bool TryGetVerticalMirror(CodePoint codePoint, out CodePoint mirro
/// <param name="codePoint">The codepoint to evaluate.</param>
/// <returns>The <see cref="LineBreakClass"/>.</returns>
public static LineBreakClass GetLineBreakClass(CodePoint codePoint)
=> UnicodeData.GetLineBreakClass(codePoint.Value);
=> UnicodeData.GetLineBreakClass(codePoint.value);

/// <summary>
/// Gets the <see cref="GraphemeClusterClass"/> for the given codepoint.
/// </summary>
/// <param name="codePoint">The codepoint to evaluate.</param>
/// <returns>The <see cref="GraphemeClusterClass"/>.</returns>
public static GraphemeClusterClass GetGraphemeClusterClass(CodePoint codePoint)
=> UnicodeData.GetGraphemeClusterClass(codePoint.Value);
=> UnicodeData.GetGraphemeClusterClass(codePoint.value);

/// <summary>
/// Gets the <see cref="JoiningClass"/> for the given codepoint.
Expand All @@ -585,7 +581,7 @@ internal static JoiningClass GetJoiningClass(CodePoint codePoint)
/// <param name="codePoint">The codepoint to evaluate.</param>
/// <returns>The <see cref="ScriptClass"/>.</returns>
internal static ScriptClass GetScriptClass(CodePoint codePoint)
=> UnicodeData.GetScriptClass(codePoint.Value);
=> UnicodeData.GetScriptClass(codePoint.value);

/// <summary>
/// Gets the <see cref="UnicodeCategory"/> for the given codepoint.
Expand All @@ -599,7 +595,7 @@ public static UnicodeCategory GetGeneralCategory(CodePoint codePoint)
return (UnicodeCategory)(AsciiCharInfo[codePoint.Value] & UnicodeCategoryMask);
}

return UnicodeData.GetUnicodeCategory(codePoint.Value);
return UnicodeData.GetUnicodeCategory(codePoint.value);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/SixLabors.Fonts/Unicode/JoiningClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public readonly struct JoiningClass
public JoiningClass(CodePoint codePoint)
{
UnicodeCategory category = CodePoint.GetGeneralCategory(codePoint);
uint value = UnicodeData.GetJoiningClass(codePoint.Value);
uint value = UnicodeData.GetJoiningClass((uint)codePoint.Value);
this.JoiningType = GetJoiningType(codePoint, value, category);
this.JoiningGroup = (JoiningGroup)((value >> 16) & 0xFF);
}
Expand Down
Binary file not shown.
Loading

0 comments on commit fc97cb1

Please sign in to comment.