From 9e1dbfd78e8fd548c996b818595e61c425fb4e3f Mon Sep 17 00:00:00 2001 From: LTLA Date: Wed, 21 Feb 2024 14:39:25 -0800 Subject: [PATCH] Avoid type coercion if the types are already the same. This avoids inefficiencies from another unnecessary delayed operation. --- R/readArray.R | 5 ++++- R/readSparseMatrix.R | 12 +++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/R/readArray.R b/R/readArray.R index 4cf4e7f..15cd9f1 100644 --- a/R/readArray.R +++ b/R/readArray.R @@ -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" diff --git a/R/readSparseMatrix.R b/R/readSparseMatrix.R index c62f746..673d112 100644 --- a/R/readSparseMatrix.R +++ b/R/readSparseMatrix.R @@ -64,8 +64,10 @@ 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" @@ -73,12 +75,12 @@ readSparseMatrix <- function(path, metadata, sparsematrix.output.type=NULL, ...) 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")) } }