Hi all, Prefix: I am a frustrated Java coder in R. I am coding a medium sized ecosystem modelling program in R. I have changed to using S4 objects and it has cost me an order of magnitude in execution speed over the functional model. I cannot afford this penalty and have found that it is the result of all the passing-by-value of objects. I see that you can now safely inherit from environment in V2.9.0. That got me all excited that I would now be able to pass objects by reference. But... That doesn't seem to be the case. It only seem that passing an environment which holds the object allows for pass-by-reference and that passing an object which inherits from environment doesn't. Why is this the case, either an object inherits the properties of its parent or it doesn't. Has anyone else had a play with this? Or have I got it all wrong. I tried the below: ----------------------------------------------------------------------------------------- setClass('foo', representation=representation(stuff='list', bar='numeric'), prototype=list(stuff=list(), bar=0), contains='.environment') setGeneric('doit', function(.Object, newfoo='environment') standardGeneric('doit')) setMethod('doit', 'foo', function(.Object, newfoo){newfoo@bar <- 10}) z <- new('foo') z@stuff$x <- new('foo') doit(z,z@stuff$x) z@stuff$x@bar [1] 0 ------------------------------------------------------------------------------------------ Can anyone help with a better way of doing this. I'm trying to avoid all the indirection of packing and unpacking environments for passing. Thanks heaps Troy Troy Robertson Database and Computing Support Provider Southern Ocean Ecosystems, ERM/Fish Australian Antarctic Division Channel Highway, Kingston 7050 PH: 03 62323571 Troy.Robertson@aad.gov.au ___________________________________________________________________________ Australian Antarctic Division - Commonwealth of Australia IMPORTANT: This transmission is intended for the addressee only. If you are not the intended recipient, you are notified that use or dissemination of this communication is strictly prohibited by Commonwealth law. If you have received this transmission in error, please notify the sender immediately by e-mail or by telephoning +61 3 6232 3209 and DELETE the message. Visit our web site at http://www.antarctica.gov.au/ ___________________________________________________________________________ [[alternative HTML version deleted]]
Not a direct answer to your question, but... You might consider using the R.oo package, from H. Bengtsson. It's very stable, written in pure R, and cleanly allows you to do pass-by-reference OO programming, with no tricks. HTH, af 2009/6/23 Troy Robertson <Troy.Robertson at aad.gov.au>:> Hi all, > > > > Prefix: I am a frustrated Java coder in R. > > > > I am coding a medium sized ecosystem modelling program in R. ?I have changed to using S4 objects and it has cost me an order of magnitude in execution speed over the functional model. ?I cannot afford this penalty and have found that it is the result of all the passing-by-value of objects. > > > > I see that you can now safely inherit from environment in V2.9.0. > > That got me all excited that I would now be able to pass objects by reference. > > > > But... > > That doesn't seem to be the case. > > It only seem that passing an environment which holds the object allows for pass-by-reference and that passing an object which inherits from environment doesn't. > > Why is this the case, either an object inherits the properties of its parent or it doesn't. > > Has anyone else had a play with this? ?Or have I got it all wrong. > > > > I tried the below: > > ----------------------------------------------------------------------------------------- > > setClass('foo', representation=representation(stuff='list', bar='numeric'), > > ? ? ? ? ? ? ? ? ? ? prototype=list(stuff=list(), bar=0), > > ? ? ? ? ? ? ? ? ? ? contains='.environment') > > > > setGeneric('doit', function(.Object, newfoo='environment') standardGeneric('doit')) > > > > setMethod('doit', 'foo', function(.Object, newfoo){newfoo at bar <- 10}) > > > > z <- new('foo') > > > > z at stuff$x <- new('foo') > > > > doit(z,z at stuff$x) > > > > z at stuff$x at bar > > > > [1] 0 > > ------------------------------------------------------------------------------------------ > > > > Can anyone help with a better way of doing this. > > I'm trying to avoid all the indirection of packing and unpacking environments for passing. > > > > > > Thanks heaps > > > > Troy > > > > > > Troy Robertson > > Database and Computing Support Provider > > Southern Ocean Ecosystems, ERM/Fish > > Australian Antarctic Division > > Channel Highway, Kingston 7050 > > PH: 03 62323571 > > Troy.Robertson at aad.gov.au > > > > > ___________________________________________________________________________ > > ? ?Australian Antarctic Division - Commonwealth of Australia > IMPORTANT: This transmission is intended for the addressee only. If you are not the > intended recipient, you are notified that use or dissemination of this communication is > strictly prohibited by Commonwealth law. If you have received this transmission in error, > please notify the sender immediately by e-mail or by telephoning +61 3 6232 3209 and > DELETE the message. > ? ? ? ?Visit our web site at http://www.antarctica.gov.au/ > ___________________________________________________________________________ > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- Antonio, Fabio Di Narzo Ph.D. student at Department of Statistical Sciences University of Bologna, Italy
Troy Robertson wrote:> Hi all, > > > > Prefix: I am a frustrated Java coder in R.ah good, if you're frustrated with Java you'll find R very different ;)> > > > I am coding a medium sized ecosystem modelling program in R. I have changed to using S4 objects and it has cost me an order of magnitude in execution speed over the functional model. I cannot afford this penalty and have found that it is the result of all the passing-by-value of objects. > > > > I see that you can now safely inherit from environment in V2.9.0. > > That got me all excited that I would now be able to pass objects by reference. > > > > But... > > That doesn't seem to be the case. > > It only seem that passing an environment which holds the object allows for pass-by-reference and that passing an object which inherits from environment doesn't. > > Why is this the case, either an object inherits the properties of its parent or it doesn't.The object inherits slots from it's parent, and the methods defined on the parent class. Maybe this example helps? setClass("Ticker", contains=".environment") ## initialize essential, so each instance gets its own environment setMethod(initialize, "Ticker", function(.Object, ..., .xData=new.env(parent=emptyenv())) { .xData[["count"]] <- 0 callNextMethod(.Object, ..., .xData=.xData) }) ## tick: increment (private) counter by n setGeneric("tick", function(reference, n=1L) standardGeneric("tick"), signature="reference") setMethod(tick, "Ticker", function(reference, n=1L) { reference[["count"]] <- reference[["count"]] + n }) ## tock: report current value of counter setGeneric("tock", function(reference) standardGeneric("tock")) setMethod(tock, "Ticker", function(reference) { reference[["count"]] }) and then> e <- new("Ticker") > tock(e)[1] 0> tick(e); tick(e, 10); tock(e)[1] 11> f <- e > tock(f); tick(e); tock(f)[1] 11 [1] 12 The data inside .environment could be structured, too, using S4. Probably it would be more appropriate to have the environment as a slot, rather the class that is being extended. And in terms of inherited 'properties', e.g., the "[[" function as defined on environments is available> e[["count"]]Of course I haven't seen your code, but a different interpretation of your performance issues is that, within the rules of S4, you've chosen to implement functionality in an inefficient way. So it might be instructive to step back a bit and try to reformulate your data structures and methods. This is hard to do. Martin> > Has anyone else had a play with this? Or have I got it all wrong. > > > > I tried the below: > > ----------------------------------------------------------------------------------------- > > setClass('foo', representation=representation(stuff='list', bar='numeric'), > > prototype=list(stuff=list(), bar=0), > > contains='.environment') > > > > setGeneric('doit', function(.Object, newfoo='environment') standardGeneric('doit')) > > > > setMethod('doit', 'foo', function(.Object, newfoo){newfoo at bar <- 10}) > > > > z <- new('foo') > > > > z at stuff$x <- new('foo') > > > > doit(z,z at stuff$x) > > > > z at stuff$x at bar > > > > [1] 0 > > ------------------------------------------------------------------------------------------ > > > > Can anyone help with a better way of doing this. > > I'm trying to avoid all the indirection of packing and unpacking environments for passing. > > > > > > Thanks heaps > > > > Troy > > > > > > Troy Robertson > > Database and Computing Support Provider > > Southern Ocean Ecosystems, ERM/Fish > > Australian Antarctic Division > > Channel Highway, Kingston 7050 > > PH: 03 62323571 > > Troy.Robertson at aad.gov.au > > > > > ___________________________________________________________________________ > > Australian Antarctic Division - Commonwealth of Australia > IMPORTANT: This transmission is intended for the addressee only. If you are not the > intended recipient, you are notified that use or dissemination of this communication is > strictly prohibited by Commonwealth law. If you have received this transmission in error, > please notify the sender immediately by e-mail or by telephoning +61 3 6232 3209 and > DELETE the message. > Visit our web site at http://www.antarctica.gov.au/ > ___________________________________________________________________________ > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
Hi Troy, first of all a question, what kind of ecosystem models are you developing in R? Differential equations or individual-based? Your write that you are a frustrated Java developer in R. I have a similar experience, however I still like JAVA, and I'm now more happy with R as it is much more efficient (i.e. sum(programming + runtime)) for the things I usually do: ecological data analysis and modelling. After using functional R quite a time and Java in parallel I had the same idea, to make R more JAVA like and to model ecosystems in an object oriented manner. At that time I took a look into R.oo (thanks Henrik Bengtssson) and was one of the Co-authors of proto. I still think that R.oo is very good and that proto is a cool idea, but finally I switched to the recommended S4 for my ecological simulation package. Note also, that my solution was *not* to model the ecosystems as objects (habitat - populations- individuals), but instead to model ecological models (equations, inputs, parameters, time steps, outputs, ...). This works quite well with S4. A speed test (see useR!2006 poster on http://simecol.r-forge.r-project.org/) showed that all OOP flavours had quite comparable performance. The only thing I have to have in mind are a few rules: - avoid unnecessary copying of large objects. Sometimes it helps to prefer matrices over data frames. - use vectorization. This means for an individual-based model that one has to re-think how to model an individual: not "many [S4] objects" like in JAVA, but R structures (arrays, lists, data frames) where vectorized functions (e.g. arithmetics or subset) can work with. - avoid interpolation (i.e. approx) and if unavoidable, minimize the tables. If all these things do not help, I write core functions in C (others use Fortran). This can be done in a mixed style and even a full C to C communication is possible (see the deSolve documentation how to do this with differential equation models). Thomas P. -- Thomas Petzoldt Technische Universitaet Dresden Institut fuer Hydrobiologie thomas.petzoldt at tu-dresden.de 01062 Dresden http://tu-dresden.de/hydrobiologie/ GERMANY