Skip to content

Commit

Permalink
Merge pull request #12 from jzebedee/zero_alloc_sais
Browse files Browse the repository at this point in the history
Add Go SAIS suffix sorter
  • Loading branch information
jzebedee authored Jan 9, 2022
2 parents 9e85771 + e2af7e4 commit 6f2a6b3
Show file tree
Hide file tree
Showing 14 changed files with 1,264 additions and 74 deletions.
39 changes: 37 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore

# User-specific files
*.rsuser
Expand Down Expand Up @@ -29,7 +29,6 @@ x86/
bld/
[Bb]in/
[Oo]bj/
[Oo]ut/
[Ll]og/
[Ll]ogs/

Expand Down Expand Up @@ -91,6 +90,7 @@ StyleCopReport.xml
*.tmp_proj
*_wpftmp.csproj
*.log
*.tlog
*.vspscc
*.vssscc
.builds
Expand Down Expand Up @@ -294,6 +294,17 @@ node_modules/
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw

# Visual Studio 6 auto-generated project file (contains which files were open etc.)
*.vbp

# Visual Studio 6 workspace and project file (working project files containing files to include in project)
*.dsw
*.dsp

# Visual Studio 6 technical files
*.ncb
*.aps

# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
Expand Down Expand Up @@ -350,6 +361,9 @@ ASALocalRun/
# Local History for Visual Studio
.localhistory/

# Visual Studio History (VSHistory) files
.vshistory/

# BeatPulse healthcheck temp database
healthchecksdb

Expand All @@ -362,5 +376,26 @@ MigrationBackup/
# Fody - auto-generated XML schema
FodyWeavers.xsd

# VS Code files for those working on multiple tools
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace

# Local History for Visual Studio Code
.history/

# Windows Installer files from build outputs
*.cab
*.msi
*.msix
*.msm
*.msp

# JetBrains Rider
*.sln.iml

# DeltaQ
/pkg
70 changes: 55 additions & 15 deletions bench/DeltaQ.Benchmarks/SuffixSortingBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,76 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Engines;
using DeltaQ.SuffixSorting;
using DeltaQ.SuffixSorting.LibDivSufSort;
using DeltaQ.SuffixSorting.SAIS;

namespace DeltaQ.Benchmarks
{
//[MemoryDiagnoser]
[MemoryDiagnoser]
[HardwareCounters(HardwareCounter.BranchInstructions, HardwareCounter.BranchMispredictions)]
public class SuffixSortingBenchmarks
{
private static byte[] GetOwnedRandomBuffer(int size)
{
var rand = new Random(63 * 13 * 63 * 13);
var buf = new byte[size];
rand.NextBytes(buf);
return buf;
}

private const string AbsoluteAssetsPath = @"assets/";
public static IEnumerable<object[]> Assets { get; } = Directory.EnumerateFiles(AbsoluteAssetsPath)
.Select(file => new object[] { Path.GetFileName(file), File.ReadAllBytes(file) })
.Select(file => new { filename = Path.GetFileName(file), contents = File.ReadAllBytes(file) })
.Select(a => new object[] { a.filename, a.contents })
.ToArray();

private static readonly ISuffixSort LDSS = new LibDivSufSort();
private static readonly ISuffixSort SAIS = new SAIS();

[ArgumentsSource(nameof(Assets))]
[Benchmark]
public void ldss(string name, byte[] asset)
public static IEnumerable<int> Sizes
{
LDSS.Sort(asset).Dispose();
get
{
yield return 0;
yield return 1;
yield return 2;
yield return 4;
yield return 8;
yield return 16;
yield return 32;
yield return 64;
yield return 128;
yield return 256;
yield return 512;
yield return 1024;
yield return 2048;
yield return 4096;
yield return 8192;
yield return 16384;
yield return 32768;
for (int i = 64; i <= 1024; i += 64)
{
yield return i * 1024;
}
}
}
public static IEnumerable<object[]> Randoms { get; } = Sizes
.Select(i => new { size = i, asset = GetOwnedRandomBuffer(i) })
.Select(a => new object[] { a.size.ToString(), a.asset })
.ToArray();

private static readonly ISuffixSort GoSAIS = new GoSAIS();
private static readonly ISuffixSort SAIS = new SAIS();
private static readonly ISuffixSort LDSS = new LibDivSufSort();

[ArgumentsSource(nameof(Assets))]
[ArgumentsSource(nameof(Randoms))]
[Benchmark(Baseline = true)]
public void sais(string name, byte[] asset)
{
SAIS.Sort(asset).Dispose();
}
public void sais(string name, byte[] asset) => SAIS.Sort(asset).Dispose();

[ArgumentsSource(nameof(Randoms))]
[Benchmark]
public void go_sais(string name, byte[] asset) => GoSAIS.Sort(asset).Dispose();

[ArgumentsSource(nameof(Randoms))]
[Benchmark]
public void ldss(string name, byte[] asset) => LDSS.Sort(asset).Dispose();

}
}
7 changes: 5 additions & 2 deletions src/DeltaQ.CommandLine/DeltaQ.CommandLine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
<ToolCommandName>dq</ToolCommandName>
<PackAsTool>true</PackAsTool>

