Why do some primitives have byte-codes and some do not?

Rich Scriven

I've noticed that when I call args on some of the primitive functions, byte-codes show up as well. But on other primitives, no byte-code appears. For example

args(length)
# function (x) 
# NULL
args(list)
# function (...) 
# NULL
# <bytecode: 0x44a0f38>

Why is that?

At first I thought it might be related to the ... argument, but the following disproves that theory.

args(dim)
# function (x) 
# NULL
args(unclass)
# function (x) 
# NULL
# <bytecode: 0x44a0450>

It's confusing to me that a byte-code only shows up in some of these, and not in others. I have always been under the impression that all primitives are special and that they all share the same "attributes" (for lack of a better word, not the actual R attributes).

Richie Cotton

As agstudy noted, this is an oddity related to how args prints things. That is, whether args includes a bytecode line in its output isn't a reliable indicator of whether or not the function was byte compiled. compare:

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>

We can compare printing of a bytecode line for args vs. standard function printing.

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))

It's worth noting that all functions where args shows bytecode information are primitives.

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<-")

The converse isn't true: some base functions where args doesn't show bytecode information are primitives; others are not.

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

So non-primitive functions in the base package are all compiled, but args doesn't mention it. Primitive functions don't show a bytecode message when printed, and only sometimes show a bytecode message when called with args.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why do some laptops not have a GPU slot?

From Dev

Why do some routers not have a WAN port?

From Dev

Why do some of functions have pass

From Dev

In python, why do some byte strings have \x in them, and others dont?

From Dev

Why do some some files in /etc have a numeric prefix?

From Java

Why do some Linux system calls not have a wrapper, but are documented as if they do?

From Dev

Why do some hex numbers have a 2s compliment but some do not?

From Dev

Why do some array parameters have a comma after opening parentheses?

From Dev

Why do some clojure functions have dots at the end or beginning of their names?

From Dev

In python, why do you have (seemingly) to import some libraries twice?

From

Why do some websites have ?utf8=✓ in their title?

From Dev

Why do some AWS services require the requestor to have IAM policies?

From Dev

TortoiseGit: Why do some of my branches not have revision numbers?

From Java

Why do some Linux system calls have two man pages?

From Java

Why do some built-in Python functions only have pass?

From Dev

Why do some regex commands have opposite intepretations of '\' with various characters?

From Dev

Why do I have some trouble with fwrite() and fread()?

From Java

Why some stacktrace do not have line number in java

From Dev

Why do some rows in csv file have an invalid format?

From Dev

Why do some serial interfaces have many pins?

From Dev

Why do some ImageMagick flags have plus sign (+) and others minus (-)?

From Dev

Vim, why do I have some parts highlighted?

From Java

Why do some functions have underscores "__" before and after the function name?

From Java

Why do some functions have underscores "__" before and after the function name?

From Dev

Why do some Linux files have a 'd' suffix?

From Dev

Why do some types in Flow libdefs have dollar sign in the begining

From Dev

why do some lines not have semicolon in C#?

From Dev

Why do some DDR4 have pins of unequal length?

From Dev

Why do some php classes have an empty init() method?

Related Related

  1. 1

    Why do some laptops not have a GPU slot?

  2. 2

    Why do some routers not have a WAN port?

  3. 3

    Why do some of functions have pass

  4. 4

    In python, why do some byte strings have \x in them, and others dont?

  5. 5

    Why do some some files in /etc have a numeric prefix?

  6. 6

    Why do some Linux system calls not have a wrapper, but are documented as if they do?

  7. 7

    Why do some hex numbers have a 2s compliment but some do not?

  8. 8

    Why do some array parameters have a comma after opening parentheses?

  9. 9

    Why do some clojure functions have dots at the end or beginning of their names?

  10. 10

    In python, why do you have (seemingly) to import some libraries twice?

  11. 11

    Why do some websites have ?utf8=✓ in their title?

  12. 12

    Why do some AWS services require the requestor to have IAM policies?

  13. 13

    TortoiseGit: Why do some of my branches not have revision numbers?

  14. 14

    Why do some Linux system calls have two man pages?

  15. 15

    Why do some built-in Python functions only have pass?

  16. 16

    Why do some regex commands have opposite intepretations of '\' with various characters?

  17. 17

    Why do I have some trouble with fwrite() and fread()?

  18. 18

    Why some stacktrace do not have line number in java

  19. 19

    Why do some rows in csv file have an invalid format?

  20. 20

    Why do some serial interfaces have many pins?

  21. 21

    Why do some ImageMagick flags have plus sign (+) and others minus (-)?

  22. 22

    Vim, why do I have some parts highlighted?

  23. 23

    Why do some functions have underscores "__" before and after the function name?

  24. 24

    Why do some functions have underscores "__" before and after the function name?

  25. 25

    Why do some Linux files have a 'd' suffix?

  26. 26

    Why do some types in Flow libdefs have dollar sign in the begining

  27. 27

    why do some lines not have semicolon in C#?

  28. 28

    Why do some DDR4 have pins of unequal length?

  29. 29

    Why do some php classes have an empty init() method?

HotTag

Archive