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

Putting Rope on a diet #12

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion benchmarks/AddRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class AddRange
[Params(10, 100, 500)]
public int EditCount;

[Benchmark(Description = "Rope")]
[Benchmark(Description = "Rope", Baseline = true)]
public void RopeOfChar()
{
var lorem = BenchmarkData.LoremIpsum.ToRope();
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/AddRangeImmutable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class AddRangeImmutable
[Params(10, 100)]
public int EditCount;

[Benchmark(Description = "Rope")]
[Benchmark(Description = "Rope", Baseline = true)]
public void RopeOfChar()
{
var lorem = BenchmarkData.LoremIpsum.ToRope();
Expand Down
4 changes: 3 additions & 1 deletion benchmarks/Benchmarks.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
<TargetFrameworks>net8.0;netstandard2.1</TargetFrameworks>
<OutputType>Exe</OutputType>
</PropertyGroup>
<PropertyGroup>
Expand All @@ -15,6 +15,8 @@
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.13.12" />
</ItemGroup>
<ItemGroup Condition="!$(TargetFramework.StartsWith('netstandard'))">
<PackageReference Include="DiffMatchPatch" Version="3.0.0" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/CreateNewEmpty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Benchmarks;
[MemoryDiagnoser]
public class CreateNewEmpty
{
[Benchmark(Description = "Rope<char>.Empty")]
[Benchmark(Description = "Rope<char>.Empty", Baseline = true)]
public void EmptyRopeOfChar() => _ = Rope<char>.Empty;

[Benchmark(Description = "new List<char>()")]
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/CreateNewWithLength10.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class CreateNewWithLength10
{
private char[] array = new[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

[Benchmark(Description = "string.ToRope()")]
[Benchmark(Description = "string.ToRope()", Baseline = true)]
public void RopeOfCharFromString() => _ = "0123456789".ToRope();

[Benchmark(Description = "new Rope<char>(array)")]
Expand Down
27 changes: 7 additions & 20 deletions benchmarks/DiffLineHashing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,14 @@ public class DiffLineHashing
[Benchmark]
public void LinesToCharsPure()
{
var b = new LinesToCharsBenchTest();
b.Run();
}

private class LinesToCharsBenchTest
{
public LinesToCharsBenchTest() : base()
{
}

public void Run()
// More than 65536 to verify any 16-bit limitation.
var lineList = Rope<char>.Empty;
for (int i = 0; i < 66000; i++)
{
// More than 65536 to verify any 16-bit limitation.
var lineList = Rope<char>.Empty;
for (int i = 0; i < 66000; i++)
{
lineList = lineList.AddRange((i + "\n").AsMemory());
}

lineList = lineList.ToMemory();
var result = lineList.DiffChunksToChars(Rope<char>.Empty, DiffOptions<char>.LineLevel);
lineList = lineList.AddRange((i + "\n").AsMemory());
}

lineList = lineList.ToMemory();
var result = lineList.DiffChunksToChars(Rope<char>.Empty, DiffOptions<char>.LineLevel);
}
}
12 changes: 7 additions & 5 deletions benchmarks/DiffOnLongText.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2010 Google Inc.
// All Right Reserved.
#if NET8_0_OR_GREATER

using BenchmarkDotNet.Attributes;
using DiffMatchPatch;
Expand All @@ -11,18 +12,19 @@ namespace Benchmarks;
[MemoryDiagnoser]
public class DiffOnLongText
{
[Benchmark]
DiffOptions<char> options = DiffOptions<char>.LineLevel with { TimeoutSeconds = 0 };
diff_match_patch diff = new diff_match_patch() { Diff_Timeout = 0 };

[Benchmark(Baseline = true)]
public void RopeOfCharDiff()
{
var options = DiffOptions<char>.LineLevel with { TimeoutSeconds = 0 };
_ = BenchmarkData.LongDiffText1.Diff(BenchmarkData.LongDiffText2, options);
}

[Benchmark]
public void DiffMatchPatchDiff()
{
var diff = new diff_match_patch();
diff.Diff_Timeout = 0;
diff.diff_main(BenchmarkData.LongDiffText1String, BenchmarkData.LongDiffText2String);
}
}
}
#endif
3 changes: 3 additions & 0 deletions benchmarks/DiffOnShortText.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2010 Google Inc.
// All Right Reserved.
#if NET8_0_OR_GREATER

using System;
using BenchmarkDotNet.Attributes;
Expand Down Expand Up @@ -40,3 +41,5 @@ public void DiffMatchPatchDiff()
this.DiffMatchPatch.diff_main(BenchmarkData.ShortDiffText1, BenchmarkData.ShortDiffText2);
}
}

#endif
25 changes: 16 additions & 9 deletions benchmarks/Equals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,59 @@

namespace Benchmarks;

/// <summary>
/// This benchmark performs a successful equality and then a negative equality of various lengths of text.
/// </summary>
[MemoryDiagnoser]
public class Equals
{
[Params(10, 100, 1000, 10000)]
[Params(2, 3, 10, 100, 1000, 10000)]
public int Length;

private Rope<char> ropeX;
private Rope<char> ropeY;
private Rope<char> ropeZ;

private StringBuilder? sbX;
private ReadOnlyMemory<char> sbY;
private ReadOnlyMemory<char> sbZ;

private string? strX;
private string? strY;

private ImmutableArray<char> arrayX;

private ImmutableArray<char> arrayY;
private string? strZ;

[GlobalSetup]
public void Setup()
{
this.ropeX = BenchmarkData.LoremIpsum.ToRope().Slice(Length);
this.ropeY = BenchmarkData.LoremIpsum.ToRope().Slice(Length);
this.sbY = BenchmarkData.LoremIpsum[..Length].AsMemory();
this.ropeZ = BenchmarkData.LoremIpsum.ToRope().Slice(1, Length - 1);
this.sbX = new StringBuilder(BenchmarkData.LoremIpsum[..Length]);
this.sbY = BenchmarkData.LoremIpsum[..Length].AsMemory();
this.sbZ = BenchmarkData.LoremIpsum[1..(Length - 1)].AsMemory();
this.strX = BenchmarkData.LoremIpsum[..Length];
this.strY = BenchmarkData.LoremIpsum[..Length];
this.arrayX = ImmutableArray<char>.Empty.AddRange(BenchmarkData.LoremIpsum[..Length].AsSpan());
this.arrayY = ImmutableArray<char>.Empty.AddRange(BenchmarkData.LoremIpsum[..Length].AsSpan());
this.strZ = BenchmarkData.LoremIpsum[1..(Length - 1)];
}

[Benchmark(Description = "Rope<char>")]
[Benchmark(Description = "Rope<char>", Baseline = true)]
public void RopeOfChar()
{
_ = this.ropeX.Equals(this.ropeY);
_ = this.ropeX.Equals(this.ropeZ);
}

[Benchmark(Description = "StringBuilder")]
public void StringBuilder()
{
_ = this.sbX!.Equals(this.sbY.Span);
_ = this.sbX!.Equals(this.sbZ.Span);
}

[Benchmark(Description = "string")]
public void String()
{
_ = this.strX!.Equals(strY);
_ = this.strX!.Equals(strZ);
}
}
2 changes: 1 addition & 1 deletion benchmarks/GetHashCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class GetHashCode
[Params(10, 100, 1000, 10000)]
public int Length;

[Benchmark(Description = "Rope<char>")]
[Benchmark(Description = "Rope<char>", Baseline = true)]
public void RopeOfChar()
{
_ = BenchmarkData.LoremIpsum.ToRope().Slice(Length).GetHashCode();
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/IndexOf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class IndexOf
[Params(10, 100, 1000, 10000)]
public int Length;

[Benchmark(Description = "Rope")]
[Benchmark(Description = "Rope", Baseline = true)]
public void RopeOfChar()
{
_ = BenchmarkData.LongDiffText1[..this.Length].IndexOf(Find);
Expand Down
14 changes: 7 additions & 7 deletions benchmarks/InsertRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public class InsertRange
[Params(10, 100, 1000)]
public int EditCount;

[Benchmark]
public void RopeOfChar()
[Benchmark(Baseline = true)]
public Rope<char> RopeOfChar()
{
var lorem = BenchmarkData.LoremIpsum.ToRope();
var s = lorem;
Expand All @@ -21,11 +21,11 @@ public void RopeOfChar()
s = s.InsertRange(321, lorem);
}

////s.ToString();
return s;
}

[Benchmark]
public void ListOfChar()
public List<char> ListOfChar()
{
var lorem = BenchmarkData.LoremIpsum.ToCharArray();
var s = new List<char>(lorem);
Expand All @@ -34,18 +34,18 @@ public void ListOfChar()
s.InsertRange(321, lorem);
}

////s.ToString();
return s;
}

[Benchmark]
public void StringBuilder()
public StringBuilder StringBuilder()
{
var s = new StringBuilder(BenchmarkData.LoremIpsum);
for (int i = 0; i < EditCount; i++)
{
s.Insert(321, BenchmarkData.LoremIpsum);
}

////s.ToString();
return s;
}
}
30 changes: 16 additions & 14 deletions benchmarks/InsertSorted.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if NET8_0_OR_GREATER
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -15,16 +16,16 @@ public class InsertSorted

