Skip to content

Commit

Permalink
FIFOQueue: Optimize PopRange() for trivially-copyable types
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Jul 2, 2024
1 parent 1963d80 commit da2dc5f
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/common/fifo_queue.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com>
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)

#pragma once
Expand Down Expand Up @@ -71,7 +71,7 @@ class FIFOQueue
}

// faster version of push_back_range for POD types which can be memcpy()ed
template<class Y = T, std::enable_if_t<std::is_standard_layout_v<Y>&& std::is_trivial_v<Y>, int> = 0>
template<class Y = T, std::enable_if_t<std::is_standard_layout_v<Y> && std::is_trivial_v<Y>, int> = 0>
void PushRange(const T* data, u32 size)
{
DebugAssert((m_size + size) <= CAPACITY);
Expand Down Expand Up @@ -137,10 +137,25 @@ class FIFOQueue
return val;
}

template<class Y = T, std::enable_if_t<std::is_standard_layout_v<Y> && std::is_trivial_v<Y>, int> = 0>
void PopRange(T* out_data, u32 count)
{
DebugAssert(m_size >= count);
do
{
const u32 contig_count = std::min(count, CAPACITY - m_head);
std::memcpy(out_data, &m_ptr[m_head], sizeof(T) * contig_count);
out_data += contig_count;
m_head = (m_head + contig_count) % CAPACITY;
m_size -= contig_count;
count -= contig_count;
} while (count > 0);
}

template<class Y = T, std::enable_if_t<!std::is_standard_layout_v<Y> || !std::is_trivial_v<Y>, int> = 0>
void PopRange(T* out_data, u32 count)
{
DebugAssert(m_size >= count);
for (u32 i = 0; i < count; i++)
{
out_data[i] = std::move(m_ptr[m_head]);
Expand Down

0 comments on commit da2dc5f

Please sign in to comment.