Repository navigation
GH-51834: [C++] Reduce ARROW_ASSIGN_OR_RAISE stack consumption with gcc/clang - #53314
Conversation
|
|
|
The gcc/clang specialization looks good to me. I checked the two things this expansion could have quietly broken, and both come out clean:
clang at |
HuaHuaY
left a comment
There was a problem hiding this comment.
While discussing this PR with my LLM, I unexpectedly discovered an issue. The GCC documentation https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html contains the following statement:
In G++, the result value of a statement expression undergoes array and function pointer decay, and is returned by value to the enclosing expression.
This extended syntax constructs a temporary value. Issues arise when encountering const Result<T>. Consider the following two code snippets:
arrow::Status Repro() {
const arrow::Result<std::string> result = std::string(128, 'x');
ARROW_ASSIGN_OR_RAISE(std::string_view view, result);
std::printf("%c\n", view.front());
return arrow::Status::OK();
}In the first code snippet, the string_view references a temporary value created via extended syntax, resulting in a dangling reference.
const arrow::Result<std::unique_ptr<int>> result = std::make_unique<int>(42);
ARROW_ASSIGN_OR_RAISE(const auto& value, result);The second code snippet fails to compile because unique_ptr cannot be copied via value semantics.
There is a TRY macro for C++23's std::expected on Reddit that is similar to our implementation, and I found that it encounters the same issue. I consider this issue acceptable.
I think you are correct that our Warnings do not currently cover this case explicitly: If I understand correctly, the issue arises in your example, because the IMO this is implicitly covered by this statement
Because the caller provides a |
| source](std::shared_ptr<ipc::RecordBatchFileReader> reader) | ||
| -> Future<std::shared_ptr<ipc::RecordBatchFileReader>> { | ||
| ARROW_ASSIGN_OR_RAISE(auto options, | ||
| ARROW_ASSIGN_OR_RAISE(auto read_options, |
There was a problem hiding this comment.
Well, because we use auto here, we got a compile-time error. Otherwise, rexpr would use the lhs's declaration as an uninitialized object.
Even when keeping in mind that
auto options = GetReadOptions(*reader->schema(), *self, *options)).ValueUnsafe();
would obviously already have been broken before, this breakage is definitely something to consider, given that it can introduce potentially subtle bugs in broken but working consumers of the macro
|
@github-actions crossbow submit -g cpp |
|
Revision: f96d9fa Submitted crossbow builds: ursacomputing/crossbow @ actions-9760cf6651 |
|
I'm going to merge now. Thanks @taepper for submitting this PR! |
Rationale for this change
@pitrou found in investigations that repeated uses of
ARROW_ASSIGN_OR_RAISEcan lead to very large stack consumptions.What changes are included in this PR?
This PR adds a new definition of the
ARROW_ASSIGN_OR_RAISEmacro, which avoids intermediate results using a gcc/clang compiler extension.Are these changes tested?
No new feature, all tests still pass.
Are there any user-facing changes?
Yes. The
ARROW_ASSIGN_OR_RAISEmacro is a public interface and now has a breaking change:If
lhsis a declaration, it will already be in scope for the body of the right-hand-side. This lead to one breakage in the arrow code-base:which needed manual fixing. This could also break users of the C++ library
Was AI used for this PR?
In accordance to the AI generation guidelines, please disclose below whether and how AI was used in this PR.
PR code and description written by:
Reviewed before submission by: