Skip to content

Commit 6b2c5bd

Browse files
authored
improve findSamplersOnNodes performance (#1614)
Refactor node matching in findSamplersOnNodes to flatten sampler target nodes once and map matches back to sampler indices. This preserves the return value and can reduce overhead for repeated matching.
1 parent 4521e48 commit 6b2c5bd

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

packages/nimble/R/MCMC_configuration.R

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,15 @@ ind: A numeric vector or character vector. A numeric vector may be used to spec
10811081
if(length(samplerConfs) == 0) return(integer())
10821082
nodes <- model$expandNodeNames(nodes, returnScalarComponents = TRUE, sort = TRUE)
10831083
samplerConfNodesList <- lapply(samplerConfs, function(sc) sc$targetAsScalar)
1084-
return(unique(unlist(lapply(nodes, function(n) which(unlist(lapply(samplerConfNodesList, function(scn) n %in% scn)))))))
1084+
1085+
# Match requested nodes in the flattened node list and map matches back to sampler indices.
1086+
samplerIndices <- 1:length(samplerConfs)
1087+
samplerConfNodesLengths <- unlist(lapply(samplerConfNodesList, length))
1088+
flatSamplerConfNodes <- unlist(samplerConfNodesList)
1089+
flatSamplerIndices <- rep.int(samplerIndices, times = samplerConfNodesLengths)
1090+
flatNodePositions <- unlist(lapply(nodes, function(n) which(n == flatSamplerConfNodes)))
1091+
matchedSamplerIndices <- flatSamplerIndices[flatNodePositions]
1092+
unique(matchedSamplerIndices)
10851093
},
10861094

10871095
getSamplerDefinition = function(ind, print = FALSE) {

0 commit comments

Comments
 (0)