You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "westonpace (via GitHub)" <gi...@apache.org> on 2023/03/21 22:08:16 UTC

[GitHub] [arrow] westonpace commented on a diff in pull request #14758: ARROW-18119: [C++] Utility method to ensure an array object meetings an alignment requirement

westonpace commented on code in PR #14758:
URL: https://github.com/apache/arrow/pull/14758#discussion_r1144027067


##########
cpp/src/arrow/util/align_util.h:
##########
@@ -19,6 +19,7 @@
 
 #include <algorithm>
 
+#include "arrow/memory_pool.h"

Review Comment:
   It must be getting included transitively but we should explicitly include `arrow/type_fwd.h` since we are relying on it with this new code.



##########
cpp/src/arrow/util/align_util.cc:
##########
@@ -0,0 +1,215 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "arrow/util/align_util.h"
+
+#include "arrow/array.h"
+#include "arrow/chunked_array.h"
+#include "arrow/record_batch.h"
+#include "arrow/table.h"
+
+namespace arrow {
+
+namespace util {
+
+bool CheckAlignment(const Buffer& buffer, const int64_t& alignment) {
+  return buffer.address() % alignment == 0;
+}
+
+bool CheckAlignment(const ArrayData& array, const int64_t& alignment) {
+  for (const auto& buffer : array.buffers) {
+    if (buffer) {
+      if (!CheckAlignment(*buffer, alignment)) return false;
+    }
+  }
+  return true;
+}
+
+bool CheckAlignment(const Array& array, const int64_t& alignment) {
+  bool align_dictionary = true, align_children = true;
+
+  if (array.type()->id() == Type::DICTIONARY) {
+    align_dictionary = CheckAlignment(*array.data()->dictionary, alignment);
+  }
+
+  for (const auto& child : array.data()->child_data) {
+    if (child) {
+      if (!CheckAlignment(*child, alignment)) align_children = false;
+    }
+  }
+  return CheckAlignment(*array.data(), alignment) && align_dictionary && align_children;

Review Comment:
   Why are you checking dictionary and child arrays here instead of in `bool CheckAlignment(const ArrayData& array, const int64_t& alignment)`?



##########
cpp/src/arrow/util/align_util.h:
##########
@@ -63,6 +64,43 @@ inline BitmapWordAlignParams BitmapWordAlign(const uint8_t* data, int64_t bit_of
   p.aligned_start = data + (bit_offset + p.leading_bits) / 8;
   return p;
 }
-
 }  // namespace internal
+
+namespace util {
+
+ARROW_EXPORT bool CheckAlignment(const Buffer& buffer, const int64_t& alignment);
+ARROW_EXPORT bool CheckAlignment(const ArrayData& array, const int64_t& alignment);
+ARROW_EXPORT bool CheckAlignment(const Array& array, const int64_t& alignment);
+ARROW_EXPORT bool CheckAlignment(const ChunkedArray& array, const int64_t& alignment,
+                                 std::vector<bool>& needs_alignment,
+                                 const int& offset = 0);
+ARROW_EXPORT bool CheckAlignment(const RecordBatch& batch, const int64_t& alignment,
+                                 std::vector<bool>& needs_alignment);
+ARROW_EXPORT bool CheckAlignment(const Table& table, const int64_t& alignment,
+                                 std::vector<bool>& needs_alignment);

Review Comment:
   These methods will need to be documented.  Specifically the `needs_alignment` argument is not obvious.



##########
cpp/src/arrow/util/align_util.cc:
##########
@@ -0,0 +1,215 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "arrow/util/align_util.h"
+
+#include "arrow/array.h"
+#include "arrow/chunked_array.h"
+#include "arrow/record_batch.h"
+#include "arrow/table.h"
+
+namespace arrow {
+
+namespace util {
+
+bool CheckAlignment(const Buffer& buffer, const int64_t& alignment) {
+  return buffer.address() % alignment == 0;
+}
+
+bool CheckAlignment(const ArrayData& array, const int64_t& alignment) {
+  for (const auto& buffer : array.buffers) {
+    if (buffer) {
+      if (!CheckAlignment(*buffer, alignment)) return false;
+    }
+  }
+  return true;
+}
+
+bool CheckAlignment(const Array& array, const int64_t& alignment) {
+  bool align_dictionary = true, align_children = true;
+
+  if (array.type()->id() == Type::DICTIONARY) {
+    align_dictionary = CheckAlignment(*array.data()->dictionary, alignment);
+  }
+
+  for (const auto& child : array.data()->child_data) {
+    if (child) {
+      if (!CheckAlignment(*child, alignment)) align_children = false;
+    }
+  }
+  return CheckAlignment(*array.data(), alignment) && align_dictionary && align_children;
+}
+
+bool CheckAlignment(const ChunkedArray& array, const int64_t& alignment,
+                    std::vector<bool>& needs_alignment, const int& offset) {
+  needs_alignment.resize(needs_alignment.size() + array.num_chunks(), false);
+  for (auto i = 0; i < array.num_chunks(); ++i) {
+    if (array.chunk(i) && !CheckAlignment(*array.chunk(i), alignment))
+      needs_alignment[i + offset] = true;
+  }
+  return std::find(needs_alignment.begin() + offset,

Review Comment:
   Instead of doing `std::find` at the end (which will loop through `needs_alignment`) could you do something like...
   
   ```
   bool at_least_one_needs_alignment = false;
   for (...) {
     if (...) {
       needs_alignment[i + offset] = true;
       at_least_one_needs_alignment = true;
     }
   }
   return at_least_one_needs_alignment;
   ```



##########
cpp/src/arrow/util/align_util_test.cc:
##########
@@ -145,6 +150,84 @@ TEST(BitmapWordAlign, UnalignedDataStart) {
   CheckBitmapWordAlign<8>(P, 1017, 64, {63, 1, 1080, A, 0, 0});
   CheckBitmapWordAlign<8>(P, 1017, 128, {63, 1, 1144, A + 128, 64, 1});
 }
-
 }  // namespace internal
+
+TEST(EnsureAlignment, Array) {
+  MemoryPool* pool = default_memory_pool();
+  auto rand = ::arrow::random::RandomArrayGenerator(1923);
+  auto random_array = rand.UInt8(/*size*/ 50, /*min*/ 0, /*max*/ 100,
+                                 /*null_probability*/ 0, /*alignment*/ 512, pool);
+  ASSERT_OK_AND_ASSIGN(auto aligned_array,

Review Comment:
   Maybe assert that `CheckAlignment(..., 1024)` returns `false` before you call `EnsureAlignment`?



##########
cpp/src/arrow/util/align_util.h:
##########
@@ -63,6 +64,43 @@ inline BitmapWordAlignParams BitmapWordAlign(const uint8_t* data, int64_t bit_of
   p.aligned_start = data + (bit_offset + p.leading_bits) / 8;
   return p;
 }
-
 }  // namespace internal
+
+namespace util {
+
+ARROW_EXPORT bool CheckAlignment(const Buffer& buffer, const int64_t& alignment);
+ARROW_EXPORT bool CheckAlignment(const ArrayData& array, const int64_t& alignment);
+ARROW_EXPORT bool CheckAlignment(const Array& array, const int64_t& alignment);
+ARROW_EXPORT bool CheckAlignment(const ChunkedArray& array, const int64_t& alignment,
+                                 std::vector<bool>& needs_alignment,
+                                 const int& offset = 0);

Review Comment:
   ```suggestion
   ARROW_EXPORT bool CheckAlignment(const ChunkedArray& array, const int64_t& alignment,
                                    std::vector<bool>* needs_alignment,
                                    const int& offset = 0);
   ```
   This works but the Arrow style guidelines prefer passing mutable parameters by pointer instead of mutable reference.
   
   For example:
   
   ```
   void Foo(int & value);
   void Bar(int * value);
   
   int x = 7;
   Foo(x); // BAD: not obvious that `x` is being modified
   Bar(&x); // GOOD: the `&` helps us know that `x` is being modified
   ```



##########
cpp/src/arrow/util/align_util.cc:
##########
@@ -0,0 +1,215 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "arrow/util/align_util.h"
+
+#include "arrow/array.h"
+#include "arrow/chunked_array.h"
+#include "arrow/record_batch.h"
+#include "arrow/table.h"
+
+namespace arrow {
+
+namespace util {
+
+bool CheckAlignment(const Buffer& buffer, const int64_t& alignment) {
+  return buffer.address() % alignment == 0;
+}
+
+bool CheckAlignment(const ArrayData& array, const int64_t& alignment) {
+  for (const auto& buffer : array.buffers) {
+    if (buffer) {
+      if (!CheckAlignment(*buffer, alignment)) return false;
+    }
+  }
+  return true;
+}
+
+bool CheckAlignment(const Array& array, const int64_t& alignment) {
+  bool align_dictionary = true, align_children = true;
+
+  if (array.type()->id() == Type::DICTIONARY) {
+    align_dictionary = CheckAlignment(*array.data()->dictionary, alignment);
+  }
+
+  for (const auto& child : array.data()->child_data) {
+    if (child) {
+      if (!CheckAlignment(*child, alignment)) align_children = false;

Review Comment:
   Could you just return false here and break out of the loop early?



##########
cpp/src/arrow/util/align_util.h:
##########
@@ -63,6 +64,43 @@ inline BitmapWordAlignParams BitmapWordAlign(const uint8_t* data, int64_t bit_of
   p.aligned_start = data + (bit_offset + p.leading_bits) / 8;
   return p;
 }
-
 }  // namespace internal
+
+namespace util {
+
+ARROW_EXPORT bool CheckAlignment(const Buffer& buffer, const int64_t& alignment);

Review Comment:
   ```suggestion
   ARROW_EXPORT bool CheckAlignment(const Buffer& buffer, int64_t alignment);
   ```
   Passing `int64_t` by const reference is not a good idea. Primitive / simple types should be passed by value (here and in all the other functions)



##########
cpp/src/arrow/util/align_util.cc:
##########
@@ -0,0 +1,215 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "arrow/util/align_util.h"
+
+#include "arrow/array.h"
+#include "arrow/chunked_array.h"
+#include "arrow/record_batch.h"
+#include "arrow/table.h"
+
+namespace arrow {
+
+namespace util {
+
+bool CheckAlignment(const Buffer& buffer, const int64_t& alignment) {
+  return buffer.address() % alignment == 0;
+}
+
+bool CheckAlignment(const ArrayData& array, const int64_t& alignment) {
+  for (const auto& buffer : array.buffers) {
+    if (buffer) {
+      if (!CheckAlignment(*buffer, alignment)) return false;
+    }
+  }
+  return true;
+}
+
+bool CheckAlignment(const Array& array, const int64_t& alignment) {
+  bool align_dictionary = true, align_children = true;
+
+  if (array.type()->id() == Type::DICTIONARY) {
+    align_dictionary = CheckAlignment(*array.data()->dictionary, alignment);
+  }
+
+  for (const auto& child : array.data()->child_data) {
+    if (child) {
+      if (!CheckAlignment(*child, alignment)) align_children = false;
+    }
+  }
+  return CheckAlignment(*array.data(), alignment) && align_dictionary && align_children;
+}
+
+bool CheckAlignment(const ChunkedArray& array, const int64_t& alignment,
+                    std::vector<bool>& needs_alignment, const int& offset) {
+  needs_alignment.resize(needs_alignment.size() + array.num_chunks(), false);

Review Comment:
   Why `needs_alignment.size() + array.num_chunks()` and not `offset + array.num_chunks()`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org