Skip to content

Commit 58531b3

Browse files
committed
efficiencies by avoiding STRING_ELT
1 parent d490eaf commit 58531b3

File tree

5 files changed

+16
-15
lines changed

5 files changed

+16
-15
lines changed

src/core.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -503,10 +503,10 @@ void nano_encode(nano_buf *enc, const SEXP object) {
503503
R_xlen_t i;
504504
size_t slen, outlen = 0;
505505
for (i = 0; i < xlen; i++)
506-
outlen += strlen(CHAR(STRING_ELT(object, i))) + 1;
506+
outlen += strlen(NANO_STR_N(object, i)) + 1;
507507
NANO_ALLOC(enc, outlen);
508508
for (i = 0; i < xlen; i++) {
509-
s = CHAR(STRING_ELT(object, i));
509+
s = NANO_STR_N(object, i);
510510
slen = strlen(s) + 1;
511511
memcpy(enc->buf + enc->cur, s, slen);
512512
enc->cur += slen;
@@ -595,7 +595,7 @@ int nano_matcharg(const SEXP mode) {
595595
int nano_matchargs(const SEXP mode) {
596596

597597
if (TYPEOF(mode) != INTSXP) {
598-
const char *mod = CHAR(STRING_ELT(mode, XLENGTH(mode) == 9));
598+
const char *mod = NANO_STR_N(mode, XLENGTH(mode) == 9);
599599
size_t slen = strlen(mod);
600600
switch (slen) {
601601
case 1:

src/nanonext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ typedef struct nano_handle_s {
9696
#define NANO_DATAPTR(x) (void *) DATAPTR_RO(x)
9797
#define NANO_VECTOR(x) ((const SEXP *) DATAPTR_RO(x))
9898
#define NANO_STRING(x) CHAR(*((const SEXP *) DATAPTR_RO(x)))
99+
#define NANO_STR_N(x, n) CHAR(((const SEXP *) DATAPTR_RO(x))[n])
99100
#define NANO_INTEGER(x) *(int *) DATAPTR_RO(x)
100101
#define NANO_ERROR(x) { Rf_error(x); return R_NilValue; }
101102

src/ncurl.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ SEXP rnng_ncurl(SEXP http, SEXP convert, SEXP follow, SEXP method, SEXP headers,
168168
if (TYPEOF(hnames) == STRSXP && XLENGTH(hnames) == hlen) {
169169
for (R_xlen_t i = 0; i < hlen; i++) {
170170
if ((xc = nng_http_req_set_header(req,
171-
CHAR(STRING_ELT(hnames, i)),
172-
CHAR(STRING_ELT(headers, i)))))
171+
NANO_STR_N(hnames, i),
172+
NANO_STR_N(headers, i))))
173173
goto exitlevel4;
174174
}
175175
}
@@ -262,7 +262,7 @@ SEXP rnng_ncurl(SEXP http, SEXP convert, SEXP follow, SEXP method, SEXP headers,
262262
SET_VECTOR_ELT(out, 1, rvec);
263263
Rf_namesgets(rvec, response);
264264
for (R_xlen_t i = 0; i < rlen; i++) {
265-
const char *r = nng_http_res_get_header(res, CHAR(STRING_ELT(response, i)));
265+
const char *r = nng_http_res_get_header(res, NANO_STR_N(response, i));
266266
SET_VECTOR_ELT(rvec, i, r == NULL ? R_NilValue : Rf_mkString(r));
267267
}
268268
} else {
@@ -343,8 +343,8 @@ SEXP rnng_ncurl_aio(SEXP http, SEXP convert, SEXP method, SEXP headers, SEXP dat
343343
if (TYPEOF(hnames) == STRSXP && XLENGTH(hnames) == hlen) {
344344
for (R_xlen_t i = 0; i < hlen; i++) {
345345
if ((xc = nng_http_req_set_header(handle->req,
346-
CHAR(STRING_ELT(hnames, i)),
347-
CHAR(STRING_ELT(headers, i)))))
346+
NANO_STR_N(hnames, i),
347+
NANO_STR_N(headers, i))))
348348
goto exitlevel4;
349349
}
350350
}
@@ -476,7 +476,7 @@ static SEXP rnng_aio_http_impl(SEXP env, const int typ) {
476476
PROTECT(rvec = Rf_allocVector(VECSXP, rlen));
477477
Rf_namesgets(rvec, response);
478478
for (R_xlen_t i = 0; i < rlen; i++) {
479-
const char *r = nng_http_res_get_header(handle->res, CHAR(STRING_ELT(response, i)));
479+
const char *r = nng_http_res_get_header(handle->res, NANO_STR_N(response, i));
480480
SET_VECTOR_ELT(rvec, i, r == NULL ? R_NilValue : Rf_mkString(r));
481481
}
482482
UNPROTECT(1);
@@ -557,8 +557,8 @@ SEXP rnng_ncurl_session(SEXP http, SEXP convert, SEXP method, SEXP headers, SEXP
557557
if (TYPEOF(hnames) == STRSXP && XLENGTH(hnames) == hlen) {
558558
for (R_xlen_t i = 0; i < hlen; i++) {
559559
if ((xc = nng_http_req_set_header(handle->req,
560-
CHAR(STRING_ELT(hnames, i)),
561-
CHAR(STRING_ELT(headers, i)))))
560+
NANO_STR_N(hnames, i),
561+
NANO_STR_N(headers, i))))
562562
goto exitlevel4;
563563
}
564564
}
@@ -670,7 +670,7 @@ SEXP rnng_ncurl_transact(SEXP session) {
670670
SET_VECTOR_ELT(out, 1, rvec);
671671
Rf_namesgets(rvec, response);
672672
for (R_xlen_t i = 0; i < rlen; i++) {
673-
const char *r = nng_http_res_get_header(handle->res, CHAR(STRING_ELT(response, i)));
673+
const char *r = nng_http_res_get_header(handle->res, NANO_STR_N(response, i));
674674
SET_VECTOR_ELT(rvec, i, r == NULL ? R_NilValue : Rf_mkString(r));
675675
}
676676
} else {

src/tls.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ SEXP rnng_tls_config(SEXP client, SEXP server, SEXP pass, SEXP auth) {
296296
if ((usefile = Rf_xlength(client)) > 0) {
297297
file = NANO_STRING(client);
298298
if (usefile > 1)
299-
crl = CHAR(STRING_ELT(client, 1));
299+
crl = NANO_STR_N(client, 1);
300300
if ((xc = nng_tls_config_alloc(&cfg, NNG_TLS_MODE_CLIENT)))
301301
goto exitlevel1;
302302
if ((xc = nng_tls_config_auth_mode(cfg, mod)))
@@ -315,7 +315,7 @@ SEXP rnng_tls_config(SEXP client, SEXP server, SEXP pass, SEXP auth) {
315315
file = NANO_STRING(server);
316316
pss = pass != R_NilValue ? NANO_STRING(pass) : NULL;
317317
if (usefile > 1)
318-
key = CHAR(STRING_ELT(server, 1));
318+
key = NANO_STR_N(server, 1);
319319
if ((xc = nng_tls_config_alloc(&cfg, NNG_TLS_MODE_SERVER)))
320320
goto exitlevel1;
321321
if ((xc = nng_tls_config_auth_mode(cfg, mod)))

src/utils.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ SEXP rnng_next_config(SEXP refhook, SEXP klass, SEXP list, SEXP mark) {
501501

502502
SETCAR(nano_refHook, plist ? CAR(refhook) : NANO_VECTOR(refhook)[0]);
503503
SETCADR(nano_refHook, plist ? CADR(refhook) : NANO_VECTOR(refhook)[1]);
504-
SETCAR(nano_klassString, STRING_ELT(klass, 0));
504+
SETCAR(nano_klassString, *NANO_VECTOR(klass));
505505

506506
registered = NANO_INTEGER(list) ? 1 : 2;
507507

0 commit comments

Comments
 (0)