Skip to content

Commit

Permalink
efficiencies by avoiding STRING_ELT
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Jul 22, 2024
1 parent d490eaf commit 58531b3
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -503,10 +503,10 @@ void nano_encode(nano_buf *enc, const SEXP object) {
R_xlen_t i;
size_t slen, outlen = 0;
for (i = 0; i < xlen; i++)
outlen += strlen(CHAR(STRING_ELT(object, i))) + 1;
outlen += strlen(NANO_STR_N(object, i)) + 1;
NANO_ALLOC(enc, outlen);
for (i = 0; i < xlen; i++) {
s = CHAR(STRING_ELT(object, i));
s = NANO_STR_N(object, i);
slen = strlen(s) + 1;
memcpy(enc->buf + enc->cur, s, slen);
enc->cur += slen;
Expand Down Expand Up @@ -595,7 +595,7 @@ int nano_matcharg(const SEXP mode) {
int nano_matchargs(const SEXP mode) {

if (TYPEOF(mode) != INTSXP) {
const char *mod = CHAR(STRING_ELT(mode, XLENGTH(mode) == 9));
const char *mod = NANO_STR_N(mode, XLENGTH(mode) == 9);
size_t slen = strlen(mod);
switch (slen) {
case 1:
Expand Down
1 change: 1 addition & 0 deletions src/nanonext.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ typedef struct nano_handle_s {
#define NANO_DATAPTR(x) (void *) DATAPTR_RO(x)
#define NANO_VECTOR(x) ((const SEXP *) DATAPTR_RO(x))
#define NANO_STRING(x) CHAR(*((const SEXP *) DATAPTR_RO(x)))
#define NANO_STR_N(x, n) CHAR(((const SEXP *) DATAPTR_RO(x))[n])
#define NANO_INTEGER(x) *(int *) DATAPTR_RO(x)
#define NANO_ERROR(x) { Rf_error(x); return R_NilValue; }

Expand Down
18 changes: 9 additions & 9 deletions src/ncurl.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ SEXP rnng_ncurl(SEXP http, SEXP convert, SEXP follow, SEXP method, SEXP headers,
if (TYPEOF(hnames) == STRSXP && XLENGTH(hnames) == hlen) {
for (R_xlen_t i = 0; i < hlen; i++) {
if ((xc = nng_http_req_set_header(req,
CHAR(STRING_ELT(hnames, i)),
CHAR(STRING_ELT(headers, i)))))
NANO_STR_N(hnames, i),
NANO_STR_N(headers, i))))
goto exitlevel4;
}
}
Expand Down Expand Up @@ -262,7 +262,7 @@ SEXP rnng_ncurl(SEXP http, SEXP convert, SEXP follow, SEXP method, SEXP headers,
SET_VECTOR_ELT(out, 1, rvec);
Rf_namesgets(rvec, response);
for (R_xlen_t i = 0; i < rlen; i++) {
const char *r = nng_http_res_get_header(res, CHAR(STRING_ELT(response, i)));
const char *r = nng_http_res_get_header(res, NANO_STR_N(response, i));
SET_VECTOR_ELT(rvec, i, r == NULL ? R_NilValue : Rf_mkString(r));
}
} else {
Expand Down Expand Up @@ -343,8 +343,8 @@ SEXP rnng_ncurl_aio(SEXP http, SEXP convert, SEXP method, SEXP headers, SEXP dat
if (TYPEOF(hnames) == STRSXP && XLENGTH(hnames) == hlen) {
for (R_xlen_t i = 0; i < hlen; i++) {
if ((xc = nng_http_req_set_header(handle->req,
CHAR(STRING_ELT(hnames, i)),
CHAR(STRING_ELT(headers, i)))))
NANO_STR_N(hnames, i),
NANO_STR_N(headers, i))))
goto exitlevel4;
}
}
Expand Down Expand Up @@ -476,7 +476,7 @@ static SEXP rnng_aio_http_impl(SEXP env, const int typ) {
PROTECT(rvec = Rf_allocVector(VECSXP, rlen));
Rf_namesgets(rvec, response);
for (R_xlen_t i = 0; i < rlen; i++) {
const char *r = nng_http_res_get_header(handle->res, CHAR(STRING_ELT(response, i)));
const char *r = nng_http_res_get_header(handle->res, NANO_STR_N(response, i));
SET_VECTOR_ELT(rvec, i, r == NULL ? R_NilValue : Rf_mkString(r));
}
UNPROTECT(1);
Expand Down Expand Up @@ -557,8 +557,8 @@ SEXP rnng_ncurl_session(SEXP http, SEXP convert, SEXP method, SEXP headers, SEXP
if (TYPEOF(hnames) == STRSXP && XLENGTH(hnames) == hlen) {
for (R_xlen_t i = 0; i < hlen; i++) {
if ((xc = nng_http_req_set_header(handle->req,
CHAR(STRING_ELT(hnames, i)),
CHAR(STRING_ELT(headers, i)))))
NANO_STR_N(hnames, i),
NANO_STR_N(headers, i))))
goto exitlevel4;
}
}
Expand Down Expand Up @@ -670,7 +670,7 @@ SEXP rnng_ncurl_transact(SEXP session) {
SET_VECTOR_ELT(out, 1, rvec);
Rf_namesgets(rvec, response);
for (R_xlen_t i = 0; i < rlen; i++) {
const char *r = nng_http_res_get_header(handle->res, CHAR(STRING_ELT(response, i)));
const char *r = nng_http_res_get_header(handle->res, NANO_STR_N(response, i));
SET_VECTOR_ELT(rvec, i, r == NULL ? R_NilValue : Rf_mkString(r));
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ SEXP rnng_tls_config(SEXP client, SEXP server, SEXP pass, SEXP auth) {
if ((usefile = Rf_xlength(client)) > 0) {
file = NANO_STRING(client);
if (usefile > 1)
crl = CHAR(STRING_ELT(client, 1));
crl = NANO_STR_N(client, 1);
if ((xc = nng_tls_config_alloc(&cfg, NNG_TLS_MODE_CLIENT)))
goto exitlevel1;
if ((xc = nng_tls_config_auth_mode(cfg, mod)))
Expand All @@ -315,7 +315,7 @@ SEXP rnng_tls_config(SEXP client, SEXP server, SEXP pass, SEXP auth) {
file = NANO_STRING(server);
pss = pass != R_NilValue ? NANO_STRING(pass) : NULL;
if (usefile > 1)
key = CHAR(STRING_ELT(server, 1));
key = NANO_STR_N(server, 1);
if ((xc = nng_tls_config_alloc(&cfg, NNG_TLS_MODE_SERVER)))
goto exitlevel1;
if ((xc = nng_tls_config_auth_mode(cfg, mod)))
Expand Down
2 changes: 1 addition & 1 deletion src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ SEXP rnng_next_config(SEXP refhook, SEXP klass, SEXP list, SEXP mark) {

SETCAR(nano_refHook, plist ? CAR(refhook) : NANO_VECTOR(refhook)[0]);
SETCADR(nano_refHook, plist ? CADR(refhook) : NANO_VECTOR(refhook)[1]);
SETCAR(nano_klassString, STRING_ELT(klass, 0));
SETCAR(nano_klassString, *NANO_VECTOR(klass));

registered = NANO_INTEGER(list) ? 1 : 2;

Expand Down

0 comments on commit 58531b3

Please sign in to comment.