Nothing
#####
## DO NOT EDIT THIS FILE!! EDIT THE SOURCE INSTEAD: rsrc_tree/zzz_R_specific/s7_dispatch_perf.R
#####
## R-SPECIFIC: fast concrete-class membership test (perf swap for S7_inherits)
##
## ## PERF-S7IS ---------------------------------------------------------------
## WHY: S7_inherits(x, ConcreteCls) costs ~2.5us/call because it rebuilds the
## class name through S7's internal S7_class_name -> paste() on EVERY call. For a
## CONCRETE, package-qualified S7 class, membership is exactly testable against
## the cached S3 class() vector that every S7 object already carries, e.g.
## class(Variable(3)) ==
## c("CVXR::Variable","CVXR::Leaf","CVXR::Expression","CVXR::Canonical","S7_object")
## so inherits(x, "CVXR::Variable") is TAUTOLOGICALLY equal to
## S7_inherits(x, Variable) for concrete CVXR classes, at ~5.5x the speed (the
## FQN is memoized per class, so no paste per call).
## Background: notes/v19_compute_overhead_investigation.md (the +45% S7_inherits
## call-count regression vs 1.8.2-1; this lever closes it AND speeds base too).
##
## CORRECTNESS SCOPE — valid ONLY for CONCRETE classes with package = "CVXR":
## * NOT S7 unions / abstract classes (no single class() string represents them;
## CVXR currently has zero `new_union`, but never swap one if added).
## * NOT base-type wrappers (class_numeric/class_character/...); S7_inherits
## errors on those anyway, so they are never legal swap sites.
## Pass the class OBJECT (same signature as S7_inherits) so swapping a call site
## changes only the function NAME, never a string literal.
##
## SINGLE-POINT REVERT: replace the body of .s7_is / .s7_is_any below with a
## direct S7_inherits(x, cls) / loop, and the entire optimization is undone (call
## sites are unchanged because they pass the class object). Find every swapped
## site with: grep -rn '\.s7_is' rsrc_tree
##
## SAFETY NET: test-s7is-equivalence.R asserts .s7_is(inst, Cls) == S7_inherits(
## inst, Cls) for every swapped class (positive + negative instance + NULL/numeric)
## and runs in R CMD check, so it FAILS LOUDLY if S7's class() layout ever changes
## (that failure is the revert trigger).
## ---------------------------------------------------------------------------
## Per-class memo of the fully-qualified "<package>::<name>" string. Keyed by the
## bare class name (unique within CVXR), so the only per-call cost is an attr()
## read + an environment lookup -- never a paste.
.s7is_fqn_cache <- new.env(parent = emptyenv())
.s7_fqn <- function(cls) {
nm <- attr(cls, "name")
fqn <- .s7is_fqn_cache[[nm]]
if (is.null(fqn)) {
fqn <- paste0(attr(cls, "package"), "::", nm)
.s7is_fqn_cache[[nm]] <- fqn
}
fqn
}
## Fast equivalent of S7_inherits(x, cls) for a CONCRETE CVXR class object.
.s7_is <- function(x, cls) inherits(x, .s7_fqn(cls))
## Fast equivalent of .inherits_any(x, classes) -- any-of over class OBJECTS.
.s7_is_any <- function(x, classes) {
for (cls in classes) if (inherits(x, .s7_fqn(cls))) return(TRUE)
FALSE
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.