C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Efficient and silent bounds checking with silent_at()

From: trtaab trtaab <tvfvof_at_[hidden]>
Date: Thu, 6 Jul 2023 01:35:27 +0000
Dear Proposal Forum,

In response to your request for a clearer explanation of the proposed "silent_at" method and its significance, I would like to provide a concrete example to illustrate its implementation and compare it to the existing "at()" method.

Consider the following example implementation of the "silent_at" method:

inline constexpr T& silent_at(std::size_t index) noexcept
{
    if (len <= index)
    {
        __builtin_trap(); // Program crash in a controlled manner
    }
    return ptr[index];
}

In this implementation, the "silent_at" method performs bounds checking by comparing the specified index to the length of the container. If the index is greater than or equal to the length, indicating an out-of-bounds access, the program crashes using __builtin_trap(). Since the method is marked as noexcept, the compiler is free to optimize it as needed, including reordering the trap.

Now, let's compare this with the existing implementation of the "at()" method in the standard library, which involves more complex operations:

using size_type = std::size_t;

inline constexpr size_type size() const noexcept
{
    return len;
}

_GLIBCXX20_CONSTEXPR
void _M_range_check(size_type __n) const
{
    if (__n >= this->size())
        ::std::__throw_out_of_range_fmt(__N("myspan::_M_range_check: __n "
                                            "(which is %zu) >= this->size() "
                                            "(which is %zu)"),
                                        __n, this->size());
}

inline constexpr T& at(std::size_t index)
{
    _M_range_check(index);
    return ptr[index];
}
In the existing implementation, the "at()" method performs bounds checking through the _M_range_check function. It compares the index to the size of the container and throws an exception (__throw_out_of_range_fmt) if an out-of-bounds access is detected. This mechanism involves additional code and exception handling, which can impact performance and optimizations.

By introducing the "silent_at" method, we provide an alternative approach that allows for efficient bounds checking without the overhead and potential performance implications of exceptions. The "silent_at" method ensures controlled program termination through a crash mechanism, such as __builtin_trap(), in the event of an out-of-bounds access.

The simplicity of the "silent_at" implementation, combined with its no-exception nature, allows for improved performance and optimization opportunities. It offers a compromise between the unchecked "operator[]" method and the exception-throwing "at()" method in terms of safety, efficiency, and debuggability.

I hope this example clarifies the proposed "silent_at" method and its benefits. Please let me know if you require any further details or examples to support this proposal.

Thank you for your attention and consideration.

Sincerely,

Shengdun Wang
tvfvof_at_[hidden]
Sincerely,

Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows


Received on 2023-07-06 01:35:30