Skip to content

Commit

Permalink
Add marshalling for 'bigmemory'
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikBengtsson committed Mar 23, 2024
1 parent e6c40e9 commit 219ef5a
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 1 deletion.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
Package: marshal
Version: 0.0.1-9004
Version: 0.0.1-9005
Title: Framework to Marshal Objects to be Used in Another R Process
Depends:
R (>= 3.2.0)
Suggests:
knitr,
DT,
jsonlite,
bigmemory,
bundle,
caret,
data.table,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ S3method(marshal,SOCKnode)
S3method(marshal,SpatVector)
S3method(marshal,XMLAbstractDocument)
S3method(marshal,XMLAbstractNode)
S3method(marshal,big.matrix)
S3method(marshal,connection)
S3method(marshal,data.table)
S3method(marshal,default)
Expand Down Expand Up @@ -46,6 +47,7 @@ S3method(marshallable,SOCKnode)
S3method(marshallable,SpatVector)
S3method(marshallable,XMLAbstractDocument)
S3method(marshallable,XMLAbstractNode)
S3method(marshallable,big.memory)
S3method(marshallable,connection)
S3method(marshallable,data.table)
S3method(marshallable,default)
Expand Down
70 changes: 70 additions & 0 deletions R/marshal.bigmemory.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#' Marshalling of 'bigmemory' objects
#'
#' @param x A [bigmemory::big.matrix] object.
#'
#' @param \dots Not used.
#'
#' @return
#' A `marshalled` object as described in [marshal()].
#'
#' @details
#' [bigmemory::write.big.matrix()] is used to produce a marshalled version
#' of the original object.
#' [bigmemory::read.big.matrix()] is used to reconstruct a version of the
#' original object from the marshalled object.
#'
#' @example incl/marshal.bigmemory.R
#'
#' @rdname marshal.bigmemory
#' @aliases marshal.bigmemory
#' @export
marshal.big.matrix <- function(x, ...) {
marshal_bigmemory(x, ...)
}

marshal_bigmemory <- function(x, ...) {
tf <- tempfile(fileext = ".bigmemory")
on.exit(if (!is.null(tf)) file.remove(tf))
bigmemory::write.big.matrix(x, filename = tf)
raw <- readBin(tf, what = "raw", n = file.size(tf))
file.remove(tf)
tf <- NULL

res <- list(
marshalled = list(
typeof = bigmemory::typeof(x),
raw = raw
)
)
class(res) <- marshal_class(x)
rm(list = "raw")

res[["unmarshal"]] <- unmarshal_bigmemory

assert_no_references(res)
res
}

unmarshal_bigmemory <- function(x, ...) {
object <- x[["marshalled"]]
raw <- object[["raw"]]
typeof <- object[["typeof"]]

tf <- tempfile(fileext = ".bigmemory")
on.exit(if (!is.null(tf)) file.remove(tf))
writeBin(raw, con = tf)

res <- bigmemory::read.big.matrix(tf, type = typeof)
file.remove(tf)
tf <- NULL

res
}


#' @rdname marshal.bigmemory
#' @aliases marshallable.big.memory
#' @export
marshallable.big.memory <- function(...) {
TRUE
}
15 changes: 15 additions & 0 deletions incl/marshal.bigmemory.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
if (requireNamespace("bigmemory", quietly = TRUE)) {
library(bigmemory)

x <- big.matrix(nrow = 3, ncol = 2, type = "double")
x[] <- seq_along(x)

## Marshal
x_ <- marshal(x)

## Unmarshal
x2 <- unmarshal(x_)

stopifnot(identical(x2[], x[]))
}

7 changes: 7 additions & 0 deletions inst/known_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
"can_be_marshalled": true,
"must_be_marshalled": true
},
{
"package": "bigmemory",
"type": "big.matrix",
"if_not_marshalled": "segmentation fault",
"can_be_marshalled": true,
"must_be_marshalled": true
},
{
"package": "caret",
"type": "train",
Expand Down
46 changes: 46 additions & 0 deletions man/marshal.bigmemory.Rd

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

16 changes: 16 additions & 0 deletions tests/marshal.bigmemory.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
library(marshal)

if (requireNamespace("bigmemory", quietly = TRUE)) {
library(bigmemory)

x <- big.matrix(nrow = 3, ncol = 2, type = "double")
x[] <- seq_along(x)

## Marshal
x_ <- marshal(x)

## Unmarshal
x2 <- unmarshal(x_)

stopifnot(identical(x2[], x[]))
}

0 comments on commit 219ef5a

Please sign in to comment.