Skip to content

Commit c26aedb

Browse files
committed
Make nf name conflict more efficient by
memoizing the check within each `compileNimble` call.
1 parent c21ae01 commit c26aedb

2 files changed

Lines changed: 21 additions & 12 deletions

File tree

packages/nimble/R/genCpp_sizeProcessing.R

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,24 @@ scalarOutputTypes <- list(decide = 'logical',
184184
## and it will set the size expressions for A and for itself to 1.
185185
expressionSymbolTypeReplacements <- c('symbolNimbleListGenerator', 'symbolNimbleList', 'symbolNimbleFunction', 'symbolMemberFunction')
186186

187+
checkNameConflict <- function(nm) {
188+
if(!exists(nm, nimbleUserNamespace$.checkedNames)) {
189+
nimbleUserNamespace$.checkedNames[[nm]] <- 1
190+
## Handle replacements such as `gamma` -> `gammafn`.
191+
if(nm %in% specificCallReplacements) {
192+
nm <- names(specificCallReplacements)[which(nm == specificCallReplacements)]
193+
} else if(nm %in% nimKeyWords)
194+
nm <- names(nimKeyWords)[which(nm == nimKeyWords)]
195+
for(i in seq_along(nm)) { # `lgammafn` will give back two items, not one.
196+
objs <- sapply(nm[i], function(x) getAnywhere(x)$objs)
197+
if(any(sapply(objs, is.rcf)))
198+
stop("The name of the nimbleFunction `", nm[i], "` conflicts with a function in the NIMBLE language (DSL); please use a different name")
199+
}
200+
}
201+
}
202+
203+
204+
187205
exprClasses_setSizes <- function(code, symTab, typeEnv) { ## input code is exprClass
188206
## name:
189207
if(code$isName) {
@@ -263,17 +281,7 @@ exprClasses_setSizes <- function(code, symTab, typeEnv) { ## input code is exprC
263281
}
264282
sizeCall <- sizeCalls[[code$name]]
265283
if(!is.null(sizeCall)) {
266-
nm <- code$name
267-
## Handle replacements such as `gamma` -> `gammafn`.
268-
if(nm %in% specificCallReplacements) {
269-
nm <- names(specificCallReplacements)[which(nm == specificCallReplacements)]
270-
} else if(nm %in% nimKeyWords)
271-
nm <- names(nimKeyWords)[which(nm == nimKeyWords)]
272-
for(i in seq_along(nm)) { # `lgammafn` will give back two items, not one.
273-
objs <- sapply(nm[i], function(x) getAnywhere(x)$objs)
274-
if(any(sapply(objs, is.rcf)))
275-
stop("The name of the nimbleFunction `", nm[i], "` conflicts with a function in the NIMBLE language (DSL); please use a different name")
276-
}
284+
checkNameConflict(code$name)
277285
if(.nimbleOptions$debugSizeProcessing) {
278286
browser()
279287
eval(

packages/nimble/R/nimbleProject.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,8 @@ compileNimble <- function(..., project, dirName = NULL, projectName = '',
11601160
for(i in names(controlDefaults)) {
11611161
if(!i %in% names(control)) control[[i]] <- controlDefaults[[i]]
11621162
}
1163-
1163+
1164+
nimbleUserNamespace$.checkedNames <- new.env() # Memoization for checking nf name conflicts in `checkNameConflict()`.
11641165

11651166
## Units should be either Rmodel, nimbleFunction, or RCfunction (now coming from nimbleFunction with no setup)
11661167
if(!showCompilerOutput) {

0 commit comments

Comments
 (0)