Skip to content

Commit

Permalink
speed up xyzmatrix.list for arrow list-vectors
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jefferis committed Feb 18, 2024
1 parent 8cf4305 commit 2538b4c
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion R/xyzmatrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
Expand All @@ -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, ...) {
Expand Down

0 comments on commit 2538b4c

Please sign in to comment.