Coder Social home page Coder Social logo

const T& as input about cpp11 HOT 6 CLOSED

r-lib avatar r-lib commented on July 28, 2024
const T& as input

from cpp11.

Comments (6)

jimhester avatar jimhester commented on July 28, 2024

I don't really see what this extra indirection buys you though, it isn't actually a constant reference to an R object, it is a constant reference to a intermediate object. You might as well just use a copy by value object directly rather than a reference.

e.g. in Rcpp const int& is basically equivalent to

my_fun(const int&);

SEXP x_sxp;
const int x_wrap = INTEGER(x_sxp)[0];
my_fun(x_wrap);

But the semantics of that in terms of behavior and number of copies are identical to

my_fun(const int);
SEXP x_sxp;
my_fun(INTEGER(x_sxp)[0]);

As far as extensions go you should be able to define custom as_cpp() overloads. If you define them in arrow_types.h they will be added to the generated code.

from cpp11.

romainfrancois avatar romainfrancois commented on July 28, 2024

Yeah, this is more about the const-ness of the function the package developer is writing, e.g.

// [[arrow::export]]
Rcpp::List Table__to_dataframe(const std::shared_ptr<arrow::Table>& table,
                               bool use_threads) {
  int64_t nc = table->num_columns();
  int64_t nr = table->num_rows();
  Rcpp::CharacterVector names(nc);
  std::vector<std::shared_ptr<arrow::r::Converter>> converters(nc);

  for (int64_t i = 0; i < nc; i++) {
    converters[i] =
        arrow::r::Converter::Make(table->column(i)->type(), table->column(i)->chunks());
    names[i] = Rcpp::String(table->field(i)->name(), CE_UTF8);
  }

  if (use_threads) {
    return arrow::r::to_dataframe_parallel(nr, nc, names, converters);
  } else {
    return arrow::r::to_dataframe_serial(nr, nc, names, converters);
  }
}

having table as a const std::shared_ptr<arrow::Table>& here prevents me from messing with it inside the function. I can do a as_cpp<shared_ptr<T>>(SEXP from) I guess with some enable_if but I don't think I could do a as_cpp<const std::shared_ptr<T>&>().

from cpp11.

jimhester avatar jimhester commented on July 28, 2024

You can do as_cpp<const std::shared_ptr<T>>() though, e.g. without the reference.

from cpp11.

romainfrancois avatar romainfrancois commented on July 28, 2024

Fair enough. That would look different from the rest of the code base, but that's fine I guess.

Now, arrow also has a few functions that take a const std::unique_ptr<T>&, e.g.

// [[arrow::export]]
int64_t ipc___Message__body_length(const std::unique_ptr<arrow::ipc::Message>& message) {
  return message->body_length();
}

The way this is organized is that the R6 object eventually holds an external pointer to the unique_ptr, we create the unique_ptr<> once when the XPtr is created and the XPtr is responsible for deletion, but still we can use const std::unique_ptr<arrow::ipc::Message>& message in the parameters without having to copy, which is by design forbidden.

from cpp11.

romainfrancois avatar romainfrancois commented on July 28, 2024

Maybe this is "too magical" and arrow can step back to using something more explicit that would be copiable, e.g.

// [[arrow::export]]
int64_t ipc___Message__body_length(arrow::r::R6<std::unique_ptr<arrow::ipc::Message>> message) {
  return message->body_length();
}

from cpp11.

romainfrancois avatar romainfrancois commented on July 28, 2024

Closing this, arrow has to do do its own code generation anyway because of availability or not of the shared library.

from cpp11.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.