[PATCH v1] libstdc++: Improve and cleanup mdspan related code.
Tomasz Kaminski
tkaminsk@redhat.com
Fri Oct 3 07:33:16 GMT 2025
On Tue, Sep 30, 2025 at 12:57 PM Luc Grosheintz <luc.grosheintz@gmail.com>
wrote:
> The improvement is that in __index_type_cast, we don't need to check at
> runtime if we know that _IndexType is smaller than _OIndexType.
>
> The cleanup is whitespace (overlength lines) in <mdspan> and
> de-uglifying a variable in test code.
>
> libstdc++-v3/ChangeLog:
>
> * include/std/mdspan (__mdspan::__index_type_cast): Optimize by
> skipping a __glibcxx_assert if it's know at compile-time.
> * testsuite/23_containers/mdspan/int_like.h: Rename _M_i to
> value.
>
> Signed-off-by: Luc Grosheintz <luc.grosheintz@gmail.com>
> ---
>
LGTM with the two whitespace changes reverted. I can do that, and add it on
top
of mdspan series I have locally.
> libstdc++-v3/include/std/mdspan | 25 +++++++++++++------
> .../testsuite/23_containers/mdspan/int_like.h | 12 ++++-----
> 2 files changed, 23 insertions(+), 14 deletions(-)
>
> diff --git a/libstdc++-v3/include/std/mdspan
> b/libstdc++-v3/include/std/mdspan
> index d0a60b2d183..8efd168bcf0 100644
> --- a/libstdc++-v3/include/std/mdspan
> +++ b/libstdc++-v3/include/std/mdspan
> @@ -77,11 +77,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> {
> if constexpr (std::is_integral_v<_OIndexType>)
> {
> - __glibcxx_assert(cmp_less_equal(__other,
> - __gnu_cxx::__int_traits<_IndexType>::__max));
> + constexpr _IndexType __index_type_max
> + = __gnu_cxx::__int_traits<_IndexType>::__max;
> + constexpr _OIndexType __oindex_type_max
> + = __gnu_cxx::__int_traits<_OIndexType>::__max;
> +
> + if constexpr (__index_type_max < __oindex_type_max)
> + __glibcxx_assert(cmp_less_equal(__other, __index_type_max));
> +
> if constexpr (std::is_signed_v<_OIndexType>)
> - __glibcxx_assert(__other >= 0);
> - return std::move(__other);
> + __glibcxx_assert(__other >= 0);
> + return static_cast<_IndexType>(std::move(__other));
> }
> else
> {
> @@ -510,7 +516,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> __fwd_prod(const _Extents& __exts, size_t __begin, size_t __end)
> noexcept
> {
> size_t __sta_prod = [__begin, __end] {
> - span<const size_t> __sta_exts =
> __static_extents<_Extents>(__begin, __end);
> + span<const size_t> __sta_exts =
> __static_extents<_Extents>(__begin,
> +
> __end);
>
Here, I prefer slightly overlong line, our hard limit is 100, and the 80 is
more guideline,
but I think the code was more readable before.
> size_t __ret = 1;
> for(auto __ext : __sta_exts)
> if (__ext != dynamic_extent)
> @@ -726,7 +733,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>
> template<typename _Layout, typename _Mapping>
> concept __mapping_of =
> - is_same_v<typename _Layout::template mapping<typename
> _Mapping::extents_type>,
> + is_same_v<typename _Layout::template mapping<
> + typename _Mapping::extents_type>,
>
Same here.
> _Mapping>;
>
> template<template<size_t> typename _Layout, typename _Mapping>
> @@ -981,7 +989,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> template<class _RightPaddedMapping>
> requires __mdspan::__is_right_padded_mapping<_RightPaddedMapping>
> && is_constructible_v<extents_type,
> - typename
> _RightPaddedMapping::extents_type>
> + typename _RightPaddedMapping::extents_type>
> constexpr
> explicit(!is_convertible_v<typename
> _RightPaddedMapping::extents_type,
> extents_type>)
> @@ -994,7 +1002,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>
> if constexpr (__rank > 1)
> {
> - if constexpr (extents_type::static_extent(__rank - 1) !=
> dynamic_extent
> + if constexpr (extents_type::static_extent(__rank - 1)
> + != dynamic_extent
>
Same here.
>
> && __ostride_sta != dynamic_extent)
> static_assert(extents_type::static_extent(__rank - 1)
> == __ostride_sta);
> diff --git a/libstdc++-v3/testsuite/23_containers/mdspan/int_like.h
> b/libstdc++-v3/testsuite/23_containers/mdspan/int_like.h
> index 310dd8ddf20..e9172c13455 100644
> --- a/libstdc++-v3/testsuite/23_containers/mdspan/int_like.h
> +++ b/libstdc++-v3/testsuite/23_containers/mdspan/int_like.h
> @@ -15,7 +15,7 @@ template<CustomIndexKind Kind>
> public:
> explicit
> CustomIndexType(int i)
> - : _M_i(i)
> + : value(i)
> { }
>
> CustomIndexType() = delete;
> @@ -31,25 +31,25 @@ template<CustomIndexKind Kind>
> constexpr
> operator int() const noexcept
> requires (Kind == CustomIndexKind::Const)
> - { return _M_i; }
> + { return value; }
>
> constexpr
> operator int() const
> requires (Kind == CustomIndexKind::Throwing)
> - { return _M_i; }
> + { return value; }
>
> constexpr
> operator int() noexcept
> requires (Kind == CustomIndexKind::Mutating)
> - { return _M_i; }
> + { return value; }
>
> constexpr
> operator int() && noexcept
> requires (Kind == CustomIndexKind::RValue)
> - { return _M_i; }
> + { return value; }
>
> private:
> - int _M_i;
> + int value;
> };
>
> using IntLike = CustomIndexType<CustomIndexKind::Const>;
> --
> 2.50.1
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20251003/f2b9b654/attachment.htm>
More information about the Libstdc++
mailing list