[Perf] (MG_Util/Misc): Reimplement IndexGenerator.

This commit is contained in:
BZLZHH
2025-10-02 14:22:03 +08:00
parent 19a965c1ef
commit 000a2ba3a7
+49 -57
View File
@@ -5,82 +5,74 @@ namespace MobileGL {
template <typename IndexType> template <typename IndexType>
class IndexGenerator { class IndexGenerator {
public: public:
explicit IndexGenerator(SizeT initial_capacity = 1024, IndexType first_index = 1) { static_assert(std::is_integral<IndexType>::value && std::is_unsigned<IndexType>::value,
const SizeT words_needed = (initial_capacity + 63) / 64; "IndexType must be an unsigned integral type.");
is_valid_.resize(words_needed, ~0ull);
freed_indices_.reserve(initial_capacity); using SizeT = size_t;
std::vector<IndexType> valuesBeforeFirst(first_index);
std::iota(valuesBeforeFirst.begin(), valuesBeforeFirst.end(), 0); explicit IndexGenerator(SizeT initial_capacity = 1024, IndexType first_index = 1)
Generate(valuesBeforeFirst.size(), valuesBeforeFirst.data()); : m_first_index(first_index), m_next_index(first_index), m_active_count(0) {
m_free_list.reserve(initial_capacity);
SizeT initial_bits_size = std::max(static_cast<SizeT>(first_index), initial_capacity);
m_valid_bits.resize(initial_bits_size, false);
} }
void Generate(SizeT n, IndexType* indices) { void Generate(SizeT n, IndexType* indices) noexcept {
if (n == 0) return; if (n == 0) return;
SizeT from_freed = std::min(n, freed_indices_.size()); const SizeT from_free_list_count = std::min(n, m_free_list.size());
if (from_freed > 0) { if (from_free_list_count > 0) {
std::copy_n(freed_indices_.end() - from_freed, from_freed, indices); const auto free_list_start = m_free_list.end() - from_free_list_count;
freed_indices_.resize(freed_indices_.size() - from_freed); std::copy(free_list_start, m_free_list.end(), indices);
m_free_list.erase(free_list_start, m_free_list.end());
for (SizeT i = 0; i < from_freed; ++i) { for (SizeT i = 0; i < from_free_list_count; ++i) {
SetValid(indices[i], true); m_valid_bits[indices[i]] = true;
} }
} }
SizeT need_new = n - from_freed; const SizeT new_indices_count = n - from_free_list_count;
if (need_new > 0) { if (new_indices_count > 0) {
SizeT required_index = next_index_ + need_new; const IndexType required_size = m_next_index + new_indices_count;
SizeT required_words = (required_index + 63) / 64; if (required_size > m_valid_bits.size()) {
if (required_words > is_valid_.size()) { m_valid_bits.resize(std::max(required_size, static_cast<IndexType>(m_valid_bits.size() * 1.5)));
is_valid_.resize(required_words, ~0ull);
} }
for (SizeT i = 0; i < need_new; ++i) { IndexType* new_indices_ptr = indices + from_free_list_count;
indices[from_freed + i] = next_index_++; for (SizeT i = 0; i < new_indices_count; ++i) {
const IndexType new_index = m_next_index++;
new_indices_ptr[i] = new_index;
m_valid_bits[new_index] = true;
} }
} }
m_active_count += n;
} }
void Delete(IndexType index) { void Delete(IndexType index) noexcept {
SetValid(index, false); if (!IsValid(index)) {
freed_indices_.push_back(index); return;
if (freed_indices_.size() > 1024 && freed_indices_.size() > next_index_ / 2) {
CompactFreeList();
} }
m_valid_bits[index] = false;
m_free_list.push_back(index);
m_active_count--;
} }
Bool IsValid(IndexType index) const { bool IsValid(IndexType index) const noexcept { return index < m_valid_bits.size() && m_valid_bits[index]; }
SizeT word = index >> 6;
SizeT bit = index & 0x3F;
return word < is_valid_.size() && (is_valid_[word] & (1ull << bit));
}
SizeT FreeListSize() const { return freed_indices_.size(); } SizeT FreeListSize() const noexcept { return m_free_list.size(); }
SizeT ActiveCount() const { return next_index_ - freed_indices_.size(); }
SizeT ActiveCount() const noexcept { return m_active_count; }
private: private:
inline void SetValid(IndexType index, Bool valid) { IndexType m_first_index;
SizeT word = index >> 6; IndexType m_next_index;
uint64_t mask = 1ull << (index & 0x3F); SizeT m_active_count;
std::vector<IndexType> m_free_list;
if (word >= is_valid_.size()) return; std::vector<bool> m_valid_bits;
if (valid) {
is_valid_[word] |= mask;
} else {
is_valid_[word] &= ~mask;
}
}
void CompactFreeList() {
std::sort(freed_indices_.begin(), freed_indices_.end());
auto last = std::unique(freed_indices_.begin(), freed_indices_.end());
freed_indices_.erase(last, freed_indices_.end());
}
private:
IndexType next_index_ = 0;
std::vector<IndexType> freed_indices_;
std::vector<Uint64> is_valid_;
}; };
} // namespace MobileGL } // namespace MobileGL