Skip to content

Commit 6e6ed16

Browse files
committed
Support prefix/suffix early exit
1 parent 110ae42 commit 6e6ed16

333 files changed

Lines changed: 1090 additions & 187 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
namespace Genbox.FastData.Benchmarks.Benchmarks;
2+
3+
/// <summary>Benchmarks where the threshold usually is between a full hash/lookup in a HashSet vs. how long a prefix/suffix early exit can be before it no longer makes sense</summary>
4+
public class EarlyExitVsLookup
5+
{
6+
private static string _myStr = "hello world";
7+
8+
private static readonly HashSet<string> _hashSet = new HashSet<string>(StringComparer.Ordinal)
9+
{
10+
_myStr
11+
};
12+
13+
[Benchmark(Baseline = true)]
14+
public bool Lookup() => _hashSet.Contains(_myStr);
15+
16+
[Benchmark]
17+
public bool Size1() => _myStr.StartsWith('h');
18+
19+
[Benchmark]
20+
public bool Size2() => _myStr.StartsWith("he", StringComparison.Ordinal);
21+
22+
[Benchmark]
23+
public bool Size3() => _myStr.StartsWith("hel", StringComparison.Ordinal);
24+
25+
[Benchmark]
26+
public bool Size4() => _myStr.StartsWith("hell", StringComparison.Ordinal);
27+
28+
[Benchmark]
29+
public bool Size5() => _myStr.StartsWith("hello", StringComparison.Ordinal);
30+
31+
[Benchmark]
32+
public bool Size6() => _myStr.StartsWith("hello ", StringComparison.Ordinal);
33+
34+
[Benchmark]
35+
public bool Size7() => _myStr.StartsWith("hello w", StringComparison.Ordinal);
36+
37+
[Benchmark]
38+
public bool Size8() => _myStr.StartsWith("hello wo", StringComparison.Ordinal);
39+
40+
[Benchmark]
41+
public bool Size9() => _myStr.StartsWith("hello wor", StringComparison.Ordinal);
42+
43+
[Benchmark]
44+
public bool Size10() => _myStr.StartsWith("hello worl", StringComparison.Ordinal);
45+
46+
[Benchmark]
47+
public bool Size11() => _myStr.StartsWith("hello world", StringComparison.Ordinal);
48+
}

Src/FastData.Cli.Tests/CommandOutputs/cpp_-k UInt8_Files_Integers.input.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public:
1414
static constexpr bool contains(const uint8_t key) noexcept {
1515
if ((static_cast<uint8_t>(key) & 129) != 0)
1616
return false;
17+
1718
if (key == 42 || key == 62 || key == 80 || key == 100)
1819
return true;
1920

Src/FastData.Cli.Tests/CommandOutputs/cpp_-s HashTable_Files_Strings.input.verified.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ class MyData final {
3939
public:
4040
[[nodiscard]]
4141
static constexpr bool contains(const std::string_view key) noexcept {
42-
if (key.length() != 5u)
43-
return false;
42+
if (key.length() != 5u)
43+
return false;
44+
4445

4546
const uint64_t hash = get_hash(key);
4647
const size_t index = hash % 2;

Src/FastData.Cli.Tests/CommandOutputs/cpp_Files_Strings.input.verified.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ class MyData final {
1212
public:
1313
[[nodiscard]]
1414
static constexpr bool contains(const std::string_view key) noexcept {
15-
if (key.length() != 5u)
16-
return false;
15+
if (key.length() != 5u)
16+
return false;
17+
1718
if (key == "test1" || key == "test2")
1819
return true;
1920

Src/FastData.Cli.Tests/CommandOutputs/csharp_-k UInt8_Files_Integers.input.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ internal static class MyData
1616
if (((byte)key & 129) != 0)
1717
return false;
1818

19+
1920
switch (key)
2021
{
2122
case 42:

Src/FastData.Cli.Tests/CommandOutputs/csharp_-s HashTable_Files_Strings.input.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ internal static class MyData
6161
if (key.Length != 5u)
6262
return false;
6363

64+
6465
ulong hash = Hash(key);
6566
uint index = (uint)(hash & 1);
6667
sbyte i = (sbyte)(_buckets[index] - 1);

Src/FastData.Cli.Tests/CommandOutputs/csharp_Files_Strings.input.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ internal static class MyData
1616
if (key.Length != 5u)
1717
return false;
1818

19+
1920
switch (key)
2021
{
2122
case "test1":

Src/FastData.Cli.Tests/CommandOutputs/rust_-k UInt8_Files_Integers.input.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ impl MyData {
1717
return false;
1818
}
1919

20+
2021
if key == 42 || key == 62 || key == 80 || key == 100 {
2122
return true;
2223
}

Src/FastData.Cli.Tests/CommandOutputs/rust_-s HashTable_Files_Strings.input.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const ENTRIES: [e; 2] = [
4646
return false;
4747
}
4848

49+
4950
let hash = unsafe { Self::get_hash(key) };
5051
let index = (hash % 2) as usize;
5152

Src/FastData.Cli.Tests/CommandOutputs/rust_Files_Strings.input.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ impl MyData {
1717
return false;
1818
}
1919

20+
2021
if key == "test1" || key == "test2" {
2122
return true;
2223
}

0 commit comments

Comments
 (0)