Skip to content

Commit 122520d

Browse files
authored
Module(): analyse reference class once and reuse C++Class objects (#1515)
* Module(): analyse reference class once and reuse C++Class objects * Module(): take classDef from the generator, hoist converter registration, match show exactly * Add ChangeLog entry, roll micro version and date
1 parent ac6879e commit 122520d

4 files changed

Lines changed: 54 additions & 31 deletions

File tree

‎ChangeLog‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
2026-10-21 Michael Sumner <mdsumner@gmail.com>
2+
3+
* R/Module.R (Module): Supply the 'initialize' method to setRefClass() with
4+
the other methods instead of adding it in a second generator$methods()
5+
call, which repeated the reference class analysis of every exposed
6+
method, and reuse the C++Class objects returned by
7+
Module__classes_info() rather than rebuilding each of them via
8+
.get_Module_Class(); both reduce the time spent loading a module (#1515)
9+
* R/Module.R (Module): Take the class representation from the generator
10+
rather than looking it up by name with methods::getClass(), which
11+
ignores 'where' and can return another loaded package's class of the same
12+
name
13+
* R/Module.R (Module): Register as(FROM, TO) converters after the loop
14+
that populates 'storage' rather than once per function inside it
15+
* R/Module.R (Module): Match the 'show' method name exactly rather than as
16+
a substring, so a class exposing e.g. 'showInfo' keeps the C++Object show
17+
method
18+
19+
* DESCRIPTION (Version, Date): Roll micro version and date
20+
* inst/include/Rcpp/config.h: Idem
21+
122
2026-10-13 Dirk Eddelbuettel <edd@debian.org>
223

324
* DESCRIPTION (Version, Date): Roll micro version and date

‎DESCRIPTION‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: Rcpp
22
Title: Seamless R and C++ Integration
3-
Version: 1.1.2.5
4-
Date: 2026-10-13
3+
Version: 1.1.2.6
4+
Date: 2026-10-21
55
Authors@R: c(person("Dirk", "Eddelbuettel", role = c("aut", "cre"), email = "edd@debian.org",
66
comment = c(ORCID = "0000-0001-6419-907X")),
77
person("Romain", "Francois", role = "aut",

‎R/Module.R‎

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -222,26 +222,28 @@ Module <- function( module, PACKAGE = methods::getPackageName(where), where = to
222222

223223
fields <- cpp_fields( CLASS, where )
224224
methods <- cpp_refMethods(CLASS, where)
225-
generator <- methods::setRefClass( clname,
226-
fields = fields,
227-
contains = "C++Object",
228-
methods = methods,
229-
where = where
230-
)
231225
# just to make codetools happy
232226
.self <- .refClassDef <- NULL
233-
generator$methods(initialize =
227+
# Supply 'initialize' together with the other methods so that the
228+
# reference class is analysed once, instead of a second full pass
229+
# through refClassInformation() in generator$methods(...)
230+
methods[["initialize"]] <-
234231
if (cpp_hasDefaultConstructor(CLASS))
235232
function(...) Rcpp::cpp_object_initializer(.self,.refClassDef, ...)
236233
else
237234
function(...) {
238235
if (nargs()) Rcpp::cpp_object_initializer(.self,.refClassDef, ...)
239236
else Rcpp::cpp_object_dummy(.self, .refClassDef) # #nocov
240237
}
241-
)
242238
rm( .self, .refClassDef )
239+
generator <- methods::setRefClass( clname,
240+
fields = fields,
241+
contains = "C++Object",
242+
methods = methods,
243+
where = where
244+
)
243245

244-
classDef <- methods::getClass(clname)
246+
classDef <- generator$def
245247
## non-public (static) fields in class representation
246248
## <fixme> Should these become real fields? </fixme>
247249
fields <- classDef@fieldPrototypes
@@ -268,7 +270,7 @@ Module <- function( module, PACKAGE = methods::getPackageName(where), where = to
268270
} # #nocov end
269271

270272
# promoting show to S4
271-
if( any( grepl( "show", names(CLASS@methods) ) ) ){
273+
if( "show" %in% names(CLASS@methods) ){
272274
setMethod( "show", clname, function(object) object$show(), where = where ) # #nocov
273275
}
274276

@@ -281,7 +283,10 @@ Module <- function( module, PACKAGE = methods::getPackageName(where), where = to
281283
CLASS <- classes[[i]]
282284
clname <- CLASS@.Data
283285
demangled_name <- sub( "^Rcpp_", "", clname )
284-
.classes_map[[ CLASS@typeid ]] <- storage[[ demangled_name ]] <- .get_Module_Class( module, demangled_name, xp )
286+
# reuse the C++Class object already built by Module__classes_info
287+
# rather than rebuilding it (and all of its method/field objects)
288+
CLASS@generator <- generators[[ clname ]]
289+
.classes_map[[ CLASS@typeid ]] <- storage[[ demangled_name ]] <- CLASS
285290

286291
# exposing enums values as CLASS.VALUE
287292
# (should really be CLASS$value but I don't know how to do it)
@@ -299,24 +304,21 @@ Module <- function( module, PACKAGE = methods::getPackageName(where), where = to
299304
functions <- .Call( Module__functions_names, xp )
300305
for( fun in functions ){
301306
storage[[ fun ]] <- .get_Module_function( module, fun, xp )
302-
303-
# register as(FROM, TO) methods
304-
converter_rx <- "^[.]___converter___(.*)___(.*)$"
305-
if( length( matches <- grep( converter_rx, functions ) ) ){ # #nocov start
306-
for( i in matches ){
307-
fun <- functions[i]
308-
from <- sub( converter_rx, "\\1", fun )
309-
to <- sub( converter_rx, "\\2", fun )
310-
converter <- function( from ){}
311-
body( converter ) <- substitute( { CONVERT(from) },
312-
list( CONVERT = storage[[fun]] )
313-
)
314-
setAs( from, to, converter, where = where )
315-
}
316-
} # #nocov end
317-
318307
}
319308

309+
# register as(FROM, TO) methods once storage holds every function: the
310+
# converter body captures storage[[fun]] by value via substitute()
311+
converter_rx <- "^[.]___converter___(.*)___(.*)$"
312+
for( fun in grep( converter_rx, functions, value = TRUE ) ){ # #nocov start
313+
from <- sub( converter_rx, "\\1", fun )
314+
to <- sub( converter_rx, "\\2", fun )
315+
converter <- function( from ){}
316+
body( converter ) <- substitute( { CONVERT(from) },
317+
list( CONVERT = storage[[fun]] )
318+
)
319+
setAs( from, to, converter, where = where )
320+
} # #nocov end
321+
320322
assign( "storage", storage, envir = as.environment(module) )
321323
module
322324
}

‎inst/include/Rcpp/config.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
#define RCPP_VERSION_STRING "1.1.2"
3131

3232
// the current source snapshot (using four components, if a fifth is used in DESCRIPTION we ignore it)
33-
#define RCPP_DEV_VERSION RcppDevVersion(1,1,2,5)
34-
#define RCPP_DEV_VERSION_STRING "1.1.2.5"
33+
#define RCPP_DEV_VERSION RcppDevVersion(1,1,2,6)
34+
#define RCPP_DEV_VERSION_STRING "1.1.2.6"
3535

3636
// Whether sugar checks that the operands of an expression, and a vector
3737
// assigned into a range, row or column, have matching lengths. On by default

0 commit comments

Comments
 (0)