Skip to content

Commit

Permalink
Add another FindFirstDiff benchmark.
Browse files Browse the repository at this point in the history
This is something I'm mostly just checking for curiosity, but scanning
DWORDs still ends up being 3x slower than the XMM words, so I intend
to keep the current strategy in place instead of trying to simplify it
at all.
  • Loading branch information
the-nerbs committed Apr 1, 2023
1 parent e5de9e8 commit 2f7867c
Showing 1 changed file with 68 additions and 1 deletion.
69 changes: 68 additions & 1 deletion Tests/MiscTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1902,7 +1902,68 @@ static std::size_t FindFirstDiff_ByteScan(
return x - xbase;
}

TEST_CASE("FindFirstDiff - benchmarks")
__declspec(noinline)
static std::size_t FindFirstDiff_DwordScan(
const std::uint8_t* x,
const std::uint8_t* y,
std::size_t len)
{
static constexpr std::size_t word_size = sizeof(std::uint32_t) - 1;

const std::uint8_t* xbase = x;
const std::uint8_t* xend = x + len;

const std::uint8_t* xalignedbase = xbase + (word_size - ((std::intptr_t)x % word_size));
const std::uint32_t* xalignedend = (const std::uint32_t*)((std::uintptr_t)xend & ~word_size);


// byte scan to DWORD alignment
while (x < xalignedbase
&& *x == *y)
{
++x;
++y;
}

if (x < xend
&& *x == *y)
{
// DWORD scan
auto xaligned = (const std::uint32_t*)x;
auto yaligned = (const std::uint32_t*)y;

while (xaligned < xalignedend
&& *xaligned == *yaligned)
{
len -= word_size;
++xaligned;
++yaligned;
}

x = (const std::uint8_t*)xaligned;
y = (const std::uint8_t*)yaligned;

if (x < xend
&& *x == *y)
{
// if there's still some bytes left, scan those too
if (len > 0)
{
while (len
&& *x == *y)
{
--len;
++x;
++y;
}
}
}
}

return x - xbase;
}

TEST_CASE("FindFirstDiff - benchmarks", "[!benchmark]")
{
constexpr std::size_t buffer_size = 4096;

Expand Down Expand Up @@ -1942,4 +2003,10 @@ TEST_CASE("FindFirstDiff - benchmarks")
{
return FindFirstDiff_ByteScan(xptr, yptr, buffer_size);
};


BENCHMARK("DWORD scanning")
{
return FindFirstDiff_DwordScan(xptr, yptr, buffer_size);
};
}

0 comments on commit 2f7867c

Please sign in to comment.