Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REVIEW] Implement span storage optimization. #515

Merged
merged 5 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions cpp/include/raft/detail/span.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,30 @@ __host__ __device__ constexpr auto lexicographical_compare(InputIt1 first1,
}
return first1 == last1 && first2 != last2;
}

template <typename T, std::size_t Extent>
struct span_storage {
private:
T* ptr_{nullptr};

public:
constexpr span_storage() noexcept = default;
constexpr span_storage(T* ptr, std::size_t) noexcept : ptr_{ptr} {}
[[nodiscard]] constexpr auto size() const noexcept -> std::size_t { return Extent; }
[[nodiscard]] constexpr auto data() const noexcept -> T* { return ptr_; }
};

template <typename T>
struct span_storage<T, dynamic_extent> {
private:
T* ptr_{nullptr};
std::size_t size_{0};

public:
constexpr span_storage() noexcept = default;
constexpr span_storage(T* ptr, std::size_t size) noexcept : ptr_{ptr}, size_{size} {}
[[nodiscard]] constexpr auto size() const noexcept -> std::size_t { return size_; }
[[nodiscard]] constexpr auto data() const noexcept -> T* { return ptr_; }
};
} // namespace detail
} // namespace raft
16 changes: 8 additions & 8 deletions cpp/include/raft/span.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,24 @@ class span {
/**
* @brief Constructs a span that is a view over the range [first, first + count);
*/
constexpr span(pointer ptr, size_type count) noexcept : size_(count), data_(ptr)
constexpr span(pointer ptr, size_type count) noexcept : storage_{ptr, count}
{
assert(!(Extent != dynamic_extent && count != Extent));
assert(ptr || count == 0);
}
/**
* @brief Constructs a span that is a view over the range [first, last)
*/
constexpr span(pointer first, pointer last) noexcept : size_(last - first), data_(first)
constexpr span(pointer first, pointer last) noexcept
: span{first, static_cast<size_type>(thrust::distance(first, last))}
trivialfis marked this conversation as resolved.
Show resolved Hide resolved
{
assert(data_ || size_ == 0);
}
/**
* @brief Constructs a span that is a view over the array arr.
*/
template <std::size_t N>
constexpr span(element_type (&arr)[N]) noexcept : size_(N), data_(&arr[0])
constexpr span(element_type (&arr)[N]) noexcept : span{&arr[0], N}
{
}

Expand All @@ -89,7 +90,7 @@ class span {
detail::is_allowed_element_type_conversion_t<U, T>::value &&
detail::is_allowed_extent_conversion_t<OtherExtent, Extent>::value>>
constexpr span(const span<U, is_device, OtherExtent>& other) noexcept
: size_(other.size()), data_(other.data())
: span{other.data(), other.size()}
{
}

Expand Down Expand Up @@ -139,10 +140,10 @@ class span {
return data()[_idx];
}

constexpr auto data() const noexcept -> pointer { return data_; }
constexpr auto data() const noexcept -> pointer { return storage_.data(); }

// Observers
[[nodiscard]] constexpr auto size() const noexcept -> size_type { return size_; }
[[nodiscard]] constexpr auto size() const noexcept -> size_type { return storage_.size(); }
[[nodiscard]] constexpr auto size_bytes() const noexcept -> size_type
{
return size() * sizeof(T);
Expand Down Expand Up @@ -197,8 +198,7 @@ class span {
}

private:
size_type size_{0};
pointer data_{nullptr};
detail::span_storage<T, Extent> storage_;
};

/**
Expand Down