Skip to content

Commit fc97cb1

Browse files
Merge pull request #281 from SixLabors/js/perf-tweaks
Speed up Unicode Data lookups and remove reflection.
2 parents 1b80769 + 7438b74 commit fc97cb1

34 files changed

+3938
-149
lines changed

SixLabors.Fonts.sln

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Microsoft Visual Studio Solution File, Format Version 12.00
2-
# Visual Studio Version 16
3-
VisualStudioVersion = 16.0.29512.175
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.2.32519.379
44
MinimumVisualStudioVersion = 10.0.40219.1
55
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_root", "_root", "{C317F1B1-D75E-4C6D-83EB-80367343E0D7}"
66
ProjectSection(SolutionItems) = preProject
@@ -74,10 +74,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "UnicodeTestData", "UnicodeT
7474
tests\UnicodeTestData\README.md = tests\UnicodeTestData\README.md
7575
EndProjectSection
7676
EndProject
77+
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}"
78+
EndProject
7779
Global
78-
GlobalSection(SharedMSBuildProjectFiles) = preSolution
79-
shared-infrastructure\src\SharedInfrastructure\SharedInfrastructure.projitems*{09e744ec-4852-4fc7-be78-c1b399f17967}*SharedItemsImports = 5
80-
EndGlobalSection
8180
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8281
Debug|Any CPU = Debug|Any CPU
8382
Release|Any CPU = Release|Any CPU
@@ -103,6 +102,10 @@ Global
103102
{ABB6E111-672F-4846-88D6-C49C6CD01606}.Debug|Any CPU.Build.0 = Debug|Any CPU
104103
{ABB6E111-672F-4846-88D6-C49C6CD01606}.Release|Any CPU.ActiveCfg = Release|Any CPU
105104
{ABB6E111-672F-4846-88D6-C49C6CD01606}.Release|Any CPU.Build.0 = Release|Any CPU
105+
{FB8FDC5F-7FEB-4132-9133-C25E05C0B3D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
106+
{FB8FDC5F-7FEB-4132-9133-C25E05C0B3D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
107+
{FB8FDC5F-7FEB-4132-9133-C25E05C0B3D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
108+
{FB8FDC5F-7FEB-4132-9133-C25E05C0B3D9}.Release|Any CPU.Build.0 = Release|Any CPU
106109
EndGlobalSection
107110
GlobalSection(SolutionProperties) = preSolution
108111
HideSolutionNode = FALSE
@@ -117,8 +120,12 @@ Global
117120
{CFCC940C-DEA3-42CC-9626-0B7D09289FF4} = {7CDD4908-7CCD-4945-860C-D2F1732D3AE6}
118121
{ABB6E111-672F-4846-88D6-C49C6CD01606} = {249327CF-1415-428B-8EEA-8C7705B1DE8F}
119122
{654DD381-B93D-4459-B669-296F5D9172ED} = {56801022-D71A-4FBE-BC5B-CBA08E2284EC}
123+
{FB8FDC5F-7FEB-4132-9133-C25E05C0B3D9} = {56801022-D71A-4FBE-BC5B-CBA08E2284EC}
120124
EndGlobalSection
121125
GlobalSection(ExtensibilityGlobals) = postSolution
122126
SolutionGuid = {38F4B47F-4F74-40F5-8707-C0EF1D0BDF92}
123127
EndGlobalSection
128+
GlobalSection(SharedMSBuildProjectFiles) = preSolution
129+
shared-infrastructure\src\SharedInfrastructure\SharedInfrastructure.projitems*{09e744ec-4852-4fc7-be78-c1b399f17967}*SharedItemsImports = 5
130+
EndGlobalSection
124131
EndGlobal

src/SixLabors.Fonts/SixLabors.Fonts.csproj

-13
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,6 @@
1717
<AssemblyVersion Condition="'$(IsContinuousIntegration)'==''">1.0.0.0</AssemblyVersion>
1818
</PropertyGroup>
1919

20-
<ItemGroup>
21-
<None Remove="Unicode\Resources\**" />
22-
</ItemGroup>
23-
24-
<ItemGroup>
25-
<EmbeddedResource Include="Unicode\Resources\**" />
26-
</ItemGroup>
27-
2820
<ItemGroup>
2921
<None Include="..\..\shared-infrastructure\branding\icons\fonts\sixlabors.fonts.128.png" Pack="true" PackagePath="" />
3022
</ItemGroup>
@@ -43,10 +35,5 @@
4335
<PackageReference Include="System.Memory" Version="4.5.4" />
4436
</ItemGroup>
4537

46-
<ItemGroup>
47-
<Resource Include="Unicode\Resources\Grapheme.trie" />
48-
<Resource Include="Unicode\Resources\UnicodeCategory.trie" />
49-
</ItemGroup>
50-
5138
<Import Project="..\..\shared-infrastructure\src\SharedInfrastructure\SharedInfrastructure.projitems" Label="Shared" />
5239
</Project>

src/SixLabors.Fonts/Unicode/BidiClass.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public readonly struct BidiClass
1818
/// </summary>
1919
/// <param name="codePoint">The codepoint.</param>
2020
public BidiClass(CodePoint codePoint)
21-
=> this.bidiValue = UnicodeData.GetBidiData(codePoint.Value);
21+
=> this.bidiValue = UnicodeData.GetBidiData((uint)codePoint.Value);
2222

2323
/// <summary>
2424
/// Gets the Unicode Bidirectional character type.

src/SixLabors.Fonts/Unicode/CodePoint.cs

+17-21
Original file line numberDiff line numberDiff line change
@@ -435,22 +435,18 @@ public static bool IsTabulation(CodePoint codePoint)
435435
/// <param name="codePoint">The codepoint to evaluate.</param>
436436
/// <returns><see langword="true"/> if <paramref name="codePoint"/> is a new line indicator; otherwise, <see langword="false"/></returns>
437437
public static bool IsNewLine(CodePoint codePoint)
438-
{
439-
// See https://www.unicode.org/standard/reports/tr13/tr13-5.html
440-
switch (codePoint.Value)
441-
{
442-
case 0x000A: // LINE FEED (LF)
443-
case 0x000B: // LINE TABULATION (VT)
444-
case 0x000C: // FORM FEED (FF)
445-
case 0x000D: // CARRIAGE RETURN (CR)
446-
case 0x0085: // NEXT LINE (NEL)
447-
case 0x2028: // LINE SEPARATOR (LS)
448-
case 0x2029: // PARAGRAPH SEPARATOR (PS)
449-
return true;
450-
default:
451-
return false;
452-
}
453-
}
438+
=> codePoint.Value switch
439+
{
440+
// See https://www.unicode.org/standard/reports/tr13/tr13-5.html
441+
0x000A // LINE FEED (LF)
442+
or 0x000B // LINE TABULATION (VT)
443+
or 0x000C // FORM FEED (FF)
444+
or 0x000D // CARRIAGE RETURN (CR)
445+
or 0x0085 // NEXT LINE (NEL)
446+
or 0x2028 // LINE SEPARATOR (LS)
447+
or 0x2029 => true, // PARAGRAPH SEPARATOR (PS)
448+
_ => false,
449+
};
454450

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

523519
if (value == 0u)
524520
{
@@ -561,15 +557,15 @@ public static bool TryGetVerticalMirror(CodePoint codePoint, out CodePoint mirro
561557
/// <param name="codePoint">The codepoint to evaluate.</param>
562558
/// <returns>The <see cref="LineBreakClass"/>.</returns>
563559
public static LineBreakClass GetLineBreakClass(CodePoint codePoint)
564-
=> UnicodeData.GetLineBreakClass(codePoint.Value);
560+
=> UnicodeData.GetLineBreakClass(codePoint.value);
565561

566562
/// <summary>
567563
/// Gets the <see cref="GraphemeClusterClass"/> for the given codepoint.
568564
/// </summary>
569565
/// <param name="codePoint">The codepoint to evaluate.</param>
570566
/// <returns>The <see cref="GraphemeClusterClass"/>.</returns>
571567
public static GraphemeClusterClass GetGraphemeClusterClass(CodePoint codePoint)
572-
=> UnicodeData.GetGraphemeClusterClass(codePoint.Value);
568+
=> UnicodeData.GetGraphemeClusterClass(codePoint.value);
573569

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

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

602-
return UnicodeData.GetUnicodeCategory(codePoint.Value);
598+
return UnicodeData.GetUnicodeCategory(codePoint.value);
603599
}
604600

605601
/// <summary>

src/SixLabors.Fonts/Unicode/JoiningClass.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public readonly struct JoiningClass
1919
public JoiningClass(CodePoint codePoint)
2020
{
2121
UnicodeCategory category = CodePoint.GetGeneralCategory(codePoint);
22-
uint value = UnicodeData.GetJoiningClass(codePoint.Value);
22+
uint value = UnicodeData.GetJoiningClass((uint)codePoint.Value);
2323
this.JoiningType = GetJoiningType(codePoint, value, category);
2424
this.JoiningGroup = (JoiningGroup)((value >> 16) & 0xFF);
2525
}
Binary file not shown.

0 commit comments

Comments
 (0)