Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R bytecode reference in function code

When examining the R code for a function, it lists a bytecode (for example, for glm): . I've looked everywhere for a simple explanation for what "0x7f8f3c954540" actually is. I know what bytecode is, but I'm assuming "0x7f8f3c954540" isn't actually a highly compressed machine-processable version of the whole code for the glm function. Is it a CRC, a link to its location in memory, or the first few bits?

like image 835
Kaister Avatar asked Jan 09 '23 19:01

Kaister


1 Answers

If you look at the code in print.c in the R source, you can see that line is printed with

Rprintf("<bytecode: %p>\n", BODY(s));

This means it's printing the pointer address to the compiled version of the body of the function.

So the same function body code may point to two different addresses. Observe

f1<-function(a) {
    sum(runif(a) * 1/(1:a))
}
f2 <- f1
f1 <- compiler:::cmpfun(f1)
f2 <- compiler:::cmpfun(f2)
f3 <- f2

f1
# function(a) {
#       sum(runif(a) * 1/(1:a))
#    }
#<bytecode: 0x111cbb358>

f2
# function(a) {
#       sum(runif(a) * 1/(1:a))
#     }
# <bytecode: 0x111bd7800>

f3
#  function(a) {
#       sum(runif(a) * 1/(1:a))
#    }
#<bytecode: 0x111bd7800>

You can see that even though f1 and f2 have the same body code, they have different bytecode values. If you wanted to see if these two functions were identical you could use

identical(f1,f2)
# [1] TRUE
identical(f2,f3)
# [1] TRUE

but note the default is to ignore the compiled part of the function. If you wanted to see if they pointed to the same bytyecode as well

identical(f1,f2, ignore.bytecode=FALSE)
# [1] FALSE
identical(f2,f3, ignore.bytecode=FALSE)
# [1] TRUE

So the bytecode value doesn't tell you anything about the code itself necessarily.

like image 84
MrFlick Avatar answered Jan 18 '23 09:01

MrFlick