decster commented on a change in pull request #3462: URL: https://github.com/apache/incubator-doris/pull/3462#discussion_r419842777
########## File path: be/src/olap/memory/hash_index.cpp ########## @@ -0,0 +1,161 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include <emmintrin.h> +#include <stdio.h> +#include <algorithm> +#include "gutil/stringprintf.h" +#include "olap/memory/hash_index.h" + +namespace doris { + +using TagVector = __m128i; + +struct alignas(64) HashChunk { + static const uint32_t CAPACITY = 12; + uint8_t tags[12]; + std::atomic<uint32_t> size; + uint32_t values[12]; + + TagVector* tagVector() { + return (TagVector*)tags; + } + + const std::string dump() const { + std::string ret; + StringPrintf("["); + for (uint32_t i=0;i<std::min((uint32_t)size, (uint32_t)12);i++) { + printf("%6u(%02x)", values[i], (uint32_t)tags[i]); + } + printf("]\n"); + return ret; + } +}; + +const uint64_t HashIndex::npos; + +HashIndex::HashIndex(size_t capacity) : + _size(0), _max_size(0), _num_chunks(0), _chunk_mask(0), + _chunks(NULL) { + size_t min_chunk = (capacity * 14 / 12 + HashChunk::CAPACITY - 1) / HashChunk::CAPACITY; + if (min_chunk == 0) { + return; + } + size_t nc = 1; + while (nc < min_chunk) { + nc *= 2; + } + _chunks = (HashChunk*)aligned_malloc(nc*64, 64); + if (_chunks) { + _num_chunks = nc; + _chunk_mask = nc - 1; + memset(_chunks, 0, _num_chunks*64); + _max_size = _num_chunks * HashChunk::CAPACITY * 12 / 14; + } +} + +HashIndex::~HashIndex() { + if (_chunks) { + free(_chunks); + _chunks = 0; + _size = 0; + _max_size = 0; + _num_chunks = 0; + _chunk_mask = 0; + } +} + +uint64_t HashIndex::find(uint64_t key_hash, std::vector<uint32_t> &entries) const { + uint64_t tag = key_hash & 0xff; + if (tag == 0) { + tag = 1; + } + uint64_t pos = (key_hash >> 8) & _chunk_mask; + uint64_t orig_pos = pos; + auto tests = _mm_set1_epi8(static_cast<uint8_t>(tag)); + while (true) { + HashChunk& chunk = _chunks[pos]; + uint32_t sz = chunk.size.load(std::memory_order_acquire); + auto tags = _mm_load_si128(chunk.tagVector()); + auto eqs = _mm_cmpeq_epi8(tags, tests); + uint32_t mask = _mm_movemask_epi8(eqs) & 0xfff; + while (mask != 0) { + uint32_t i = __builtin_ctz(mask); + mask &= (mask -1); + entries.emplace_back(chunk.values[i]); + } + if (sz == HashChunk::CAPACITY) { + uint64_t step = tag*2+1; // 1; + pos = (pos + step) & _chunk_mask; + if (pos == orig_pos) { + return npos; + } + } else { + return (pos << 4) | sz; + } + } +} + +void HashIndex::set(uint64_t slot, uint64_t key_hash, uint32_t value) { + uint64_t pos = slot >> 4; + uint64_t tpos = slot & 0xf; + HashChunk& chunk = _chunks[pos]; + uint64_t tag = key_hash & 0xff; + if (tag == 0) { + tag = 1; + } + chunk.tags[tpos] = tag; + chunk.values[tpos] = value; + if (tpos == chunk.size) { + chunk.size.store(tpos+1, std::memory_order_release); Review comment: fixed, remove load/save entirely ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org