skarupke / flat_hash_map

A very fast hashtable
1.69k stars 186 forks source link

optimized clear for trivially_destructable entries #31

Closed p-groarke closed 5 years ago

p-groarke commented 5 years ago

Since distance_from_desired is int8_t, this works as expected.

private:
    inline void clear(std::true_type)
    {
        std::memset(entries, -1, num_elements * sizeof(Entry));
        num_elements = 0;
    }

    inline void clear(std::false_type)
    {
        for (EntryPointer it = entries, end = it + static_cast<ptrdiff_t>(num_slots_minus_one + max_lookups); it != end;
                ++it)
        {
            if (it->has_value())
                it->destroy_value();
        }
        num_elements = 0;
    }

public:
    void clear()
    {
        this->clear(std::is_trivially_destructible<T>::type{});
    }