<!--<DefineConstants>FUZZ</DefineConstants>-->

<!-- Source Link Support -->
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
Expand Down Expand Up @@ -60,11 +62,12 @@
<ItemGroup>
<PackageReference Include="SharpFuzz" Version="1.6.2" />
</ItemGroup>

<!-- Instrumented fuzz binary, change as needed -->
<!--
<ItemGroup>
<Reference Include="DeltaQ.SuffixSorting.LibDivSufSort">
<HintPath>..\..\pkg\DeltaQ.SuffixSorting.LibDivSufSort.0.5.0\lib\net6.0\DeltaQ.SuffixSorting.LibDivSufSort.dll</HintPath>
<Reference Include="DeltaQ.SuffixSorting.SAIS">
<HintPath>..\..\pkg\DeltaQ.SuffixSorting.SAIS.2.1.1\lib\netstandard2.1\DeltaQ.SuffixSorting.SAIS.dll</HintPath>
</Reference>
</ItemGroup>
-->
Expand Down
10 changes: 7 additions & 3 deletions src/DeltaQ.CommandLine/Fuzzing/Commands.Fuzz.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using DeltaQ.CommandLine.Fuzzing;
using DeltaQ.SuffixSorting.LibDivSufSort;
using DeltaQ.SuffixSorting.SAIS;
using Microsoft.Extensions.CommandLineUtils;
using Microsoft.Toolkit.HighPerformance.Buffers;
using SharpFuzz;
using System;
using System.IO;
Expand Down Expand Up @@ -28,9 +30,11 @@ internal static partial class Commands
throw new InvalidOperationException();
}

var ldss = new LibDivSufSort();
using var ownedSA = ldss.Sort(T);
Verify(T, ownedSA.Memory.Span);
var sais = new GoSAIS();
using var ownedSA = sais.Sort(T);
var SA = ownedSA.Span;

Verify(T, SA);
});
return 0;
});
Expand Down
1 change: 1 addition & 0 deletions src/DeltaQ.CommandLine/Fuzzing/fuzz-min.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
afl-tmin -i findings/crashes.2022-01-07-13\:56\:25/id:000002,sig:02,src:000128,op:arith8,pos:91,val:+27 -o crash-force-sais-tmp-alloc -t 5000 -m 10000 -- dotnet bin/Release/net6.0/DeltaQ.CommandLine.dll fuzz
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<Product>DeltaQ</Product>
<Authors>jzebedee</Authors>
<VersionPrefix>2.0.0</VersionPrefix>
<VersionPrefix>2.1.0</VersionPrefix>
<Description>DeltaQ implementation of the suffix array induced sort (SAIS) algorithm</Description>
<PackageProjectUrl>https://github.com/jzebedee/deltaq</PackageProjectUrl>
<PackageIcon>dq.png</PackageIcon>
Expand Down
Loading

0 comments on commit 6f2a6b3

Please sign in to comment.