Skip to content

Commit

Permalink
prefetch agg data
Browse files Browse the repository at this point in the history
Signed-off-by: guo-shaoge <[email protected]>
  • Loading branch information
guo-shaoge committed Dec 20, 2024
1 parent 6750737 commit 2e94829
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions dbms/src/AggregateFunctions/IAggregateFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,20 +235,42 @@ class IAggregateFunctionHelper : public IAggregateFunction
Arena * arena,
ssize_t if_argument_pos = -1) const override
{
const auto end = start_offset + batch_size;
static constexpr size_t prefetch_step = 16;
if (if_argument_pos >= 0)
{
const auto & flags = assert_cast<const ColumnUInt8 &>(*columns[if_argument_pos]).getData();
for (size_t i = start_offset; i < start_offset + batch_size; ++i)
for (size_t i = start_offset; i < end; ++i)
{
if (flags[i] && places[i - start_offset])
static_cast<const Derived *>(this)->add(places[i - start_offset] + place_offset, columns, i, arena);
const auto place_idx = i - start_offset;
const auto prefetch_idx = place_idx + prefetch_step;

if (flags[i] && places[place_idx])
{
// Only prefetch first agg function data for simplicity.
// This may not work when there are too many agg functions.
if likely (prefetch_idx < end)
__builtin_prefetch(places[prefetch_idx]);

static_cast<const Derived *>(this)->add(places[place_idx] + place_offset, columns, i, arena);
}
}
}
else
{
for (size_t i = start_offset; i < start_offset + batch_size; ++i)
if (places[i - start_offset])
static_cast<const Derived *>(this)->add(places[i - start_offset] + place_offset, columns, i, arena);
for (size_t i = start_offset; i < end; ++i)
{
const auto place_idx = i - start_offset;
const auto prefetch_idx = place_idx + prefetch_step;

if (places[place_idx])
{
if likely (prefetch_idx < end)
__builtin_prefetch(places[prefetch_idx]);

static_cast<const Derived *>(this)->add(places[place_idx] + place_offset, columns, i, arena);
}
}
}
}

Expand Down

0 comments on commit 2e94829

Please sign in to comment.