Why is assigning an S4 object with an environment slot faster with a medium reference than a direct assignment?

When an S4 object has a slot containing an environment, assigning a value directly to the environment is slower than assigning the environment to a variable and then executing the job. I gave an example with a reference:

setClass( "test_class", slots = list( a_slot = "environment" ) )

test_object = new( "test_class", a_slot = list2env( list( a = rep(NaN,10000) ) ) )
test_object2 = new( "test_class", a_slot = list2env( list( a = rep(NaN,10) ) ) ) 

a1 = function( test_object, i ){
    test_object@a_slot$a[i] = 1
}

a2 = function( test_object, i ){
    w = test_object@a_slot
    w$a[i] = 1
}

a3 = function( test_obj_slot, i ){
    test_obj_slot$a[i] = 1
}

microbenchmark( 
    test_object_a1 = a1( test_object, 1 ), 
    test_object_a2 = a2( test_object, 1 ), 
    test_object_a3 = a3( test_object@a_slot, 1 ),
    test_object2_a1 = a1( test_object2, 1 ), 
    test_object2_a2 = a2( test_object2, 1 ),
    test_object2_a3 = a3( test_object2@a_slot, 1 )
)

Unit: nanoseconds
            expr   min    lq     mean median      uq   max neval cld
  test_object_a1 15873 16897 20258.31  17409 23680.5 41217   100   c
  test_object_a2  1025  1282  1844.20   1537  1793.0 11265   100 a  
  test_object_a3  1024  1281  2289.62   1537  1921.5 20225   100 a  
 test_object2_a1  4609  5633  7256.03   6144  6657.0 28161   100  b 
 test_object2_a2  1025  1537  1787.85   1793  1793.5 11264   100 a  
 test_object2_a3   769  1281  2143.69   1537  1793.0 21248   100 a  

      

Reducing the speed of direct assignment ( a1()

) versus creating a new variable ( a2()

) or passing the environment as an argument to a function ( a3()

) does not seem to involve overhead [<-

, because resizing the vector within the environment ( test_object2

versus test_object1

) changes the size of the difference in speed between a1()

and a2()

.

I suspect direct assignment requires R to copy the object, although environments should not be copied. Does anyone know what's going on here?

+3


source to share





All Articles