我注意到,当我叫args
上一些的基本功能,字节码显示为好.但在其他原语上,不会出现字节码.例如
args(length)
# function (x)
# NULL
args(list)
# function (...)
# NULL
# <bytecode: 0x44a0f38>
Run Code Online (Sandbox Code Playgroud)
这是为什么?
起初我认为这可能与...
争论有关,但以下反驳了这一理论.
args(dim)
# function (x)
# NULL
args(unclass)
# function (x)
# NULL
# <bytecode: 0x44a0450>
Run Code Online (Sandbox Code Playgroud)
令我困惑的是,字节码只出现在其中一些中,而不出现在其他中.我一直认为所有原语都是特殊的,并且它们都共享相同的"属性"(缺少更好的单词,而不是实际的R属性).
正如agstudy指出的那样,这与如何args
打印事物有关.也就是说,args
在其输出中是否包含字节码行并不是该函数是否是字节编译的可靠指示.相比:
args(writeLines)
## function (text, con = stdout(), sep = "\n", useBytes = FALSE)
## NULL
writeLines
## function (text, con = stdout(), sep = "\n", useBytes = FALSE)
## {
## if (is.character(con)) {
## con <- file(con, "w")
## on.exit(close(con))
## }
## .Internal(writeLines(text, con, sep, useBytes))
## }
## <bytecode: 0x000000001bf3aeb0>
Run Code Online (Sandbox Code Playgroud)
我们可以比较字节码行的args
打印与标准功能打印.
arg_shows_bytecode <- function(fn)
{
output <- capture.output(args(fn))
grepl("^<bytecode", output[length(output)])
}
printing_shows_bytecode <- function(fn)
{
output <- capture.output(print(fn))
length(output) > 1 && grepl("^<bytecode", output[length(output) - 1])
}
base_fns <- Filter(is.function, mget(ls(baseenv()), baseenv()))
yn_args <- vapply(base_fns, arg_shows_bytecode, logical(1))
yn_print <- vapply(base_fns, printing_shows_bytecode, logical(1))
Run Code Online (Sandbox Code Playgroud)
值得注意的是,args
显示字节码信息的所有函数都是原语.
head(base_fns[yn_args])
## $`%*%`
## function (x, y) .Primitive("%*%")
##
## $as.call
## function (x) .Primitive("as.call")
##
## $attr
## function (x, which, exact = FALSE) .Primitive("attr")
##
## $`attr<-`
## function (x, which, value) .Primitive("attr<-")
##
## $attributes
## function (obj) .Primitive("attributes")
##
## $`attributes<-`
## function (obj, value) .Primitive("attributes<-")
Run Code Online (Sandbox Code Playgroud)
相反的情况并非如此:一些args
不显示字节码信息的基函数是基元; 其他人不是.
yn_prim <- vapply(base_fns, is.primitive, logical(1))
table(yn_args, yn_print, yn_prim)
## , , yn_prim = FALSE
##
## yn_print
## yn_args FALSE TRUE
## FALSE 0 988
## TRUE 0 0
##
## , , yn_prim = TRUE
##
## yn_print
## yn_args FALSE TRUE
## FALSE 119 0
## TRUE 63 0
Run Code Online (Sandbox Code Playgroud)
所以基础包中的非原始函数都被编译,但args
没有提到它.原始函数在打印时不显示字节码消息,并且仅在使用args调用时有时显示字节码消息.