Skip to content

Commit ab104a0

Browse files
committed
move encode down to C level
1 parent 3bd36d4 commit ab104a0

File tree

11 files changed

+145
-113
lines changed

11 files changed

+145
-113
lines changed

R/aio.R

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ send_aio <- function(con, data, mode = c("serial", "raw"), timeout = -2L) UseMet
4949
#'
5050
send_aio.nanoSocket <- function(con, data, mode = c("serial", "raw"), timeout = -2L) {
5151

52-
mode <- match.arg2(mode, c("serial", "raw"))
53-
force(data)
54-
data <- encode(data = data, mode = mode)
52+
if (missing(mode) || match.arg1(mode) == 1L)
53+
data <- serialize(object = data, connection = NULL)
5554
aio <- .Call(rnng_send_aio, con, data, timeout)
5655
is.integer(aio) && return(aio)
5756

@@ -78,9 +77,8 @@ send_aio.nanoSocket <- function(con, data, mode = c("serial", "raw"), timeout =
7877
#'
7978
send_aio.nanoContext <- function(con, data, mode = c("serial", "raw"), timeout = -2L) {
8079

81-
mode <- match.arg2(mode, c("serial", "raw"))
82-
force(data)
83-
data <- encode(data = data, mode = mode)
80+
if (missing(mode) || match.arg1(mode) == 1L)
81+
data <- serialize(object = data, connection = NULL)
8482
aio <- .Call(rnng_ctx_send_aio, con, data, timeout)
8583
is.integer(aio) && return(aio)
8684

@@ -107,8 +105,6 @@ send_aio.nanoContext <- function(con, data, mode = c("serial", "raw"), timeout =
107105
#'
108106
send_aio.nanoStream <- function(con, data, mode = "raw", timeout = -2L) {
109107

110-
force(data)
111-
data <- encode(data = data, mode = 2L)
112108
aio <- .Call(rnng_stream_send_aio, con, data, timeout)
113109
is.integer(aio) && return(aio)
114110

@@ -479,7 +475,7 @@ stop_aio <- function(aio) {
479475
#'
480476
#' while (unresolved(aio)) {
481477
#' # do stuff before checking resolution again
482-
#' cat("unresolved")
478+
#' cat("unresolved\n")
483479
#' s2 <- socket("pair", dial = "inproc://nanonext")
484480
#' Sys.sleep(0.01)
485481
#' }

R/context.R

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,16 @@ reply <- function(context,
115115

116116
recv_mode <- match.arg2(recv_mode, c("serial", "character", "complex", "double",
117117
"integer", "logical", "numeric", "raw"))
118-
send_mode <- match.arg2(send_mode, c("serial", "raw"))
119118
res <- .Call(rnng_ctx_recv, context, recv_mode, timeout, FALSE)
120-
is_error_value(res) && return(res)
119+
is_error_value(res) && return(invisible(res))
121120
on.exit(expr = send_aio(context, as.raw(0L), mode = send_mode))
122121
data <- execute(res, ...)
123-
data <- encode(data = data, mode = send_mode)
122+
if (missing(send_mode) || match.arg1(send_mode) == 1L)
123+
data <- serialize(object = data, connection = NULL)
124+
res <- .Call(rnng_ctx_send, context, data, timeout)
124125
on.exit()
125-
invisible(.Call(rnng_ctx_send, context, data, timeout))
126+
is.integer(res) && return(invisible(res))
127+
invisible(0L)
126128

127129
}
128130

@@ -181,11 +183,10 @@ request <- function(context,
181183
timeout = -2L,
182184
keep.raw = TRUE) {
183185

184-
send_mode <- match.arg2(send_mode, c("serial", "raw"))
185186
recv_mode <- match.arg2(recv_mode, c("serial", "character", "complex", "double",
186187
"integer", "logical", "numeric", "raw"))
187-
force(data)
188-
data <- encode(data = data, mode = send_mode)
188+
if (missing(send_mode) || match.arg1(send_mode) == 1L)
189+
data <- serialize(object = data, connection = NULL)
189190
res <- .Call(rnng_ctx_send_aio, context, data, -2L)
190191
is.integer(res) && return(res)
191192

R/messenger.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ messenger <- function(url) {
5555
data <- readline()
5656
if (identical(data, ":q")) break
5757
if (identical(data, "")) next
58-
rdata <- writeBin(object = data, con = raw())
59-
s <- suppressWarnings(.Call(rnng_send, sock, rdata, 0L))
58+
s <- suppressWarnings(.Call(rnng_send, sock, data, 0L))
6059
if (is.integer(s)) {
6160
cat(sprintf("%*s > not sent: peer offline: %s\n", nchar(data), "", format.POSIXct(Sys.time())),
6261
file = stderr())

R/sendrecv.R

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,11 @@ send.nanoSocket <- function(con,
7474
block = FALSE,
7575
echo = TRUE) {
7676

77-
mode <- match.arg2(mode, c("serial", "raw"))
78-
force(data)
79-
data <- encode(data = data, mode = mode)
77+
if (missing(mode) || match.arg1(mode) == 1L)
78+
data <- serialize(object = data, connection = NULL)
8079
res <- .Call(rnng_send, con, data, block)
81-
res && return(invisible(res))
82-
if (missing(echo) || isTRUE(echo)) data else invisible(res)
80+
is.integer(res) && return(res)
81+
if (missing(echo) || isTRUE(echo)) res else invisible(0L)
8382

8483
}
8584

@@ -93,13 +92,12 @@ send.nanoContext <- function(con,
9392
block = TRUE,
9493
echo = TRUE) {
9594

96-
mode <- match.arg2(mode, c("serial", "raw"))
97-
force(data)
98-
data <- encode(data = data, mode = mode)
95+
if (missing(mode) || match.arg1(mode) == 1L)
96+
data <- serialize(object = data, connection = NULL)
9997
if (missing(block) || isTRUE(block)) block <- -2L
10098
res <- .Call(rnng_ctx_send, con, data, block)
101-
res && return(invisible(res))
102-
if (missing(echo) || isTRUE(echo)) data else invisible(res)
99+
is.integer(res) && return(res)
100+
if (missing(echo) || isTRUE(echo)) res else invisible(0L)
103101

104102
}
105103

@@ -113,12 +111,10 @@ send.nanoStream <- function(con,
113111
block = TRUE,
114112
echo = TRUE) {
115113

116-
force(data)
117-
data <- encode(data = data, mode = 2L)
118114
if (missing(block) || isTRUE(block)) block <- -2L
119115
res <- .Call(rnng_stream_send, con, data, block)
120-
res && return(invisible(res))
121-
if (missing(echo) || isTRUE(echo)) data else invisible(res)
116+
is.integer(res) && return(res)
117+
if (missing(echo) || isTRUE(echo)) res else invisible(0L)
122118

123119
}
124120

R/utils.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,6 @@ nano_init <- function(warn = c("immediate", "deferred", "error", "none")) {
256256

257257
# nanonext - Non-exported functions --------------------------------------------
258258

259-
encode <- function(data, mode) {
260-
switch(mode,
261-
serialize(object = data, connection = NULL),
262-
if (is.raw(data)) data else writeBin(object = data, con = raw()))
263-
}
264-
265259
match.arg2 <- function(choice, choices) {
266260
identical(choice, choices) && return(1L)
267261
index <- pmatch(choice[1L], choices, nomatch = 0L, duplicates.ok = TRUE)
@@ -270,3 +264,9 @@ match.arg2 <- function(choice, choices) {
270264
index
271265
}
272266

267+
match.arg1 <- function(choice) {
268+
index <- pmatch(choice[1L], c("serial", "raw"), nomatch = 0L, duplicates.ok = TRUE)
269+
index || stop(sprintf("'%s' should be one of serial, raw", deparse(substitute(choice))))
270+
index
271+
}
272+

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ aio
377377
#> < recvAio >
378378
#> - $data for message data
379379
aio$data |> str()
380-
#> num [1:100000000] 0.369 -0.681 -0.413 0.83 -0.988 ...
380+
#> num [1:100000000] -0.154 -0.281 -1.865 0.332 2.625 ...
381381
```
382382

383383
As `call_aio()` is blocking and will wait for completion, an alternative
@@ -514,11 +514,11 @@ ncurl("http://httpbin.org/headers")
514514
#> [1] 7b 0a 20 20 22 68 65 61 64 65 72 73 22 3a 20 7b 0a 20 20 20 20 22 48 6f 73
515515
#> [26] 74 22 3a 20 22 68 74 74 70 62 69 6e 2e 6f 72 67 22 2c 20 0a 20 20 20 20 22
516516
#> [51] 58 2d 41 6d 7a 6e 2d 54 72 61 63 65 2d 49 64 22 3a 20 22 52 6f 6f 74 3d 31
517-
#> [76] 2d 36 32 36 32 39 36 30 32 2d 34 38 64 39 33 64 39 33 33 64 36 32 35 66 30
518-
#> [101] 36 30 36 62 38 66 37 33 65 22 0a 20 20 7d 0a 7d 0a
517+
#> [76] 2d 36 32 36 33 32 39 65 32 2d 34 61 64 37 64 36 33 62 35 64 33 62 30 65 63
518+
#> [101] 62 31 38 63 66 38 32 31 38 22 0a 20 20 7d 0a 7d 0a
519519
#>
520520
#> $data
521-
#> [1] "{\n \"headers\": {\n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-62629602-48d93d933d625f0606b8f73e\"\n }\n}\n"
521+
#> [1] "{\n \"headers\": {\n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-626329e2-4ad7d63b5d3b0ecb18cf8218\"\n }\n}\n"
522522
```
523523

524524
For advanced use, supports additional HTTP methods such as POST or PUT.
@@ -533,7 +533,7 @@ res
533533
#> - $raw for raw message
534534

535535
call_aio(res)$data
536-
#> [1] "{\n \"args\": {}, \n \"data\": \"{\\\"key\\\": \\\"value\\\"}\", \n \"files\": {}, \n \"form\": {}, \n \"headers\": {\n \"Authorization\": \"Bearer APIKEY\", \n \"Content-Length\": \"16\", \n \"Content-Type\": \"application/json\", \n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-62629602-1fb9951a0f4246df56d74460\"\n }, \n \"json\": {\n \"key\": \"value\"\n }, \n \"origin\": \"78.145.225.121\", \n \"url\": \"http://httpbin.org/post\"\n}\n"
536+
#> [1] "{\n \"args\": {}, \n \"data\": \"{\\\"key\\\": \\\"value\\\"}\", \n \"files\": {}, \n \"form\": {}, \n \"headers\": {\n \"Authorization\": \"Bearer APIKEY\", \n \"Content-Length\": \"16\", \n \"Content-Type\": \"application/json\", \n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-626329e3-3ad612de0091f2db61bb42eb\"\n }, \n \"json\": {\n \"key\": \"value\"\n }, \n \"origin\": \"78.145.225.121\", \n \"url\": \"http://httpbin.org/post\"\n}\n"
537537
```
538538

539539
In this respect, it may be used as a performant and lightweight method

man/unresolved.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/aio.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -446,12 +446,12 @@ SEXP rnng_send_aio(SEXP socket, SEXP data, SEXP timeout) {
446446

447447
nng_socket *sock = (nng_socket *) R_ExternalPtrAddr(socket);
448448
const nng_duration dur = (nng_duration) Rf_asInteger(timeout);
449-
unsigned char *dp = RAW(data);
450-
const R_xlen_t xlen = Rf_xlength(data);
451449
nano_aio *saio = R_Calloc(1, nano_aio);
452450
nng_msg *msg;
453451
int xc;
454-
SEXP aio;
452+
SEXP aio, enc = nano_encode(data);
453+
const R_xlen_t xlen = Rf_xlength(enc);
454+
unsigned char *dp = RAW(enc);
455455

456456
saio->type = SENDAIO;
457457

@@ -499,12 +499,12 @@ SEXP rnng_ctx_send_aio(SEXP context, SEXP data, SEXP timeout) {
499499

500500
nng_ctx *ctxp = (nng_ctx *) R_ExternalPtrAddr(context);
501501
const nng_duration dur = (nng_duration) Rf_asInteger(timeout);
502-
unsigned char *dp = RAW(data);
503-
const R_xlen_t xlen = Rf_xlength(data);
504502
nano_aio *saio = R_Calloc(1, nano_aio);
505503
nng_msg *msg;
506504
int xc;
507-
SEXP aio;
505+
SEXP aio, enc = nano_encode(data);
506+
const R_xlen_t xlen = Rf_xlength(enc);
507+
unsigned char *dp = RAW(enc);
508508

509509
saio->type = SENDAIO;
510510

@@ -553,13 +553,13 @@ SEXP rnng_stream_send_aio(SEXP stream, SEXP data, SEXP timeout) {
553553

554554
nng_stream *sp = (nng_stream *) R_ExternalPtrAddr(stream);
555555
const nng_duration dur = (nng_duration) Rf_asInteger(timeout);
556-
unsigned char *dp = RAW(data);
557-
const R_xlen_t xlen = Rf_xlength(data);
558556
const int frames = *LOGICAL(Rf_getAttrib(stream, nano_TextframesSymbol));
559557
nano_aio *iaio = R_Calloc(1, nano_aio);
560558
nng_iov *iov = R_Calloc(1, nng_iov);
561559
int xc;
562-
SEXP aio;
560+
SEXP aio, enc = nano_encode(data);
561+
const R_xlen_t xlen = Rf_xlength(enc);
562+
unsigned char *dp = RAW(enc);
563563

564564
iaio->type = IOV_SENDAIO;
565565
iaio->data = iov;

0 commit comments

Comments
 (0)