Skip to content

Commit d8d42a0

Browse files
committed
much more efficient errorValue generation
1 parent 95475ae commit d8d42a0

File tree

6 files changed

+71
-76
lines changed

6 files changed

+71
-76
lines changed

R/listdial.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,12 @@ listen <- function(socket, url = "inproc://nanonext", autostart = TRUE)
155155
#' Start a Listener/Dialer.
156156
#'
157157
#' @param x a Listener or Dialer.
158-
#' @param async [default TRUE] logical flag whether the connection attempt,
159-
#' including any name resolution, is to be made asynchronously. This helps
160-
#' an application be more resilient, but it also generally makes diagnosing
161-
#' failures somewhat more difficult. If FALSE, failure, such as if the
162-
#' connection is refused, will be returned immediately, and no further
163-
#' action will be taken. Supplying a non-logical value will error.
158+
#' @param async [default TRUE] (applicable to Dialers only) logical flag whether
159+
#' the connection attempt, including any name resolution, is to be made
160+
#' asynchronously. This behaviour is more resilient, but also generally
161+
#' makes diagnosing failures somewhat more difficult. If FALSE, failure,
162+
#' such as if the connection is refused, will be returned immediately, and
163+
#' no further action will be taken. Supplying a non-logical value will error.
164164
#' @param ... not used.
165165
#'
166166
#' @return Invisibly, an integer exit code (zero on success).

