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 2 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, typename SizeType, size_t Extent>
trivialfis marked this conversation as resolved.
Show resolved Hide resolved
struct span_storage {
private:
T* ptr_{nullptr};

public:
constexpr span_storage() = default;
constexpr span_storage(T* ptr, SizeType) : ptr_{ptr} {}
[[nodiscard]] constexpr auto size() const -> SizeType { return Extent; }
constexpr auto data() const -> T* { return ptr_; }
trivialfis marked this conversation as resolved.
Show resolved Hide resolved
};

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

public:
constexpr span_storage() = default;
constexpr span_storage(T* ptr, SizeType size) : ptr_{ptr}, size_{size} {}
[[nodiscard]] constexpr auto size() const -> SizeType { return size_; }
constexpr auto data() const -> T* { return ptr_; }
trivialfis marked this conversation as resolved.
Show resolved Hide resolved
};
} // 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
: storage_{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 : storage_{&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())
: storage_{other.data(), other.size()}
trivialfis marked this conversation as resolved.
Show resolved Hide resolved
{
}

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, size_type, Extent> storage_;
};

/**
Expand Down