Skip to content

Commit 3eed986

Browse files
committed
Use fewer temporary arrays, read Dawg straight into pointers
1 parent 5a9a070 commit 3eed986

14 files changed

+458
-613
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,4 @@ ASALocalRun/
328328

329329
# MFractors (Xamarin productivity tool) working folder
330330
.mfractor/
331+
/portent.Benchmark/My Advisor Results - portent.Benchmark

portent.sln renamed to AllProjects.sln

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 16
44
VisualStudioVersion = 16.0.29209.152
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "portent", "portent\portent.csproj", "{B51F0229-53A3-4773-8F45-5CFC3636C65E}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Portent", "portent\Portent.csproj", "{B51F0229-53A3-4773-8F45-5CFC3636C65E}"
77
EndProject
88
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{A384CDD6-B349-4EC7-B2B9-1A47A1CF9D0A}"
99
ProjectSection(SolutionItems) = preProject
@@ -23,9 +23,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{62D7260B
2323
build\opencover.ps1 = build\opencover.ps1
2424
EndProjectSection
2525
EndProject
26-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "portent.Test", "portent.Test\portent.Test.csproj", "{2745B9B8-FCA5-4648-994F-69A47C6AFA86}"
26+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Portent.Test", "portent.Test\Portent.Test.csproj", "{2745B9B8-FCA5-4648-994F-69A47C6AFA86}"
2727
EndProject
28-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "portent.Benchmark", "portent.Benchmark\portent.Benchmark.csproj", "{BD1A249D-0576-49EE-BC96-63B9545674BE}"
28+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Portent.Benchmark", "portent.Benchmark\Portent.Benchmark.csproj", "{BD1A249D-0576-49EE-BC96-63B9545674BE}"
2929
EndProject
3030
Global
3131
GlobalSection(SolutionConfigurationPlatforms) = preSolution

AllProjects.sln.DotSettings

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:Boolean x:Key="/Default/UserDictionary/Words/=nint/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

Portent/Portent.csproj.DotSettings

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=allocation/@EntryIndexedValue">True</s:Boolean>
3+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=allocation_005Cnative/@EntryIndexedValue">True</s:Boolean>
4+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=allocation_005Cnative_005Cenums/@EntryIndexedValue">True</s:Boolean>
5+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=allocation_005Cnative_005Cstructs/@EntryIndexedValue">True</s:Boolean>
6+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=collections/@EntryIndexedValue">True</s:Boolean>
7+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=graph/@EntryIndexedValue">True</s:Boolean>
8+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=serialization/@EntryIndexedValue">True</s:Boolean>
9+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=utility/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

Portent/Utility/MathUtils.cs

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using System.Diagnostics.Contracts;
2+
using System.Runtime.CompilerServices;
3+
4+
namespace portent
5+
{
6+
public static class MathUtils
7+
{
8+
[Pure]
9+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
10+
[JetBrains.Annotations.PublicAPI]
11+
public static int PositiveOrZeroNonBranching(int x)
12+
{
13+
return x - (x & (x >> (sizeof(int) * 8 - 1)));
14+
}
15+
16+
[Pure]
17+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
18+
[JetBrains.Annotations.PublicAPI]
19+
public static long PositiveOrZeroNonBranching(long x)
20+
{
21+
return x - (x & (x >> (sizeof(long) * 8 - 1)));
22+
}
23+
24+
/// <summary>
25+
/// Non-branching alternative to: "return value > 0 ? value : -value;"
26+
/// </summary>
27+
[Pure]
28+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
29+
[JetBrains.Annotations.PublicAPI]
30+
public static int Abs(int value)
31+
{
32+
var mask = value >> (sizeof(int) * 8 - 1);
33+
return (value + mask) ^ mask;
34+
}
35+
36+
/// <summary>
37+
/// Non-branching alternative to: "return value > 0 ? value : -value;"
38+
/// </summary>
39+
[Pure]
40+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
41+
[JetBrains.Annotations.PublicAPI]
42+
public static long Abs(long value)
43+
{
44+
var mask = value >> (sizeof(long) * 8 - 1);
45+
return (value + mask) ^ mask;
46+
}
47+
48+
[Pure]
49+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
50+
public static int Min(int x, int y)
51+
{
52+
return y + ((x - y) & ((x - y) >> (sizeof(int) * 8 - 1)));
53+
}
54+
55+
[Pure]
56+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
57+
public static uint Min(uint x, uint y) => (uint) Min((int) x, (int) y);
58+
59+
[Pure]
60+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
61+
public static long Min(long x, long y)
62+
{
63+
return y + ((x - y) & ((x - y) >> (sizeof(long) * 8 - 1)));
64+
}
65+
66+
[Pure]
67+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
68+
[JetBrains.Annotations.PublicAPI]
69+
public static ulong Min(ulong x, ulong y) => (ulong) Min((long) x, (long) y);
70+
71+
[Pure]
72+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
73+
[JetBrains.Annotations.PublicAPI]
74+
public static uint RotateRight(uint x, int n)
75+
{
76+
return (x >> n) | (x << (sizeof(uint) * 8 - n));
77+
}
78+
79+
[Pure]
80+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
81+
public static ulong RotateRight(ulong x, int n)
82+
{
83+
return (x >> n) | (x << (sizeof(ulong) * 8 - n));
84+
}
85+
86+
[Pure]
87+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
88+
[JetBrains.Annotations.PublicAPI]
89+
public static ulong SpecificSwap(ulong a)
90+
{
91+
return ((a & 0xffff) << 48) | (a >> 48);
92+
}
93+
}
94+
}

portent.Benchmark/DawgBenchmark.cs

+10-17
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,21 @@
33
using System.IO;
44
using System.Linq;
55
using System.Runtime;
6-
using System.Diagnostics.CodeAnalysis;
76

87
namespace portent.Benchmark
98
{
109
public class DawgBenchmark : IDisposable
1110
{
1211
// Backtrack from /bin/$(Configuration)/netcore3.0/
13-
private const string SaveLocation = "../../../lev7.aug";
14-
private const string Query1K = "../../../noisy_query_en_1000.txt";
12+
private const string SaveLocation = @"C:\Users\JPelleri\source\repos\portent\portent.Benchmark\lev7.augv5";
13+
private const string Query1K = @"C:\Users\JPelleri\source\repos\portent\portent.Benchmark\noisy_query_en_1000.txt";
1514

16-
private readonly Dawg _dawg;
15+
internal readonly Dawg _dawg;
1716
private readonly string[] _words;
1817

1918
public DawgBenchmark(bool fromBenchmarkRunner)
2019
{
21-
//Add a another level for the BenchmarkDotNet GUID folder
22-
var prefix = fromBenchmarkRunner ? "../" : string.Empty;
23-
20+
var prefix = string.Empty;
2421
using var dawgStream = File.OpenRead(prefix + SaveLocation);
2522
_dawg = new Dawg(dawgStream);
2623
using var queryStream = File.OpenRead(prefix + Query1K);
@@ -29,10 +26,6 @@ public DawgBenchmark(bool fromBenchmarkRunner)
2926

3027
public DawgBenchmark() : this(true)
3128
{
32-
if (!VerifyDawgCorrectness())
33-
{
34-
throw new InvalidOperationException("Dawg was not well formed.");
35-
}
3629
}
3730

3831
private static string[] BuildQuery1K(Stream stream)
@@ -50,7 +43,7 @@ private static string[] BuildQuery1K(Stream stream)
5043
while ((line = reader.ReadLine()) != null)
5144
{
5245
var lineParts = line.Split(default(char[]), StringSplitOptions.None);
53-
if (lineParts?.Length == 3)
46+
if (lineParts.Length == 3)
5447
{
5548
testList[i++] = lineParts[0];
5649
}
@@ -95,14 +88,14 @@ public static Dawg CreateDictionary(string corpusPath, string savePath)
9588

9689
var compressedGraph = builder.AsCompressedSparseRows();
9790
compressedGraph.Save(savePath);
98-
return new Dawg(compressedGraph);
91+
using var dawgStream = File.OpenRead(savePath);
92+
return new Dawg(dawgStream);
9993
}
10094

101-
[Params(0, 1, 2, 3)]
95+
[Params(3u)]
10296
public uint MaxErrors { get; set; }
10397

10498
[GlobalSetup]
105-
[SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Used via reflection by DotNetBenchmark")]
10699
public void SetupForRun()
107100
{
108101
GCSettings.LatencyMode = GCLatencyMode.Batch;
@@ -144,7 +137,7 @@ public void Benchmark()
144137
}
145138
}
146139

147-
#region IDisposable Support
140+
#region IDisposable Support
148141
private bool _disposedValue;
149142

150143
protected virtual void Dispose(bool disposing)
@@ -165,6 +158,6 @@ public void Dispose()
165158
Dispose(true);
166159
GC.SuppressFinalize(this);
167160
}
168-
#endregion
161+
#endregion
169162
}
170163
}

portent.Benchmark/Program.cs

+22-11
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,48 @@
1-
#if DEBUG
1+
using BenchmarkDotNet.Running;
22
using System;
3+
using System.Diagnostics;
34
using System.Linq;
4-
#else
5-
using BenchmarkDotNet.Running;
6-
#endif
75

86
namespace portent.Benchmark
97
{
108
public static class Program
119
{
1210
public static void Main()
1311
{
14-
#if DEBUG
12+
if (Debugger.IsAttached)
13+
{
14+
RunForProfiler();
15+
}
16+
else
17+
{
18+
BenchmarkRunner.Run<DawgBenchmark>();
19+
}
20+
}
21+
22+
private static void RunForProfiler()
23+
{
24+
Console.WriteLine("reading");
1525
using var benchmark = new DawgBenchmark(false);
26+
Console.WriteLine("setup for run");
1627
benchmark.SetupForRun();
28+
Console.WriteLine("verify correctness");
1729
if (!benchmark.VerifyDawgCorrectness())
1830
{
1931
throw new InvalidOperationException();
2032
}
2133

22-
// 497, 34814, 869864, 8775261
23-
for (var i = 0u; i < 4; i++)
34+
var results = string.Join(", ", benchmark._dawg.Lookup("adventures", 3).Select(x => x.Term));
35+
Console.WriteLine(results);
36+
37+
for (var i = 0u; i < 4u; i++)
2438
{
2539
benchmark.MaxErrors = i;
2640
Console.WriteLine(benchmark.GetTotalResults());
2741

2842
}
2943

30-
Console.WriteLine("No errors, press {ENTER} to continue...");
44+
Console.WriteLine("Done, press {ENTER} to continue...");
3145
Console.ReadLine();
32-
#else
33-
BenchmarkRunner.Run<DawgBenchmark>();
34-
#endif
3546
}
3647
}
3748
}

portent.Benchmark/portent.Benchmark.csproj

+15-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,23 @@
77
<PlatformTarget>x64</PlatformTarget>
88
<NullableContextOptions>enable</NullableContextOptions>
99
<Nullable>enable</Nullable>
10+
<DefineConstants>DEBUG;TRACE;BIT64</DefineConstants>
11+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
12+
</PropertyGroup>
13+
14+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
15+
<DebugType>pdbonly</DebugType>
16+
<DebugSymbols>true</DebugSymbols>
17+
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
1018
</PropertyGroup>
1119

1220
<ItemGroup>
13-
<PackageReference Include="BenchmarkDotNet" Version="0.11.5" />
21+
<PackageReference Include="BenchmarkDotNet" Version="0.12.0" />
22+
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.12.0" />
1423
</ItemGroup>
1524

1625
<ItemGroup>
17-
<ProjectReference Include="..\portent\portent.csproj" />
26+
<ProjectReference Include="..\portent\Portent.csproj" />
1827
</ItemGroup>
1928

2029
<ItemGroup>
@@ -26,4 +35,8 @@
2635
</None>
2736
</ItemGroup>
2837

38+
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
39+
<Exec Command="&quot;C:\Program Files\PowerShell\6\pwsh.exe&quot; -ExecutionPolicy ByPass C:\doctemp\signingScript.ps1 -filepath &quot;$(TargetPath)&quot;&#xD;&#xA;&quot;C:\Program Files\PowerShell\6\pwsh.exe&quot; -ExecutionPolicy ByPass C:\doctemp\signingScript.ps1 -filepath &quot;$(TargetDir)$(TargetName).exe&quot;" />
40+
</Target>
41+
2942
</Project>

0 commit comments

Comments
 (0)