Skip to content

Commit

Permalink
Avoid type coercion if the types are already the same.
Browse files Browse the repository at this point in the history
This avoids inefficiencies from another unnecessary delayed operation.
  • Loading branch information
LTLA committed Feb 21, 2024
1 parent f71fc7e commit 9e1dbfd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
5 changes: 4 additions & 1 deletion R/readArray.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ readArray <- function(path, metadata, array.output.type=NULL, ...) {
out <- DelayedMask(out, placeholder=details$placeholder)
out <- DelayedArray(out)
}
type(out) <- from_array_type(details$type)
intended.type <- from_array_type(details$type)
if (type(out) != intended.type) {
type(out) <- intended.type
}

if (is.null(array.output.type)) {
array.output.type <- "ReloadedArray"
Expand Down
12 changes: 7 additions & 5 deletions R/readSparseMatrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,23 @@ readSparseMatrix <- function(path, metadata, sparsematrix.output.type=NULL, ...)
out <- DelayedMask(out, placeholder=details$placeholder)
out <- DelayedArray(out)
}
tt <- from_array_type(details$type)
type(out) <- tt
intended.type <- from_array_type(details$type)
if (type(out) != intended.type) {
type(out) <- intended.type
}

if (is.null(sparsematrix.output.type)) {
sparsematrix.output.type <- "ReloadedArray"
} else {
sparsematrix.output.type <- match.arg(sparsematrix.output.type, c("CsparseMatrix", "SVT_SparseMatrix", "ReloadedArray"))
}
if (sparsematrix.output.type == "CsparseMatrix") {
if (tt == "logical") {
if (intended.type == "logical") {
return(as(out, "lgCMatrix"))
} else if (tt == "double") {
} else if (intended.type == "double") {
return(as(out, "dgCMatrix"))
} else {
warning("cannot faithfully coerce a sparse matrix of type '", tt, "' to a CsparseMatrix subclass")
warning("cannot faithfully coerce a sparse matrix of type '", intended.type, "' to a CsparseMatrix subclass")
return(as(out, "dgCMatrix"))
}
}
Expand Down

0 comments on commit 9e1dbfd

Please sign in to comment.