Skip to content

[C++] Excessive stack consumption with ARROW_ASSIGN_OR_RAISE #51834

Description

@pitrou

Describe the bug, including details regarding any error messages, version, and platform.

While investigating some of the CI failures in #51758, it came to me that each use of ARROW_ASSIGN_OR_RAISE added stack consumption through an additional uniquely-named local variable to hold the temporary Result<T>.

A lexical sequence of ARROW_ASSIGN_OR_RAISE will therefore consume N times the necessary stack space, as this sample code shows:

using BigObject = std::array<uint8_t, 128>;

ARROW_NOINLINE Result<BigObject> ReturnSomethingBig() { return BigObject{}; }

ARROW_NOINLINE Status PrintStack(int depth = 0, uint8_t* last_stack = nullptr) {
  uint8_t x;
  if (last_stack) {
    ARROW_LOG(INFO) << "Depth " << depth << " -> stack: " << (void*)(&x)
                    << " -> frame size: " << last_stack - &x;
  } else {
    ARROW_LOG(INFO) << "Depth " << depth << " -> stack: " << (void*)(&x);
  }
  ARROW_ASSIGN_OR_RAISE(auto obj, ReturnSomethingBig());
  ARROW_ASSIGN_OR_RAISE(obj, ReturnSomethingBig());
  ARROW_ASSIGN_OR_RAISE(obj, ReturnSomethingBig());
  ARROW_ASSIGN_OR_RAISE(obj, ReturnSomethingBig());
  ARROW_ASSIGN_OR_RAISE(obj, ReturnSomethingBig());
  ARROW_ASSIGN_OR_RAISE(obj, ReturnSomethingBig());
  ARROW_ASSIGN_OR_RAISE(obj, ReturnSomethingBig());
  ARROW_ASSIGN_OR_RAISE(obj, ReturnSomethingBig());
  ARROW_UNUSED(obj);
  if (depth < 10) {
    RETURN_NOT_OK(PrintStack(depth + 1, &x));
  }
  return Status::OK();
}

TEST(ResultTest, StackConsumption) { ASSERT_OK(PrintStack()); }

On my machine in debug mode this prints:

[ RUN      ] ResultTest.StackConsumption
/home/antoine/arrow/dev/cpp/src/arrow/result_test.cc:842: Depth 0 -> stack: 0x7ffc1d874bdf
/home/antoine/arrow/dev/cpp/src/arrow/result_test.cc:839: Depth 1 -> stack: 0x7ffc1d87455f -> frame size: 1664
/home/antoine/arrow/dev/cpp/src/arrow/result_test.cc:839: Depth 2 -> stack: 0x7ffc1d873edf -> frame size: 1664
/home/antoine/arrow/dev/cpp/src/arrow/result_test.cc:839: Depth 3 -> stack: 0x7ffc1d87385f -> frame size: 1664
/home/antoine/arrow/dev/cpp/src/arrow/result_test.cc:839: Depth 4 -> stack: 0x7ffc1d8731df -> frame size: 1664
/home/antoine/arrow/dev/cpp/src/arrow/result_test.cc:839: Depth 5 -> stack: 0x7ffc1d872b5f -> frame size: 1664
/home/antoine/arrow/dev/cpp/src/arrow/result_test.cc:839: Depth 6 -> stack: 0x7ffc1d8724df -> frame size: 1664
/home/antoine/arrow/dev/cpp/src/arrow/result_test.cc:839: Depth 7 -> stack: 0x7ffc1d871e5f -> frame size: 1664
/home/antoine/arrow/dev/cpp/src/arrow/result_test.cc:839: Depth 8 -> stack: 0x7ffc1d8717df -> frame size: 1664
/home/antoine/arrow/dev/cpp/src/arrow/result_test.cc:839: Depth 9 -> stack: 0x7ffc1d87115f -> frame size: 1664
/home/antoine/arrow/dev/cpp/src/arrow/result_test.cc:839: Depth 10 -> stack: 0x7ffc1d870adf -> frame size: 1664

Ideally the frame size should be not much more than 128 bytes in this example.

This matters when doing recursive calls, because stack consumption can be much higher than expected.

Component(s)

C++

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions