From 2538b4c5b8e2268da56765ed79f0b38206bbc82a Mon Sep 17 00:00:00 2001 From: Gregory Jefferis Date: Sun, 18 Feb 2024 11:01:09 +0000 Subject: [PATCH] speed up xyzmatrix.list for arrow list-vectors * for some reason base lengths is very slow in this case * will use natcpp if available. Speed up is from 500 ms to 2 ms for 250k points --- R/xyzmatrix.R | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/R/xyzmatrix.R b/R/xyzmatrix.R index aa7d9322..86b52308 100644 --- a/R/xyzmatrix.R +++ b/R/xyzmatrix.R @@ -102,7 +102,7 @@ xyzmatrix.list<-function(x, empty2na=TRUE, ...) { if(is.neuron(x,Strict=FALSE)) return(xyzmatrix(x$d[,c("X","Y","Z")])) - lens=lengths(x) + lens=listlengths(x, use.names = F) if(!empty2na) { if(any(lens!=3)) stop("xyzmatrix accepts lists where each element has 3 numbers!") @@ -119,6 +119,16 @@ xyzmatrix.list<-function(x, empty2na=TRUE, ...) { xyzmatrix(mat) } +# private function - motivated by the case that arrow lists of vectors +# are extremely slow with base R lengths +listlengths <- function(x, use.names = FALSE) { + if(use_natcpp() & !use.names) { + return(natcpp::c_listlengths(x)) + } + lengths(x, use.names = use.names) +} + + #' @export #' @rdname xyzmatrix xyzmatrix.character<-function(x, ...) {