Conversation
| const xmlChar* uri = (xmlChar*) Rf_translateCharUTF8(STRING_ELT(x_sxp, i)); | ||
| SET_STRING_ELT(out, i, Xml2String(xmlBuildURI(uri, base_uri)).asRString()); | ||
| const xmlChar* uri = (xmlChar*) Rf_translateCharUTF8(x_sxp[i]); | ||
| out[i] = Xml2String(xmlBuildURI(uri, base_uri)).asRString(); |
There was a problem hiding this comment.
I guess the slow down due to cpp11 protection probably doesn't matter so much here (https://cpp11.r-lib.org/articles/FAQ.html#ok-but-i-really-want-to-call-cpp11unwind_protect-manually).
There was a problem hiding this comment.
Yea if it isn't critical then I'd probably leave as is
| for (int i = 0; i < n; ++i) { | ||
| const xmlChar* uri = (xmlChar*) Rf_translateCharUTF8(STRING_ELT(x_sxp, i)); | ||
| SET_STRING_ELT(out, i, Xml2String(xmlBuildURI(uri, base_uri)).asRString()); | ||
| const xmlChar* uri = (xmlChar*) Rf_translateCharUTF8(x_sxp[i]); |
There was a problem hiding this comment.
Probably we can somehow leave away the Rf_translateCharUTF8() due to using cpp11.
There was a problem hiding this comment.
Hmm, I don't think cpp11 attempts to convert to UTF8 in this case
- When
x_sxpis created ascpp11::strings, it just wraps the character vector SEXP with no translation on the elements - When
x_sxp[i]runs, it extracts the CHARSXP and turns it into ar_string, but that just calls this very simple constructor https://github.com/r-lib/cpp11/blob/500f642b4ea132ec8c168fc70a28e81e9510ece3/inst/include/cpp11/r_string.hpp#L17C10-L17C10 - On the way into
Rf_translateCharUTF8()ther_stringis automatically changed back to a SEXP by this operator, which also doesn't translate https://github.com/r-lib/cpp11/blob/500f642b4ea132ec8c168fc70a28e81e9510ece3/inst/include/cpp11/r_string.hpp#L22
It looks like if we went through an intermediate std::string it would translate, but what you currently have seems ok
https://github.com/r-lib/cpp11/blob/500f642b4ea132ec8c168fc70a28e81e9510ece3/inst/include/cpp11/r_string.hpp#L29
src/xml2_url.cpp
Outdated
| if (Rf_xlength(base_sxp) > 1) { | ||
| Rf_error("Base URL must be length 1"); | ||
| const xmlChar* to_xml_chr(cpp11::strings x, const char* arg) { | ||
| if (x.size() > 1) { |
There was a problem hiding this comment.
Might make more sense to check this in R.
src/connection.h
Outdated
| SEXP read_bin(SEXP con, size_t bytes = 64 * 1024); | ||
| SEXP write_bin(SEXP data, SEXP con); | ||
|
|
||
| inline SEXP R_GetConnection(SEXP con) { return con; } |
There was a problem hiding this comment.
I removed R_GetConnection() as it seemed unnecessary.
src/connection.h
Outdated
|
|
||
| inline SEXP R_GetConnection(SEXP con) { return con; } | ||
|
|
||
| inline size_t R_ReadConnection(SEXP con, void* buf, size_t n) { |
There was a problem hiding this comment.
R_ReadConnection() wasn't used anywhere.
src/connection.h
Outdated
| } | ||
| cpp11::sexp write_bin(cpp11::sexp data, cpp11::sexp con); | ||
|
|
||
| inline size_t R_WriteConnection(SEXP con, void* buf, size_t n) { |
There was a problem hiding this comment.
This uses memcpy() and I wasn't sure how to do this properly with cpp11.
There was a problem hiding this comment.
I don't think there is an alternative to memcpy(), but you could allocate a cpp11 raws and then do payload.sexp() to get access to the SEXP you can call RAW() on. I don't think that is too awful, and at least lets cpp11 manage the memory
src/connection.h
Outdated
| } | ||
| cpp11::sexp write_bin(cpp11::sexp data, cpp11::sexp con); | ||
|
|
||
| inline size_t R_WriteConnection(SEXP con, void* buf, size_t n) { |
There was a problem hiding this comment.
I don't think there is an alternative to memcpy(), but you could allocate a cpp11 raws and then do payload.sexp() to get access to the SEXP you can call RAW() on. I don't think that is too awful, and at least lets cpp11 manage the memory
src/xml2_namespace.cpp
Outdated
| extern "C" SEXP unique_ns(SEXP ns) { | ||
| [[cpp11::register]] | ||
| cpp11::sexp unique_ns(SEXP ns) { | ||
| BEGIN_CPP |
There was a problem hiding this comment.
And in the other places below. Do we need this macro at all anymore? I assume if you go through cpp11 everywhere it will wrap it in try/catch for you
There was a problem hiding this comment.
@DavisVaughan If you could merge #400 then I could also finish xml2_node.cpp and I think then it should be safe to remove the macros.
| for (int i = 0; i < n; ++i) { | ||
| const xmlChar* uri = (xmlChar*) Rf_translateCharUTF8(STRING_ELT(x_sxp, i)); | ||
| SET_STRING_ELT(out, i, Xml2String(xmlBuildURI(uri, base_uri)).asRString()); | ||
| const xmlChar* uri = (xmlChar*) Rf_translateCharUTF8(x_sxp[i]); |
There was a problem hiding this comment.
Hmm, I don't think cpp11 attempts to convert to UTF8 in this case
- When
x_sxpis created ascpp11::strings, it just wraps the character vector SEXP with no translation on the elements - When
x_sxp[i]runs, it extracts the CHARSXP and turns it into ar_string, but that just calls this very simple constructor https://github.com/r-lib/cpp11/blob/500f642b4ea132ec8c168fc70a28e81e9510ece3/inst/include/cpp11/r_string.hpp#L17C10-L17C10 - On the way into
Rf_translateCharUTF8()ther_stringis automatically changed back to a SEXP by this operator, which also doesn't translate https://github.com/r-lib/cpp11/blob/500f642b4ea132ec8c168fc70a28e81e9510ece3/inst/include/cpp11/r_string.hpp#L22
It looks like if we went through an intermediate std::string it would translate, but what you currently have seems ok
https://github.com/r-lib/cpp11/blob/500f642b4ea132ec8c168fc70a28e81e9510ece3/inst/include/cpp11/r_string.hpp#L29
| const xmlChar* uri = (xmlChar*) Rf_translateCharUTF8(STRING_ELT(x_sxp, i)); | ||
| SET_STRING_ELT(out, i, Xml2String(xmlBuildURI(uri, base_uri)).asRString()); | ||
| const xmlChar* uri = (xmlChar*) Rf_translateCharUTF8(x_sxp[i]); | ||
| out[i] = Xml2String(xmlBuildURI(uri, base_uri)).asRString(); |
There was a problem hiding this comment.
Yea if it isn't critical then I'd probably leave as is
src/xml2_url.cpp
Outdated
| SET_STRING_ELT(scheme, i, Rf_mkChar(uri->scheme == NULL ? "" : uri->scheme)); | ||
| SET_STRING_ELT(server, i, Rf_mkChar(uri->server == NULL ? "" : uri->server)); | ||
| INTEGER(port)[i] = uri->port == 0 ? NA_INTEGER : uri->port; | ||
| SET_STRING_ELT(user, i, Rf_mkChar(uri->user == NULL ? "" : uri->user)); | ||
| SET_STRING_ELT(path, i, Rf_mkChar(uri->path == NULL ? "" : uri->path)); | ||
| SET_STRING_ELT(fragment, i, Rf_mkChar(uri->fragment == NULL ? "" : uri->fragment)); | ||
| scheme[i] = uri->scheme == NULL ? "" : uri->scheme; | ||
| server[i] = uri->server == NULL ? "" : uri->server; | ||
| port[i] = uri->port == 0 ? NA_INTEGER : uri->port; | ||
| user[i] = uri->user == NULL ? "" : uri->user; | ||
| path[i] = uri->path == NULL ? "" : uri->path; | ||
| fragment[i] = uri->fragment == NULL ? "" : uri->fragment; |
There was a problem hiding this comment.
This one might get significantly slower, but again it doesn't seem like it is used all that often? I couldn't really say
There was a problem hiding this comment.
I also think it shouldn't be used that often.
#Conflicts: # R/xml2-package.R # src/init.c
|
@DavisVaughan I now also migrated |
|
Is it one or two functions in particular? |
|
I haven't looked into this in more detail but only checked |
8f62602 to
9c3fc65
Compare
463c7e9 to
ece90bb
Compare
88024a3 to
f9c5fe3
Compare
memcpy()?xml2_node.cppcan be tackled after Replace S3 dispatch by C implementation #400 is merged.