Skip to content

Commit 2311a1f

Browse files
committed
opt for pack_fixeds
update fix
1 parent e3e0590 commit 2311a1f

File tree

7 files changed

+59
-34
lines changed

7 files changed

+59
-34
lines changed

be/src/pipeline/exec/hashjoin_build_sink.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,8 @@ struct ProcessHashTableBuild {
192192
bool* has_null_key) {
193193
if (null_map) {
194194
// first row is mocked and is null
195-
// TODO: Need to test the for loop. break may better
196-
for (uint32_t i = 1; i < _rows; i++) {
197-
if ((*null_map)[i]) {
198-
*has_null_key = true;
199-
}
195+
if (simd::contain_one(null_map->data() + 1, _rows - 1)) {
196+
*has_null_key = true;
200197
}
201198
if (short_circuit_for_null && *has_null_key) {
202199
return Status::OK();
@@ -208,7 +205,7 @@ struct ProcessHashTableBuild {
208205
_rows, _batch_size, *has_null_key, hash_table_ctx.direct_mapping_range());
209206

210207
// In order to make the null keys equal when using single null eq, all null keys need to be set to default value.
211-
if (_build_raw_ptrs.size() == 1 && null_map) {
208+
if (_build_raw_ptrs.size() == 1 && null_map && *has_null_key) {
212209
_build_raw_ptrs[0]->assume_mutable()->replace_column_null_data(null_map->data());
213210
}
214211

be/src/pipeline/exec/join/process_hash_table_probe_impl.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,9 @@ typename HashTableType::State ProcessHashTableProbe<JoinOpType>::_init_probe_sid
192192
hash_table_ctx.arena.clear();
193193
// In order to make the null keys equal when using single null eq, all null keys need to be set to default value.
194194
if (_parent->_probe_columns.size() == 1 && null_map) {
195-
_parent->_probe_columns[0]->assume_mutable()->replace_column_null_data(null_map);
195+
if (simd::contain_one(null_map, probe_rows)) {
196+
_parent->_probe_columns[0]->assume_mutable()->replace_column_null_data(null_map);
197+
}
196198
}
197199

198200
hash_table_ctx.init_serialized_keys(_parent->_probe_columns, probe_rows, null_map, true,

be/src/util/simd/bits.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,5 +281,27 @@ inline size_t find_zero(const std::vector<uint8_t>& vec, size_t start) {
281281
return find_byte<uint8_t>(vec, start, 0);
282282
}
283283

284+
inline bool contain_one(const uint8_t* __restrict data, size_t size) {
285+
size_t i = 0;
286+
#if defined(__SSE2__)
287+
{
288+
const __m128i zero = _mm_setzero_si128();
289+
__m128i acc = zero;
290+
for (; i + 15 < size; i += 16) {
291+
acc = _mm_or_si128(acc, _mm_loadu_si128(reinterpret_cast<const __m128i*>(data + i)));
292+
}
293+
294+
if (_mm_movemask_epi8(_mm_cmpeq_epi8(acc, zero)) != 0xFFFF) {
295+
return true;
296+
}
297+
}
298+
#endif
299+
uint8_t tail_acc = 0;
300+
for (; i < size; ++i) {
301+
tail_acc |= data[i];
302+
}
303+
return tail_acc != 0;
304+
}
305+
284306
} // namespace doris::simd
285307
#include "common/compile_check_end.h"

be/src/vec/columns/column_decimal.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -523,10 +523,6 @@ void ColumnDecimal<T>::compare_internal(size_t rhs_row_id, const IColumn& rhs,
523523
template <PrimitiveType T>
524524
void ColumnDecimal<T>::replace_column_null_data(const uint8_t* __restrict null_map) {
525525
auto s = size();
526-
size_t null_count = s - simd::count_zero_num((const int8_t*)null_map, s);
527-
if (0 == null_count) {
528-
return;
529-
}
530526
for (size_t i = 0; i < s; ++i) {
531527
data[i] = null_map[i] ? value_type() : data[i];
532528
}

be/src/vec/columns/column_vector.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -525,10 +525,6 @@ MutableColumnPtr ColumnVector<T>::permute(const IColumn::Permutation& perm, size
525525
template <PrimitiveType T>
526526
void ColumnVector<T>::replace_column_null_data(const uint8_t* __restrict null_map) {
527527
auto s = size();
528-
size_t null_count = s - simd::count_zero_num((const int8_t*)null_map, s);
529-
if (0 == null_count) {
530-
return;
531-
}
532528
for (size_t i = 0; i < s; ++i) {
533529
data[i] = null_map[i] ? default_value() : data[i];
534530
}

be/src/vec/common/hash_table/hash_map_context.h

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <utility>
2323

2424
#include "common/compiler_util.h"
25+
#include "util/simd/bits.h"
2526
#include "vec/columns/column_array.h"
2627
#include "vec/columns/column_nullable.h"
2728
#include "vec/common/arena.h"
@@ -502,42 +503,50 @@ struct MethodKeysFixed : public MethodBase<TData> {
502503
result.clear();
503504
result.resize(row_numbers);
504505

506+
auto* __restrict result_data = reinterpret_cast<char*>(result.data());
507+
505508
size_t offset = 0;
509+
std::vector<bool> has_null_column(nullmap_columns.size(), false);
506510
if (bitmap_size > 0) {
507511
for (size_t j = 0; j < nullmap_columns.size(); j++) {
508512
if (!nullmap_columns[j]) {
509513
continue;
510514
}
511-
size_t bucket = j / BITSIZE;
512-
size_t local_offset = j % BITSIZE;
513-
const auto& data =
515+
const auto* __restrict data =
514516
assert_cast<const ColumnUInt8&>(*nullmap_columns[j]).get_data().data();
517+
518+
if (!simd::contain_one(data, row_numbers)) {
519+
continue;
520+
}
521+
has_null_column[j] = true;
522+
523+
size_t bucket = j / BITSIZE;
524+
size_t local_offset = j - bucket * BITSIZE;
525+
const auto mask = uint8_t(1 << local_offset);
526+
auto* __restrict current = result_data + bucket;
515527
for (size_t i = 0; i < row_numbers; ++i) {
516-
*((char*)(&result[i]) + bucket) |= data[i] << local_offset;
528+
*(current) |= data[i] * mask;
529+
current += sizeof(T);
517530
}
518531
}
519532
offset += bitmap_size;
520533
}
521534

522535
for (size_t j = 0; j < key_columns.size(); ++j) {
523-
const char* data = key_columns[j]->get_raw_data().data;
536+
const char* __restrict data = key_columns[j]->get_raw_data().data;
524537

525538
auto foo = [&]<typename Fixed>(Fixed zero) {
526539
CHECK_EQ(sizeof(Fixed), key_sizes[j]);
527-
if (!nullmap_columns.empty() && nullmap_columns[j]) {
528-
const auto& nullmap =
540+
if (has_null_column.size() && has_null_column[j]) {
541+
const auto* nullmap =
529542
assert_cast<const ColumnUInt8&>(*nullmap_columns[j]).get_data().data();
530-
for (size_t i = 0; i < row_numbers; ++i) {
531-
// make sure null cell is filled by 0x0
532-
memcpy_fixed<Fixed, true>(
533-
(char*)(&result[i]) + offset,
534-
nullmap[i] ? (char*)&zero : data + i * sizeof(Fixed));
535-
}
536-
} else {
537-
for (size_t i = 0; i < row_numbers; ++i) {
538-
memcpy_fixed<Fixed, true>((char*)(&result[i]) + offset,
539-
data + i * sizeof(Fixed));
540-
}
543+
// make sure null cell is filled by 0x0
544+
key_columns[j]->assume_mutable()->replace_column_null_data(nullmap);
545+
}
546+
auto* __restrict current = result_data + offset;
547+
for (size_t i = 0; i < row_numbers; ++i) {
548+
memcpy_fixed<Fixed, true>(current, data + i * sizeof(Fixed));
549+
current += sizeof(T);
541550
}
542551
};
543552

be/src/vec/core/column_with_type_and_name.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <sstream>
2828
#include <string>
2929

30+
#include "util/simd/bits.h"
3031
#include "vec/columns/column.h"
3132
#include "vec/columns/column_const.h"
3233
#include "vec/columns/column_nothing.h"
@@ -128,7 +129,9 @@ ColumnWithTypeAndName ColumnWithTypeAndName::unnest_nullable(
128129
const auto& null_map = source_column->get_null_map_data();
129130
// only need to mutate nested column, avoid to copy nullmap
130131
auto mutable_nested_col = (*std::move(nested_column)).mutate();
131-
mutable_nested_col->replace_column_null_data(null_map.data());
132+
if (simd::contain_one(null_map.data(), null_map.size())) {
133+
mutable_nested_col->replace_column_null_data(null_map.data());
134+
}
132135

133136
return {std::move(mutable_nested_col), nested_type, ""};
134137
}

0 commit comments

Comments
 (0)