private static readonly float[] RandomFloats = Enumerable.Range(0, 65000).Select(s => random.NextSingle()).ToArray();

[Benchmark]
public void AddRangeThenOrderLong()
[Benchmark(Baseline = true)]
public long AddRangeThenOrderLong()
{
var rope = Rope<long>.Empty.AddRange(RandomLongs);
var comparer = Comparer<long>.Default;
_ = rope.Order(comparer).First();
return rope.Order(comparer).First();
}

[Benchmark]
public void InsertSortedLong()
public long InsertSortedLong()
{
var rope = Rope<long>.Empty;
var comparer = Comparer<long>.Default;
Expand All @@ -33,11 +34,11 @@ public void InsertSortedLong()
rope = rope.InsertSorted(RandomLongs[i], comparer);
}

_ = rope[0];
return rope[0];
}

[Benchmark]
public void PriorityQueueOfLong()
public long PriorityQueueOfLong()
{
var comparer = Comparer<long>.Default;
var queue = new PriorityQueue<long, long>(comparer);
Expand All @@ -46,19 +47,19 @@ public void PriorityQueueOfLong()
queue.Enqueue(i, RandomLongs[i]);
}

queue.Dequeue();
return queue.Dequeue();
}

[Benchmark]
public void AddRangeThenOrderFloat()
public float AddRangeThenOrderFloat()
{
var rope = Rope<float>.Empty.AddRange(RandomFloats);
var comparer = Comparer<float>.Default;
_ = rope.Order(comparer).First();
return rope.Order(comparer).First();
}