man/start.Rd

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/aio.c

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -137,50 +137,48 @@ static void haio_finalizer(SEXP xptr) {
137137

138138
static SEXP mk_error_saio(const int xc, SEXP env) {
139139

140-
SEXP err;
141-
PROTECT(err = Rf_ScalarInteger(xc));
142-
Rf_classgets(err, nano_error);
143-
Rf_defineVar(nano_ResultSymbol, err, ENCLOS(env));
144-
UNPROTECT(1);
145-
return err;
140+
SET_INTEGER_ELT(nano_error, 0, xc);
141+
Rf_defineVar(nano_ResultSymbol, nano_error, ENCLOS(env));
142+
return nano_error;
146143

147144
}
148145

149146
static SEXP mk_error_raio(const int xc, SEXP env) {
150147

151-
SEXP err;
152-
PROTECT(err = Rf_ScalarInteger(xc));
153-
Rf_classgets(err, nano_error);
154-
Rf_defineVar(nano_RawSymbol, err, ENCLOS(env));
155-
Rf_defineVar(nano_DataSymbol, err, ENCLOS(env));
156-
UNPROTECT(1);
157-
return err;
148+
SET_INTEGER_ELT(nano_error, 0, xc);
149+
Rf_defineVar(nano_RawSymbol, nano_error, ENCLOS(env));
150+
Rf_defineVar(nano_DataSymbol, nano_error, ENCLOS(env));
151+
return nano_error;
158152

159153
}
160154

161155
static SEXP mk_error_haio(const int xc, SEXP env) {
162156

163-
SEXP err;
164-
PROTECT(err = Rf_ScalarInteger(xc));
165-
Rf_classgets(err, nano_error);
166-
Rf_defineVar(nano_StatusSymbol, err, ENCLOS(env));
167-
Rf_defineVar(nano_IdSymbol, err, ENCLOS(env));
168-
Rf_defineVar(nano_RawSymbol, err, ENCLOS(env));
169-
Rf_defineVar(nano_ProtocolSymbol, err, ENCLOS(env));
170-
UNPROTECT(1);
171-
return err;
157+
SET_INTEGER_ELT(nano_error, 0, xc);
158+
Rf_defineVar(nano_StatusSymbol, nano_error, ENCLOS(env));
159+
Rf_defineVar(nano_IdSymbol, nano_error, ENCLOS(env));
160+
Rf_defineVar(nano_RawSymbol, nano_error, ENCLOS(env));
161+
Rf_defineVar(nano_ProtocolSymbol, nano_error, ENCLOS(env));
162+
return nano_error;
172163

173164
}
174165

175-
static SEXP mk_error_list(const int xc, const int data) {
166+
static SEXP mk_error_data(const int xc) {
176167

177-
SEXP out, err;
178-
const char *names[] = {data ? "data" : "result", ""};
179-
PROTECT(out = Rf_mkNamed(VECSXP, names));
180-
err = Rf_ScalarInteger(xc);
181-
Rf_classgets(err, nano_error);
182-
SET_VECTOR_ELT(out, 0, err);
183-
UNPROTECT(1);
168+
const char *names[] = {"data", ""};
169+
SET_INTEGER_ELT(nano_error, 0, xc);
170+
SEXP out = Rf_mkNamed(VECSXP, names);
171+
SET_VECTOR_ELT(out, 0, nano_error);
172+
return out;
173+
174+
}
175+
176+
static SEXP mk_error_result(const int xc) {
177+
178+
const char *names[] = {"result", ""};
179+
SET_INTEGER_ELT(nano_error, 0, xc);
180+
SEXP out = Rf_mkNamed(VECSXP, names);
181+
SET_VECTOR_ELT(out, 0, nano_error);
184182
return out;
185183

186184
}
@@ -415,13 +413,13 @@ SEXP rnng_send_aio(SEXP con, SEXP data, SEXP mode, SEXP timeout, SEXP clo) {
415413

416414
if ((xc = nng_msg_alloc(&msg, 0))) {
417415
R_Free(saio);
418-
return mk_error_list(xc, 0);
416+
return mk_error_result(xc);
419417
}
420418
if ((xc = nng_msg_append(msg, dp, xlen)) ||
421419
(xc = nng_aio_alloc(&saio->aio, saio_complete, saio))) {
422420
nng_msg_free(msg);
423421
R_Free(saio);
424-
return mk_error_list(xc, 0);
422+
return mk_error_result(xc);
425423
}
426424

427425
nng_aio_set_msg(saio->aio, msg);
@@ -447,14 +445,14 @@ SEXP rnng_send_aio(SEXP con, SEXP data, SEXP mode, SEXP timeout, SEXP clo) {
447445

448446
if ((xc = nng_msg_alloc(&msg, 0))) {
449447
R_Free(saio);
450-
return mk_error_list(xc, 0);
448+
return mk_error_result(xc);
451449
}
452450

453451
if ((xc = nng_msg_append(msg, dp, xlen)) ||
454452
(xc = nng_aio_alloc(&saio->aio, saio_complete, saio))) {
455453
nng_msg_free(msg);
456454
R_Free(saio);
457-
return mk_error_list(xc, 0);
455+
return mk_error_result(xc);
458456
}
459457

460458
nng_aio_set_msg(saio->aio, msg);
@@ -484,14 +482,14 @@ SEXP rnng_send_aio(SEXP con, SEXP data, SEXP mode, SEXP timeout, SEXP clo) {
484482
if ((xc = nng_aio_alloc(&iaio->aio, iaio_complete, iaio))) {
485483
R_Free(iov);
486484
R_Free(iaio);
487-
return mk_error_list(xc, 0);
485+
return mk_error_result(xc);
488486
}
489487

490488
if ((xc = nng_aio_set_iov(iaio->aio, 1u, iov))) {
491489
nng_aio_free(iaio->aio);
492490
R_Free(iov);
493491
R_Free(iaio);
494-
return mk_error_list(xc, 0);
492+
return mk_error_result(xc);
495493
}
496494

497495
nng_aio_set_timeout(iaio->aio, dur);
@@ -546,7 +544,7 @@ SEXP rnng_recv_aio(SEXP con, SEXP mode, SEXP timeout, SEXP keep, SEXP bytes, SEX
546544

547545
if ((xc = nng_aio_alloc(&raio->aio, raio_complete, raio))) {
548546
R_Free(raio);
549-
return kpr ? mk_error_recv(xc) : mk_error_list(xc, 1);
547+
return kpr ? mk_error_recv(xc) : mk_error_data(xc);
550548
}
551549

552550
nng_aio_set_timeout(raio->aio, dur);
@@ -567,7 +565,7 @@ SEXP rnng_recv_aio(SEXP con, SEXP mode, SEXP timeout, SEXP keep, SEXP bytes, SEX
567565

568566
if ((xc = nng_aio_alloc(&raio->aio, raio_complete, raio))) {
569567
R_Free(raio);
570-
return kpr ? mk_error_recv(xc) : mk_error_list(xc, 1);
568+
return kpr ? mk_error_recv(xc) : mk_error_data(xc);
571569
}
572570

573571
nng_aio_set_timeout(raio->aio, dur);
@@ -594,15 +592,15 @@ SEXP rnng_recv_aio(SEXP con, SEXP mode, SEXP timeout, SEXP keep, SEXP bytes, SEX
594592
R_Free(iov->iov_buf);
595593
R_Free(iov);
596594
R_Free(iaio);
597-
return kpr ? mk_error_recv(xc) : mk_error_list(xc, 1);
595+
return kpr ? mk_error_recv(xc) : mk_error_data(xc);
598596
}
599597

600598
if ((xc = nng_aio_set_iov(iaio->aio, 1u, iov))) {
601599
nng_aio_free(iaio->aio);
602600
R_Free(iov->iov_buf);
603601
R_Free(iov);
604602
R_Free(iaio);
605-
return kpr ? mk_error_recv(xc) : mk_error_list(xc, 1);
603+
return kpr ? mk_error_recv(xc) : mk_error_data(xc);
606604
}
607605

608606
nng_aio_set_timeout(iaio->aio, dur);
@@ -898,14 +896,14 @@ SEXP rnng_request(SEXP con, SEXP data, SEXP sendmode, SEXP recvmode, SEXP timeou
898896

899897
if ((xc = nng_msg_alloc(&msg, 0))) {
900898
R_Free(saio);
901-
return kpr ? mk_error_recv(xc) : mk_error_list(xc, 1);
899+
return kpr ? mk_error_recv(xc) : mk_error_data(xc);
902900
}
903901

904902
if ((xc = nng_msg_append(msg, dp, xlen)) ||
905903
(xc = nng_aio_alloc(&saio->aio, saio_complete, saio))) {
906904
nng_msg_free(msg);
907905
R_Free(saio);
908-
return kpr ? mk_error_recv(xc) : mk_error_list(xc, 1);
906+
return kpr ? mk_error_recv(xc) : mk_error_data(xc);
909907
}
910908

911909
nng_aio_set_msg(saio->aio, msg);
@@ -922,7 +920,7 @@ SEXP rnng_request(SEXP con, SEXP data, SEXP sendmode, SEXP recvmode, SEXP timeou
922920

923921
if ((xc = nng_aio_alloc(&raio->aio, raio_complete, raio))) {
924922
R_Free(raio);
925-
return kpr ? mk_error_recv(xc) : mk_error_list(xc, 1);
923+
return kpr ? mk_error_recv(xc) : mk_error_data(xc);
926924
}
927925

928926
nng_aio_set_timeout(raio->aio, dur);

src/core.c

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,45 +23,39 @@
2323

2424
static SEXP mk_error(const int xc) {
2525

26-
SEXP err = Rf_ScalarInteger(xc);
27-
Rf_classgets(err, nano_error);
28-
return err;
26+
SET_INTEGER_ELT(nano_error, 0, xc);
27+
return nano_error;
2928

3029
}
3130

3231
SEXP mk_werror(const int xc) {
3332

3433
Rf_warning("%d | %s", xc, nng_strerror(xc));
35-
SEXP err = Rf_ScalarInteger(xc);
36-
Rf_classgets(err, nano_error);
37-
return err;
34+
SET_INTEGER_ELT(nano_error, 0, xc);
35+
return nano_error;
3836

3937
}
4038

4139
SEXP mk_error_recv(const int xc) {
4240

43-
SEXP out, err;
4441
const char *names[] = {"raw", "data", ""};
45-
PROTECT(out = Rf_mkNamed(VECSXP, names));
46-
err = Rf_ScalarInteger(xc);
47-
Rf_classgets(err, nano_error);
48-
SET_VECTOR_ELT(out, 0, err);
49-
SET_VECTOR_ELT(out, 1, err);
50-
UNPROTECT(1);
42+
SET_INTEGER_ELT(nano_error, 0, xc);
43+
SEXP out = Rf_mkNamed(VECSXP, names);
44+
SET_VECTOR_ELT(out, 0, nano_error);
45+
SET_VECTOR_ELT(out, 1, nano_error);
5146
return out;
5247

5348
}
5449

5550
SEXP mk_error_ncurl(const int xc) {
5651

57-
SEXP out, err;
5852
const char *names[] = {"status", "headers", "raw", "data", ""};
59-
PROTECT(out = Rf_mkNamed(VECSXP, names));
60-
err = Rf_ScalarInteger(xc);
61-
Rf_classgets(err, nano_error);
62-
for (int i = 0; i < 4; i++)
63-
SET_VECTOR_ELT(out, i, err);
64-
UNPROTECT(1);
53+
SET_INTEGER_ELT(nano_error, 0, xc);
54+
SEXP out = Rf_mkNamed(VECSXP, names);
55+
SET_VECTOR_ELT(out, 0, nano_error);
56+
SET_VECTOR_ELT(out, 1, nano_error);
57+
SET_VECTOR_ELT(out, 2, nano_error);
58+
SET_VECTOR_ELT(out, 3, nano_error);
6559
return out;
6660

6761
}

src/init.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ SEXP nano_aioFormals;
4545
SEXP nano_aioFuncs;
4646
SEXP nano_aioNFuncs;
4747
SEXP nano_error;
48+
SEXP nano_errorLists;
4849
SEXP nano_ncurlAio;
4950
SEXP nano_recvAio;
5051
SEXP nano_sendAio;
@@ -88,7 +89,8 @@ static void PreserveObjects(void) {
8889
SETCADR(nano_aioNFuncs, Rf_lang5(nano_DotcallSymbol, nano_AioHttpSymbol, nano_DataSymbol, nano_ResponseSymbol, Rf_ScalarInteger(2)));
8990
SETCADDR(nano_aioNFuncs, Rf_lang5(nano_DotcallSymbol, nano_AioHttpSymbol, nano_DataSymbol, nano_ResponseSymbol, Rf_ScalarInteger(3)));
9091
SETCADDDR(nano_aioNFuncs, Rf_lang5(nano_DotcallSymbol, nano_AioHttpSymbol, nano_DataSymbol, nano_ResponseSymbol, Rf_ScalarInteger(4)));
91-
R_PreserveObject(nano_error = Rf_mkString("errorValue"));
92+
R_PreserveObject(nano_error = Rf_ScalarInteger(1));
93+
Rf_classgets(nano_error, Rf_mkString("errorValue"));
9294
R_PreserveObject(nano_ncurlAio = Rf_allocVector(STRSXP, 2));
9395
SET_STRING_ELT(nano_ncurlAio, 0, Rf_mkChar("ncurlAio"));
9496
SET_STRING_ELT(nano_ncurlAio, 1, Rf_mkChar("recvAio"));

src/utils.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ SEXP rnng_stream_close(SEXP stream) {
497497
nng_stream_free(sp);
498498
R_ClearExternalPtr(stream);
499499
SET_ATTRIB(stream, R_NilValue);
500+
SET_OBJECT(stream, 0);
500501

501502
return nano_success;
502503

0 commit comments

Comments
 (0)