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

const modulo optimization #7750

Merged
merged 2 commits into from
Nov 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 17 additions & 2 deletions dbms/src/Functions/modulo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,23 @@ struct ModuloByConstantImpl

/// Here we failed to make the SSE variant from libdivide give an advantage.
size_t size = a.size();
for (size_t i = 0; i < size; ++i)
c[i] = a[i] - (a[i] / divider) * b; /// NOTE: perhaps, the division semantics with the remainder of negative numbers is not preserved.

/// strict aliasing optimization for char like arrays
auto * __restrict src = a.data();
auto * __restrict dst = c.data();

if (b & (b - 1))
{
for (size_t i = 0; i < size; ++i)
dst[i] = src[i] - (src[i] / divider) * b; /// NOTE: perhaps, the division semantics with the remainder of negative numbers is not preserved.
}
else
{
// gcc libdivide doesn't work well for pow2 division
auto mask = b - 1;
for (size_t i = 0; i < size; ++i)
dst[i] = src[i] & mask;
}
}
};

Expand Down
17 changes: 17 additions & 0 deletions dbms/tests/performance/modulo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<test>
<type>loop</type>

<stop_conditions>
<any_of>
<iterations>10</iterations>
</any_of>
</stop_conditions>

<main_metric>
<min_time />
</main_metric>

<query>SELECT number % 128 FROM numbers(300000000) FORMAT Null</query>
<query>SELECT number % 255 FROM numbers(300000000) FORMAT Null</query>
<query>SELECT number % 256 FROM numbers(300000000) FORMAT Null</query>
</test>