You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2021/06/10 08:09:31 UTC

[GitHub] [arrow] pitrou commented on a change in pull request #10445: ARROW-9140: [R] Zero-copy Arrow to R where possible

pitrou commented on a change in pull request #10445:
URL: https://github.com/apache/arrow/pull/10445#discussion_r648947602



##########
File path: r/src/altrep.cpp
##########
@@ -0,0 +1,180 @@
+// 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 <cpp11/altrep.hpp>
+
+#include "./arrow_types.h"
+
+#if defined(HAS_ALTREP)
+
+#include <R_ext/Altrep.h>
+#include <arrow/array.h>
+
+namespace arrow {
+namespace r {
+
+struct array_nonull {
+  // altrep object around an Array with no nulls
+  // data1: an external pointer to a shared pointer to the Array
+  // data2: not used
+
+  static SEXP Make(R_altrep_class_t class_t, const std::shared_ptr<Array>& array) {
+    // we don't need the whole r6 object, just an external pointer
+    // that retain the array
+    cpp11::external_pointer<std::shared_ptr<Array>> xp(new std::shared_ptr<Array>(array));
+
+    SEXP res = R_new_altrep(class_t, xp, R_NilValue);
+    MARK_NOT_MUTABLE(res);
+
+    return res;
+  }
+
+  static Rboolean Inspect(SEXP x, int pre, int deep, int pvec,
+                          void (*inspect_subtree)(SEXP, int, int, int)) {
+    auto& array = Get(x);
+    Rprintf("std::shared_ptr<arrow::Array, %s, NONULL> (len=%d, ptr=%p)\n",
+            array->type()->ToString().c_str(), array->length(), array.get());
+    return TRUE;
+  }
+
+  static std::shared_ptr<Array>& Get(SEXP vec) {
+    return *cpp11::external_pointer<std::shared_ptr<Array>>(R_altrep_data1(vec));
+  }
+
+  static R_xlen_t Length(SEXP vec) { return Get(vec)->length(); }
+
+  static const void* Dataptr_or_null(SEXP vec) {
+    auto& array = Get(vec);
+
+    int size = array->type_id() == Type::INT32 ? sizeof(int) : sizeof(double);
+    return reinterpret_cast<const void*>(array->data()->buffers[1]->data() +
+                                         size * array->offset());

Review comment:
       Or `array->data()->GetValues<c_type>(1)` where `using c_type = typename ArrowType::c_type`.
   (`ArrowType` being the class template parameter, e.g. `Int64Type` or `DoubleType`)

##########
File path: r/src/altrep.cpp
##########
@@ -0,0 +1,180 @@
+// 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 <cpp11/altrep.hpp>
+
+#include "./arrow_types.h"
+
+#if defined(HAS_ALTREP)
+
+#include <R_ext/Altrep.h>
+#include <arrow/array.h>
+
+namespace arrow {
+namespace r {
+
+struct array_nonull {
+  // altrep object around an Array with no nulls
+  // data1: an external pointer to a shared pointer to the Array
+  // data2: not used
+
+  static SEXP Make(R_altrep_class_t class_t, const std::shared_ptr<Array>& array) {
+    // we don't need the whole r6 object, just an external pointer
+    // that retain the array
+    cpp11::external_pointer<std::shared_ptr<Array>> xp(new std::shared_ptr<Array>(array));
+
+    SEXP res = R_new_altrep(class_t, xp, R_NilValue);
+    MARK_NOT_MUTABLE(res);
+
+    return res;
+  }
+
+  static Rboolean Inspect(SEXP x, int pre, int deep, int pvec,
+                          void (*inspect_subtree)(SEXP, int, int, int)) {
+    auto& array = Get(x);
+    Rprintf("std::shared_ptr<arrow::Array, %s, NONULL> (len=%d, ptr=%p)\n",
+            array->type()->ToString().c_str(), array->length(), array.get());
+    return TRUE;
+  }
+
+  static std::shared_ptr<Array>& Get(SEXP vec) {
+    return *cpp11::external_pointer<std::shared_ptr<Array>>(R_altrep_data1(vec));
+  }
+
+  static R_xlen_t Length(SEXP vec) { return Get(vec)->length(); }
+
+  static const void* Dataptr_or_null(SEXP vec) {
+    auto& array = Get(vec);
+
+    int size = array->type_id() == Type::INT32 ? sizeof(int) : sizeof(double);

Review comment:
       Is it possible to make this a template class instead of manually switching on the type at runtime?

##########
File path: r/src/altrep.cpp
##########
@@ -0,0 +1,180 @@
+// 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 <cpp11/altrep.hpp>
+
+#include "./arrow_types.h"
+
+#if defined(HAS_ALTREP)
+
+#include <R_ext/Altrep.h>
+#include <arrow/array.h>
+
+namespace arrow {
+namespace r {
+
+struct array_nonull {
+  // altrep object around an Array with no nulls
+  // data1: an external pointer to a shared pointer to the Array
+  // data2: not used
+
+  static SEXP Make(R_altrep_class_t class_t, const std::shared_ptr<Array>& array) {
+    // we don't need the whole r6 object, just an external pointer
+    // that retain the array
+    cpp11::external_pointer<std::shared_ptr<Array>> xp(new std::shared_ptr<Array>(array));
+
+    SEXP res = R_new_altrep(class_t, xp, R_NilValue);
+    MARK_NOT_MUTABLE(res);
+
+    return res;
+  }
+
+  static Rboolean Inspect(SEXP x, int pre, int deep, int pvec,
+                          void (*inspect_subtree)(SEXP, int, int, int)) {
+    auto& array = Get(x);
+    Rprintf("std::shared_ptr<arrow::Array, %s, NONULL> (len=%d, ptr=%p)\n",
+            array->type()->ToString().c_str(), array->length(), array.get());
+    return TRUE;
+  }
+
+  static std::shared_ptr<Array>& Get(SEXP vec) {

Review comment:
       Return a const reference?

##########
File path: r/src/array_to_vector.cpp
##########
@@ -1275,7 +1276,22 @@ cpp11::writable::list to_dataframe_parallel(
 
 // [[arrow::export]]
 SEXP Array__as_vector(const std::shared_ptr<arrow::Array>& array) {
-  return arrow::r::ArrayVector__as_vector(array->length(), array->type(), {array});
+  auto type = array->type();
+
+#if defined(HAS_ALTREP)
+  if (array->null_count() == 0 && arrow::r::GetBoolOption("arrow.use_altrep", true)) {
+    switch (type->id()) {
+      case arrow::Type::DOUBLE:
+        return arrow::r::Make_array_nonull_dbl_vector(array);
+      case arrow::Type::INT32:
+        return arrow::r::Make_array_nonull_int_vector(array);
+      default:

Review comment:
       Is there a plan to support other types here?

##########
File path: r/src/altrep.cpp
##########
@@ -0,0 +1,180 @@
+// 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 <cpp11/altrep.hpp>
+
+#include "./arrow_types.h"
+
+#if defined(HAS_ALTREP)
+
+#include <R_ext/Altrep.h>
+#include <arrow/array.h>
+
+namespace arrow {
+namespace r {
+
+struct array_nonull {
+  // altrep object around an Array with no nulls
+  // data1: an external pointer to a shared pointer to the Array
+  // data2: not used
+
+  static SEXP Make(R_altrep_class_t class_t, const std::shared_ptr<Array>& array) {
+    // we don't need the whole r6 object, just an external pointer
+    // that retain the array
+    cpp11::external_pointer<std::shared_ptr<Array>> xp(new std::shared_ptr<Array>(array));
+
+    SEXP res = R_new_altrep(class_t, xp, R_NilValue);
+    MARK_NOT_MUTABLE(res);
+
+    return res;
+  }
+
+  static Rboolean Inspect(SEXP x, int pre, int deep, int pvec,
+                          void (*inspect_subtree)(SEXP, int, int, int)) {
+    auto& array = Get(x);
+    Rprintf("std::shared_ptr<arrow::Array, %s, NONULL> (len=%d, ptr=%p)\n",
+            array->type()->ToString().c_str(), array->length(), array.get());
+    return TRUE;
+  }
+
+  static std::shared_ptr<Array>& Get(SEXP vec) {
+    return *cpp11::external_pointer<std::shared_ptr<Array>>(R_altrep_data1(vec));
+  }
+
+  static R_xlen_t Length(SEXP vec) { return Get(vec)->length(); }
+
+  static const void* Dataptr_or_null(SEXP vec) {
+    auto& array = Get(vec);
+
+    int size = array->type_id() == Type::INT32 ? sizeof(int) : sizeof(double);
+    return reinterpret_cast<const void*>(array->data()->buffers[1]->data() +
+                                         size * array->offset());
+  }
+
+  static SEXP Duplicate(SEXP vec, Rboolean) {
+    auto& array = Get(vec);
+    auto size = array->length();
+
+    bool int_array = array->type_id() == Type::INT32;
+
+    SEXP copy = PROTECT(Rf_allocVector(int_array ? INTSXP : REALSXP, array->length()));
+
+    memcpy(DATAPTR(copy), Dataptr_or_null(vec),
+           int_array ? (size * sizeof(int)) : (size * sizeof(double)));
+
+    UNPROTECT(1);
+    return copy;
+  }
+
+  static void* Dataptr(SEXP vec, Rboolean writeable) {
+    return const_cast<void*>(Dataptr_or_null(vec));
+  }
+
+  // by definition, there are no NA
+  static int No_NA(SEXP vec) { return 1; }
+
+  static void Init(R_altrep_class_t class_t, DllInfo* dll) {
+    // altrep
+    R_set_altrep_Length_method(class_t, array_nonull::Length);
+    R_set_altrep_Inspect_method(class_t, array_nonull::Inspect);
+    R_set_altrep_Duplicate_method(class_t, array_nonull::Duplicate);
+
+    // altvec
+    R_set_altvec_Dataptr_method(class_t, array_nonull::Dataptr);
+    R_set_altvec_Dataptr_or_null_method(class_t, array_nonull::Dataptr_or_null);
+  }
+};
+
+struct array_nonull_dbl_vector {
+  static R_altrep_class_t class_t;
+
+  static void Init(DllInfo* dll) {
+    class_t = R_make_altreal_class("array_nonull_dbl_vector", "arrow", dll);
+    array_nonull::Init(class_t, dll);
+    R_set_altreal_No_NA_method(class_t, array_nonull::No_NA);
+  }
+};
+
+struct array_nonull_int_vector {
+  static R_altrep_class_t class_t;
+
+  static void Init(DllInfo* dll) {
+    class_t = R_make_altinteger_class("array_nonull_int_vector", "arrow", dll);
+    array_nonull::Init(class_t, dll);
+    R_set_altinteger_No_NA_method(class_t, array_nonull::No_NA);
+  }
+};
+
+R_altrep_class_t array_nonull_int_vector::class_t;
+R_altrep_class_t array_nonull_dbl_vector::class_t;
+
+void Init_Altrep_classes(DllInfo* dll) {
+  array_nonull_dbl_vector::Init(dll);
+  array_nonull_int_vector::Init(dll);
+}
+
+SEXP Make_array_nonull_dbl_vector(const std::shared_ptr<Array>& array) {
+  return array_nonull::Make(array_nonull_dbl_vector::class_t, array);
+}
+
+SEXP Make_array_nonull_int_vector(const std::shared_ptr<Array>& array) {
+  return array_nonull::Make(array_nonull_int_vector::class_t, array);
+}
+
+}  // namespace r
+}  // namespace arrow
+
+// TODO: when arrow depends on R 3.5 we can eliminate this

Review comment:
       What does this mean? Does Arrow still support R versions before 3.5? @nealrichardson 

##########
File path: r/tests/testthat/test-altrep.R
##########
@@ -0,0 +1,52 @@
+# 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.
+
+context("altrep")
+
+skip_if(getRversion() <= "3.5.0")
+
+test_that("altrep vectors from int32 arrays with no nulls", {
+  withr::local_options(list(arrow.use_altrep = TRUE))
+  v <- Array$create(1:1000)
+  expect_true(is_altrep_int_nonull(as.vector(v)))
+  expect_true(is_altrep_int_nonull(as.vector(v$Slice(1))))
+
+  withr::local_options(list(arrow.use_altrep = NULL))
+  v <- Array$create(1:1000)

Review comment:
       Can you also test with an empty array?

##########
File path: r/src/altrep.cpp
##########
@@ -0,0 +1,180 @@
+// 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 <cpp11/altrep.hpp>
+
+#include "./arrow_types.h"
+
+#if defined(HAS_ALTREP)
+
+#include <R_ext/Altrep.h>
+#include <arrow/array.h>
+
+namespace arrow {
+namespace r {
+
+struct array_nonull {
+  // altrep object around an Array with no nulls
+  // data1: an external pointer to a shared pointer to the Array
+  // data2: not used
+
+  static SEXP Make(R_altrep_class_t class_t, const std::shared_ptr<Array>& array) {
+    // we don't need the whole r6 object, just an external pointer
+    // that retain the array
+    cpp11::external_pointer<std::shared_ptr<Array>> xp(new std::shared_ptr<Array>(array));
+
+    SEXP res = R_new_altrep(class_t, xp, R_NilValue);
+    MARK_NOT_MUTABLE(res);
+
+    return res;
+  }
+
+  static Rboolean Inspect(SEXP x, int pre, int deep, int pvec,
+                          void (*inspect_subtree)(SEXP, int, int, int)) {
+    auto& array = Get(x);
+    Rprintf("std::shared_ptr<arrow::Array, %s, NONULL> (len=%d, ptr=%p)\n",
+            array->type()->ToString().c_str(), array->length(), array.get());
+    return TRUE;
+  }
+
+  static std::shared_ptr<Array>& Get(SEXP vec) {
+    return *cpp11::external_pointer<std::shared_ptr<Array>>(R_altrep_data1(vec));
+  }
+
+  static R_xlen_t Length(SEXP vec) { return Get(vec)->length(); }
+
+  static const void* Dataptr_or_null(SEXP vec) {
+    auto& array = Get(vec);

Review comment:
       Nit, but should use `const auto&` when mutation is not intended.

##########
File path: r/tests/testthat/test-altrep.R
##########
@@ -0,0 +1,52 @@
+# 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.
+
+context("altrep")
+
+skip_if(getRversion() <= "3.5.0")
+
+test_that("altrep vectors from int32 arrays with no nulls", {
+  withr::local_options(list(arrow.use_altrep = TRUE))
+  v <- Array$create(1:1000)
+  expect_true(is_altrep_int_nonull(as.vector(v)))
+  expect_true(is_altrep_int_nonull(as.vector(v$Slice(1))))
+
+  withr::local_options(list(arrow.use_altrep = NULL))
+  v <- Array$create(1:1000)
+  expect_true(is_altrep_int_nonull(as.vector(v)))
+  expect_true(is_altrep_int_nonull(as.vector(v$Slice(1))))
+
+  withr::local_options(list(arrow.use_altrep = FALSE))
+  expect_false(is_altrep_int_nonull(as.vector(v)))
+  expect_false(is_altrep_int_nonull(as.vector(v$Slice(1))))
+})
+
+test_that("altrep vectors from double arrays with no nulls", {

Review comment:
       Can you add tests for when there are nulls?

##########
File path: r/src/altrep.cpp
##########
@@ -0,0 +1,180 @@
+// 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 <cpp11/altrep.hpp>
+
+#include "./arrow_types.h"
+
+#if defined(HAS_ALTREP)
+
+#include <R_ext/Altrep.h>
+#include <arrow/array.h>
+
+namespace arrow {
+namespace r {
+
+struct array_nonull {

Review comment:
       Is there a reason for the `lower_case` naming style? Note our C++ style guide asks for `CamelCase` function and class names.




-- 
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.

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