Skip to content

Commit

Permalink
fixes rexyai#151
Browse files Browse the repository at this point in the history
  • Loading branch information
dselivanov committed Jun 10, 2020
1 parent 77e86aa commit 7110854
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ cpp_parse_multipart_body <- function(body, boundary) {
.Call(`_RestRserve_cpp_parse_multipart_body`, body, boundary)
}

raw_slice <- function(x, offset, size) {
.Call(`_RestRserve_raw_slice`, x, offset, size)
}

cpp_url_decode <- function(x) {
.Call(`_RestRserve_cpp_url_decode`, x)
}
Expand Down
5 changes: 3 additions & 2 deletions R/Request.R
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,9 @@ Request = R6::R6Class(
if (is.null(self$files[[name]]) || !is.raw(self$body)) {
return(NULL)
}
idx = seq_len(self$files[[name]]$length) + self$files[[name]]$offset - 1L
res = self$body[idx]
# see https://github.com/rexyai/RestRserve/issues/151
# https://stackoverflow.com/questions/56614592/faster-way-to-slice-a-raw-vector
res = raw_slice(self$body, self$files[[name]]$offset, self$files[[name]]$length)
attr(res, "filname") = self$files[[name]]$filename
attr(res, "content-type") = self$files[[name]]$content_type
return(res)
Expand Down
13 changes: 13 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// raw_slice
Rcpp::RawVector raw_slice(const Rcpp::RawVector& x, const R_xlen_t offset, const R_xlen_t size);
RcppExport SEXP _RestRserve_raw_slice(SEXP xSEXP, SEXP offsetSEXP, SEXP sizeSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::traits::input_parameter< const Rcpp::RawVector& >::type x(xSEXP);
Rcpp::traits::input_parameter< const R_xlen_t >::type offset(offsetSEXP);
Rcpp::traits::input_parameter< const R_xlen_t >::type size(sizeSEXP);
rcpp_result_gen = Rcpp::wrap(raw_slice(x, offset, size));
return rcpp_result_gen;
END_RCPP
}
// cpp_url_decode
Rcpp::CharacterVector cpp_url_decode(Rcpp::CharacterVector x);
RcppExport SEXP _RestRserve_cpp_url_decode(SEXP xSEXP) {
Expand Down Expand Up @@ -96,6 +108,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_RestRserve_cpp_parse_headers", (DL_FUNC) &_RestRserve_cpp_parse_headers, 1},
{"_RestRserve_cpp_parse_multipart_boundary", (DL_FUNC) &_RestRserve_cpp_parse_multipart_boundary, 1},
{"_RestRserve_cpp_parse_multipart_body", (DL_FUNC) &_RestRserve_cpp_parse_multipart_body, 2},
{"_RestRserve_raw_slice", (DL_FUNC) &_RestRserve_raw_slice, 3},
{"_RestRserve_cpp_url_decode", (DL_FUNC) &_RestRserve_cpp_url_decode, 1},
{"_RestRserve_cpp_url_encode", (DL_FUNC) &_RestRserve_cpp_url_encode, 1},
{NULL, NULL, 0}
Expand Down
10 changes: 10 additions & 0 deletions src/parse_multipart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,13 @@ Rcpp::List cpp_parse_multipart_body(Rcpp::RawVector body, const char* boundary)
);
return res;
}

// see https://github.com/rexyai/RestRserve/issues/151
// https://stackoverflow.com/questions/56614592/faster-way-to-slice-a-raw-vector

// [[Rcpp::export(rng=false)]]
Rcpp::RawVector raw_slice(const Rcpp::RawVector &x, const R_xlen_t offset, const R_xlen_t size) {
Rcpp::RawVector result = Rcpp::no_init(size);
memcpy ( &result[0], &x[offset - 1], size );
return result;
}
1 change: 1 addition & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ std::string str_join(const std::vector<T>&, const char*);
std::string str_join(Rcpp::CharacterVector, const char*);
template<typename T>
Rcpp::Environment map_to_env(const std::unordered_map<std::string,T>&);
Rcpp::RawVector raw_slice(const Rcpp::RawVector &x, const R_xlen_t offset, const R_xlen_t size);

#endif

0 comments on commit 7110854

Please sign in to comment.