[Benchmark]
public void InsertSortedFloat()
public float InsertSortedFloat()
{
var rope = Rope<float>.Empty;
var comparer = Comparer<float>.Default;
Expand All @@ -67,11 +68,11 @@ public void InsertSortedFloat()
rope = rope.InsertSorted(RandomFloats[i], comparer);
}

_ = rope[0];
return rope[0];
}

[Benchmark]
public void PriorityQueueOfFloat()
public float PriorityQueueOfFloat()
{
var comparer = Comparer<float>.Default;
var queue = new PriorityQueue<long, float>(comparer);
Expand All @@ -80,6 +81,7 @@ public void PriorityQueueOfFloat()
queue.Enqueue(i, RandomFloats[i]);
}

queue.Dequeue();
return queue.Dequeue();
}
}
}
#endif
2 changes: 1 addition & 1 deletion benchmarks/LastIndexOf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class LastIndexOf

private static readonly Rope<char> Find = "[[New Haven Register]]".ToRope();

[Benchmark(Description = "Rope<char>")]
[Benchmark(Description = "Rope<char>", Baseline = true)]
public void RopeOfChar()
{
_ = BenchmarkData.LongDiffText1.LastIndexOf(Find);
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/SplitThenConcat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class SplitThenConcat
[Params(10, 100, 1000)]
public int EditCount;

[Benchmark]
[Benchmark(Baseline = true)]
public void RopeOfChar()
{
var lorem = BenchmarkData.LoremIpsum.ToRope();
Expand Down
Binary file modified benchmarks/results/Benchmarks.AddRange-barplot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading