Skip to content

Commit e6bce79

Browse files
committed
optimise socket opening code
1 parent 1fb6abe commit e6bce79

File tree

5 files changed

+84
-47
lines changed

5 files changed

+84
-47
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
* `socket()` gains the ability to open 'raw' mode sockets. Please note: this is not for general use - do not set this argument unless you have a specific need, such as for use with `device()` (refer to NNG documentation).
77
* Implements `device()` which creates a socket forwarder or proxy. Warning: use this in a separate process as this function blocks with no ability to interrupt.
88

9+
#### Updates
10+
11+
* Internal performance optimisations.
12+
913
# nanonext 0.5.1
1014

1115
#### Updates

R/sendrecv.R

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@
4545
#' queued for sending. Certain protocol / transport combinations may limit
4646
#' the number of messages that can be queued if they have yet to be received.
4747
#'
48-
#' For Contexts and Streams: the default behaviour is blocking with \code{block = TRUE}.
49-
#' This will wait until the send has completed. Set a timeout in this case
50-
#' to ensure that the function returns under all scenarios. As the underlying
51-
#' implementation uses an asynchronous send with a wait, it is recommended
52-
#' to set a positive integer value for \code{block} rather than FALSE.
48+
#' For Contexts and Streams: the default behaviour is blocking with
49+
#' \code{block = TRUE}. This will wait until the send has completed. Set a
50+
#' timeout in this case to ensure that the function returns under all scenarios.
51+
#' As the underlying implementation uses an asynchronous send with a wait,
52+
#' it is recommended to set a positive integer value for \code{block} rather
53+
#' than FALSE.
5354
#'
5455
#' @examples
5556
#' pub <- socket("pub", dial = "inproc://nanonext")

R/socket.R

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ socket <- function(protocol = c("bus", "pair", "push", "pull", "pub", "sub",
8383
autostart = TRUE,
8484
raw = FALSE) {
8585

86-
protocol <- match.arg2(protocol, c("bus", "pair", "push", "pull", "pub", "sub",
87-
"req", "rep", "surveyor", "respondent"))
8886
sock <- .Call(rnng_protocol_open, protocol, raw)
8987
is.integer(sock) && return(sock)
9088
if (!missing(dial)) dial(sock, url = dial, autostart = autostart)

man/send.Rd

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

src/core.c

Lines changed: 68 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -374,57 +374,90 @@ void context_finalizer(SEXP xptr) {
374374

375375
SEXP rnng_protocol_open(SEXP protocol, SEXP raw) {
376376

377-
const int pro = INTEGER(protocol)[0];
377+
const char *pro = CHAR(STRING_ELT(protocol, 0));
378378
const int rw = LOGICAL(raw)[0];
379+
380+
size_t slen = strlen(pro);
381+
const char *bus = "bus", *pair = "pair", *push = "push", *pull = "pull",
382+
*pub = "pub", *sub = "sub", *req = "req", *rep = "rep", *sur = "surveyor",
383+
*res = "respondent";
384+
379385
nng_socket *sock;
380-
char *pname;
381-
int xc;
386+
const char *pname;
387+
int xc = -1;
382388
SEXP socket, klass;
383389

384390
sock = R_Calloc(1, nng_socket);
385-
switch (pro) {
391+
392+
switch (slen) {
386393
case 1:
387-
pname = "bus";
388-
xc = rw ? nng_bus0_open_raw(sock) : nng_bus0_open(sock);
389-
break;
390394
case 2:
391-
pname = "pair";
392-
xc = rw ? nng_pair0_open_raw(sock) : nng_pair0_open(sock);
393-
break;
394395
case 3:
395-
pname = "push";
396-
xc = rw ? nng_push0_open_raw(sock) : nng_push0_open(sock);
397-
break;
396+
if (!strncmp(bus, pro, slen)) {
397+
pname = bus;
398+
xc = rw ? nng_bus0_open_raw(sock) : nng_bus0_open(sock);
399+
break;
400+
}
401+
if (slen > 2) {
402+
if (!strncmp(pub, pro, slen)) {
403+
pname = pub;
404+
xc = rw ? nng_pub0_open_raw(sock) : nng_pub0_open(sock);
405+
break;
406+
}
407+
if (!strncmp(sub, pro, slen)) {
408+
pname = sub;
409+
xc = rw ? nng_sub0_open_raw(sock) : nng_sub0_open(sock);
410+
break;
411+
}
412+
if (!strncmp(req, pro, slen)) {
413+
pname = req;
414+
xc = rw ? nng_req0_open_raw(sock) : nng_req0_open(sock);
415+
break;
416+
}
417+
if (!strncmp(rep, pro, slen)) {
418+
pname = rep;
419+
xc = rw ? nng_rep0_open_raw(sock) : nng_rep0_open(sock);
420+
break;
421+
}
422+
}
398423
case 4:
399-
pname = "pull";
400-
xc = rw ? nng_pull0_open_raw(sock) : nng_pull0_open(sock);
401-
break;
424+
if (slen > 1) {
425+
if (!strncmp(pair, pro, slen)) {
426+
pname = pair;
427+
xc = rw ? nng_pair0_open_raw(sock) : nng_pair0_open(sock);
428+
break;
429+
}
430+
if (slen > 2) {
431+
if (!strncmp(push, pro, slen)) {
432+
pname = push;
433+
xc = rw ? nng_push0_open_raw(sock) : nng_push0_open(sock);
434+
break;
435+
}
436+
if (!strncmp(pull, pro, slen)) {
437+
pname = pull;
438+
xc = rw ? nng_pull0_open_raw(sock) : nng_pull0_open(sock);
439+
break;
440+
}
441+
}
442+
}
402443
case 5:
403-
pname = "pub";
404-
xc = rw ? nng_pub0_open_raw(sock) : nng_pub0_open(sock);
405-
break;
406444
case 6:
407-
pname = "sub";
408-
xc = rw ? nng_sub0_open_raw(sock) : nng_sub0_open(sock);
409-
break;
410445
case 7:
411-
pname = "req";
412-
xc = rw ? nng_req0_open_raw(sock) : nng_req0_open(sock);
413-
break;
414446
case 8:
415-
pname = "rep";
416-
xc = rw ? nng_rep0_open_raw(sock) : nng_rep0_open(sock);
417-
break;
447+
if (slen > 2 && !strncmp(sur, pro, slen)) {
448+
pname = sur;
449+
xc = rw ? nng_surveyor0_open_raw(sock) : nng_surveyor0_open(sock);
450+
break;
451+
}
418452
case 9:
419-
pname = "surveyor";
420-
xc = rw ? nng_surveyor0_open_raw(sock) : nng_surveyor0_open(sock);
421-
break;
422453
case 10:
423-
pname = "respondent";
424-
xc = rw ? nng_respondent0_open_raw(sock) : nng_respondent0_open(sock);
425-
break;
454+
if (slen > 2 && !strncmp(res, pro, slen)) {
455+
pname = res;
456+
xc = rw ? nng_respondent0_open_raw(sock) : nng_respondent0_open(sock);
457+
break;
458+
}
426459
default:
427-
xc = -1;
460+
error_return("'protocol' should be one of bus, pair, push, pull, pub, sub, req, rep, surveyor, respondent");
428461
}
429462

430463
if (xc) {

0 commit comments

Comments
 (0)