Skip to content

Commit cbcd256

Browse files
committed
Add bit pattern early exit
1 parent 3f74876 commit cbcd256

File tree

73 files changed

+239
-125
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+239
-125
lines changed

Docs/Optimizations.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ Input: `stable softice sophisticated santa`
5858

5959
It generates this early exit: `if ((4208UL & (1UL << (value.Length - 1))) == 0) return false;`
6060

61+
### Value bitmask early exit
62+
For integer keys, FastData can build a bitmask of all bits that appear in the dataset. If an input has any bit set that never appears in any key, it cannot be in the dataset.
63+
Input: `2 4 6 8`
64+
65+
It generates this early exit: `if ((key & 1) != 0) return false;`
66+
6167
## Structure specializations
6268

6369
### Hash table

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class MyData final {
1212
public:
1313
[[nodiscard]]
1414
static constexpr bool contains(const uint8_t key) noexcept {
15-
if (key < 42 || key > 100)
15+
if ((static_cast<uint8_t>(key) & 129) != 0)
1616
return false;
1717
if (key == 42 || key == 62 || key == 80 || key == 100)
1818
return true;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal static class MyData
1313

1414
public static bool Contains(byte key)
1515
{
16-
if (key < 42 || key > 100)
16+
if (((byte)key & 129) != 0)
1717
return false;
1818

1919
switch (key)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub struct MyData;
1313
impl MyData {
1414
#[must_use]
1515
pub fn contains(key: u8) -> bool {
16-
if key < 42 || key > 100 {
16+
if (key as u8 & 129) != 0 {
1717
return false;
1818
}
1919

Src/FastData.Generator.CPlusPlus.Tests/Features/ObjectSupportTest-ArrayStructure_Int32_3_complex.verified.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ArrayStructure_Int32_3_complex final {
2424
public:
2525
[[nodiscard]]
2626
static constexpr bool contains(const int32_t key) noexcept {
27-
if (key < 1 || key > 3)
27+
if ((static_cast<uint32_t>(key) & 4294967292u) != 0)
2828
return false;
2929

3030
for (size_t i = 0; i < 3; i++)
@@ -36,7 +36,7 @@ public:
3636
}
3737
[[nodiscard]]
3838
static bool try_lookup(const int32_t key, const Person*& value) noexcept {
39-
if (key < 1 || key > 3)
39+
if ((static_cast<uint32_t>(key) & 4294967292u) != 0)
4040
{
4141
value = nullptr;
4242
return false;

Src/FastData.Generator.CPlusPlus.Tests/Features/ObjectSupportTest-ArrayStructure_Int32_3_simple.verified.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ArrayStructure_Int32_3_simple final {
1717
public:
1818
[[nodiscard]]
1919
static constexpr bool contains(const int32_t key) noexcept {
20-
if (key < 1 || key > 3)
20+
if ((static_cast<uint32_t>(key) & 4294967292u) != 0)
2121
return false;
2222

2323
for (size_t i = 0; i < 3; i++)
@@ -29,7 +29,7 @@ public:
2929
}
3030
[[nodiscard]]
3131
static bool try_lookup(const int32_t key, const int32_t*& value) noexcept {
32-
if (key < 1 || key > 3)
32+
if ((static_cast<uint32_t>(key) & 4294967292u) != 0)
3333
{
3434
value = nullptr;
3535
return false;

Src/FastData.Generator.CPlusPlus.Tests/Features/ObjectSupportTest-BinarySearchStructure_Int32_3_complex.verified.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class BinarySearchStructure_Int32_3_complex final {
2424
public:
2525
[[nodiscard]]
2626
static constexpr bool contains(const int32_t key) noexcept {
27-
if (key < 1 || key > 3)
27+
if ((static_cast<uint32_t>(key) & 4294967292u) != 0)
2828
return false;
2929

3030
int32_t lo = 0;
@@ -46,7 +46,7 @@ public:
4646
}
4747
[[nodiscard]]
4848
static bool try_lookup(const int32_t key, const Person*& value) noexcept {
49-
if (key < 1 || key > 3)
49+
if ((static_cast<uint32_t>(key) & 4294967292u) != 0)
5050
{
5151
value = nullptr;
5252
return false;

Src/FastData.Generator.CPlusPlus.Tests/Features/ObjectSupportTest-BinarySearchStructure_Int32_3_simple.verified.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class BinarySearchStructure_Int32_3_simple final {
1717
public:
1818
[[nodiscard]]
1919
static constexpr bool contains(const int32_t key) noexcept {
20-
if (key < 1 || key > 3)
20+
if ((static_cast<uint32_t>(key) & 4294967292u) != 0)
2121
return false;
2222

2323
int32_t lo = 0;
@@ -39,7 +39,7 @@ public:
3939
}
4040
[[nodiscard]]
4141
static bool try_lookup(const int32_t key, const int32_t*& value) noexcept {
42-
if (key < 1 || key > 3)
42+
if ((static_cast<uint32_t>(key) & 4294967292u) != 0)
4343
{
4444
value = nullptr;
4545
return false;

Src/FastData.Generator.CPlusPlus.Tests/Features/ObjectSupportTest-BitSetStructure_Int32_3_complex.verified.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class BitSetStructure_Int32_3_complex final {
2323
public:
2424
[[nodiscard]]
2525
static constexpr bool contains(const int32_t key) noexcept {
26-
if (key < 1 || key > 3)
26+
if ((static_cast<uint32_t>(key) & 4294967292u) != 0)
2727
return false;
2828

2929
const uint64_t offset = static_cast<uint64_t>(key - min_key);
@@ -32,7 +32,7 @@ public:
3232
}
3333
[[nodiscard]]
3434
static bool try_lookup(const int32_t key, const Person*& value) noexcept {
35-
if (key < 1 || key > 3)
35+
if ((static_cast<uint32_t>(key) & 4294967292u) != 0)
3636
{
3737
value = nullptr;
3838
return false;

Src/FastData.Generator.CPlusPlus.Tests/Features/ObjectSupportTest-HashTablePerfectStructure_Int32_3_complex.verified.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class HashTablePerfectStructure_Int32_3_complex final {
3434
public:
3535
[[nodiscard]]
3636
static constexpr bool contains(const int32_t key) noexcept {
37-
if (key < 1 || key > 3)
37+
if ((static_cast<uint32_t>(key) & 4294967292u) != 0)
3838
return false;
3939

4040
const uint64_t hash = get_hash(key);
@@ -45,7 +45,7 @@ public:
4545
}
4646
[[nodiscard]]
4747
static bool try_lookup(const int32_t key, const Person*& value) noexcept {
48-
if (key < 1 || key > 3)
48+
if ((static_cast<uint32_t>(key) & 4294967292u) != 0)
4949
{
5050
value = nullptr;
5151
return false;

0 commit comments

Comments
 (0)