我注意到,当我叫args
上一些的基本功能,字节码显示为好。但是在其他原语上,没有字节码出现。例如
args(length)
# function (x)
# NULL
args(list)
# function (...)
# NULL
# <bytecode: 0x44a0f38>
这是为什么?
起初,我认为这可能与该...
论点有关,但以下观点反驳了该理论。
args(dim)
# function (x)
# NULL
args(unclass)
# function (x)
# NULL
# <bytecode: 0x44a0450>
令我困惑的是,字节码仅在其中一些中显示,而在其他一些中则不显示。我一直给人的印象是,所有基元都是特殊的,并且它们都共享相同的“属性”(因为缺少更好的词,而不是实际的R属性)。
正如研究表明的那样,这与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>
我们可以将字节码行的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))
值得注意的是,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<-")
反之则不正确:一些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
因此,基本软件包中的非原始函数都已编译,但未args
提及。原始函数在打印时不显示字节码消息,仅在用args调用时有时显示字节码消息。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句