Skip to content

Commit

Permalink
Pushes Performance of mirai_map() (#160)
Browse files Browse the repository at this point in the history
* increase performance for mirai_map()

* simplify one + style

* corrects typo

* simplify one
  • Loading branch information
shikokuchuo authored Oct 7, 2024
1 parent 1dc6ac1 commit 2b0db19
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
52 changes: 24 additions & 28 deletions R/map.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
#' allowing only the failures to be re-run.
#'
#' Note: requires daemons to have previously been set. If not, then one
#' local daemon is set before the function propceeds.
#' local daemon is set before the function proceeds.
#'
#' @examples
#' if (interactive()) {
Expand Down Expand Up @@ -168,47 +168,43 @@ mirai_map <- function(.x, .f, ..., .args = list(), .promise = NULL, .compute = "
envir <- ..[[.compute]]
is.null(envir) && {
.x
.f
warning(._[["requires_daemons"]], call. = FALSE, immediate. = TRUE)
daemons(n = 1L, dispatcher = "none", .compute = .compute)
return(mirai_map(.x = .x, .f = .f, ..., .args = .args, .promise = .promise, .compute = .compute))
}
xilen <- dim(.x)[1L]
if (length(xilen)) {
vec <- vector(mode = "list", length = xilen)
if (is.matrix(.x)) {
for (i in seq_len(xilen))
vec[[i]] <- mirai(
.expr = do.call(.f, c(as.list(.x), .args)),
.f = .f,
.x = .x[i, ],
...,
.args = list(.args = .args),
.compute = .compute
)
} else {
for (i in seq_len(xilen))
vec[[i]] <- mirai(
.expr = do.call(.f, c(.x, .args)),
vec <- if (length(xilen))
lapply(
seq_len(xilen),
if (is.matrix(.x)) function(i) mirai(
.expr = do.call(.f, c(as.list(.x), .args)),
.f = .f,
.x = lapply(.x, .subset2, i),
.x = .x[i, ],
...,
.args = list(.args = .args),
.compute = .compute
)
}
} else {
vec <- `names<-`(vector(mode = "list", length = length(.x)), names(.x))
for (i in seq_along(vec))
vec[[i]] <- mirai(
.expr = do.call(.f, c(list(.x), .args)),
) else function(i) mirai(
.expr = do.call(.f, c(.x, .args)),
.f = .f,
.x = .subset2(.x, i),
.x = lapply(.x, .subset2, i),
...,
.args = list(.args = .args),
.compute = .compute
)
}
) else `names<-`(
lapply(
.x,
function(x) mirai(
.expr = do.call(.f, c(list(.x), .args)),
.f = .f,
.x = x,
...,
.args = list(.args = .args),
.compute = .compute
)
),
names(.x)
)

if (length(.promise))
if (is.list(.promise)) {
Expand Down
2 changes: 1 addition & 1 deletion man/mirai_map.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2b0db19

Please sign in to comment.