Rev 77328 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
---title: "Immediate Binding Values"author: Luke Tierneyoutput: html_document---## BackgroundFor scalar numerical code it can help to allow variable bindings tohold scalar integer, logical, and double values as immediate valuesrather than as allocated scalar vectors, or _boxed_ values. Thiseliminates the overhead of checking whether they might be shared orhave attributes. It also makes inlining scalar computations for basicarithmetic operations and element access in the byte code engine moreeffective. The combined benefit can be as high as 20% for someexamples, including the convolution example from the extensionsmanual. Having immediate bindings also allows some brittleoptimizations for updating scalar variable bindings and loop indicesto be removed.This note reflects changed committed to R_devel in r77327.)## InterfaceBinding cells have a marker that is returned by `BNDCELL_TAG`. Themarker, or tag, is zero for standard bindings, and one of `REALSXP`,`INTSXP`, or `LGLSXP` for immediate bindings.`BINDING_VALUE`, used only in `eval.c` and `envir.c`, always returnsan allocated object as the value of a binding. For immediate bindingsit first converts to a standard binding by allocating and installing ascalar vector of the appropriate type. This allows most code to beunaware of the existence of typed bindings. The allocation is done by`R_expand_binding_value`.Code that wants to take advantage of typed bindings can read and settheir values with- `INTSXP`: `BNDCELL_IVAL(cell)`, `SET_BNDCELL_IVAL(cell, val)`- `LGLSXP`: `BNDCELL_LVAL(cell)`, `SET_BNDCELL_LVAL(cell, val)`- `REALSXP`:`BNDCELL_DVAL(cell)`, `SET_BNDCELL_DVAL(cell, val)`These do not check or set the type tag. To create and initialize a newimmediate binding in a cell use- `INTSXP`: `NEW_BNDCELL_IVAL(cell, val)`- `LGLSXP`: `NEW_BNDCELL_LVAL(cell, val)`- `REALSXP`:`NEW_BNDCELL_DVAL(cell, val)`The generic `CAR` accessor has been modified to signal an error if itencounters a cell with an immediate `CAR` value. This ensuresimmediate values are only used in the context of bindings. This makesit easier to avoid inadvertent boxing and may help with a transitionto a different environment and binding representation.The setters, such as `SETCAR`, clear an immediate binding markerwithout signaling an error.## Notes- For now, the `sxpinfo.extra` field is used to hold the bindingtag.- Two implementations are provided for representing the immediatevalues. One replaces the `SEXP` `CAR` field by a union; he otherallocates a boxed value. The union representation is conceptuallymore natural and a little more efficient. But it would require achange in memory layout on 32-bit platforms since the unionrequires 8 bytes for the `double` value while a pointer onlyrequires 4 bytes. On 64-bit hardware the union approach should notchange the memory layout.For now, the union approach is used on 64-bit platforms and theboxed approach on 32-bit ones. It would be best to use the unionapproach unconditionally, but this would require changing thebinary version and rebuilding all packages with compiled code.This should probably be done before release.- The approach taken for now is to just allow immediate values inthe `CAR` of binding cells. An alternative would be to allowimmediate values in all `CONS` cells, or even more widely, such asin vector element. Allowing immediate values in all `CONS` cellswould have been a little simpler. But it would have make it harderto detect unintended boxing, and might also have made it harder totransition to an alternate environment or binding representationshould we wish to do that.In case immediate values are to be supported more widely the GC issuspended when when boxing values in `R_expand_binding_value`.- Serialization handles environment frames with standard pairlistcode, so the code not checks for an immediate binding and boxesthe value if necessary. An alternative would be to update theserialization format to support immediate bindings. But given howchallenging it is to change the format it seemed best just to box.- Only unlocked standard environment bindings that can be cached canbe turned into immediate bindings. Symbol bindings for the baseenvironment are not cached, and bindings for user data bases arelocked when returned by `findVarLoc` or findVarLocInFrame, soneither of these can become immediate bindings.- `BINDING_VALUE` is defined slightly differently in `eval.c` and`envir.c`. It would be good to unify these eventually.<!--Local Variables:mode: poly-markdown+Rmode: flyspellEnd:-->