Skip to content

[C++] util: making SafeCopy call c++20's std::bit_cast #55763

In review

Description

@mapleFU

Describe the enhancement requested

See: https://en.cppreference.com/cpp/numeric/bit_cast

arrow::util::SafeCopy was originally added before Arrow adopted C++20. In #48429, it was changed to go through arrow::internal::AlignedStorage, whose get() calls arrow::internal::launder (std::launder).

In GCC (both x86_64 and AArch64, e.g., GCC 14.2 with -O3 -std=c++20), std::launder lowers to .LAUNDER(&raw_data.data_) which GCC treats as clobbering memory (missed: statement clobbers memory: _21 = .LAUNDER (&raw_data.data_);). This blocks loop auto-vectorization in hot loops using SafeCopy (for example, ALP decoding in alp_compression.cc).

Since Arrow now requires C++20 and SafeCopy's constraints (std::is_trivially_copyable_v<T> && std::is_trivially_copyable_v<U> && sizeof(T) == sizeof(U)) match std::bit_cast (which also natively supports non-default-constructible types), we can implement SafeCopy directly with std::bit_cast<std::remove_const_t<U>>(value).

Godbolt verification (GCC 14.2 x86_64 & AArch64)

Repro: https://godbolt.org/z/oedGWfPna

  • Before (AlignedStorage + std::launder):
    • missed: couldn't vectorize loop
    • missed: statement clobbers memory: _21 = .LAUNDER (&raw_data.data_);
  • After (std::bit_cast):
    • optimized: loop vectorized using 16 byte vectors

Component(s)

C++

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions