Skip to contents

The dispatch performance should be roughly on par with S3 and S4, though as this is implemented in a package there is some overhead due to .Call vs .Primitive.

Text := new_class(parent = class_character)
Number := new_class(parent = class_double)

x <- Text("hi")
y <- Number(1)

foo_S7 := new_generic("x")
method(foo_S7, Text) <- function(x, ...) paste0(x, "-foo")

foo_S3 <- function(x, ...) {
  UseMethod("foo_S3")
}

foo_S3.Text <- function(x, ...) {
  paste0(x, "-foo")
}

library(methods)
setOldClass(c("Number", "numeric", "S7_object"))
setOldClass(c("Text", "character", "S7_object"))

setGeneric("foo_S4", function(x, ...) standardGeneric("foo_S4"))
#> [1] "foo_S4"
setMethod("foo_S4", c("Text"), function(x, ...) paste0(x, "-foo"))

# Measure performance of single dispatch
bench::mark(foo_S7(x), foo_S3(x), foo_S4(x))
#> # A tibble: 3 × 6
#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 foo_S7(x)    5.98µs   6.92µs   135132.    10.9KB     27.0
#> 2 foo_S3(x)    2.41µs   2.75µs   313950.        0B     31.4
#> 3 foo_S4(x)    2.53µs   2.98µs   312865.        0B     31.3

bar_S7 := new_generic(c("x", "y"))
method(bar_S7, list(Text, Number)) <- function(x, y, ...) paste0(x, "-", y, "-bar")

setGeneric("bar_S4", function(x, y, ...) standardGeneric("bar_S4"))
#> [1] "bar_S4"
setMethod("bar_S4", c("Text", "Number"), function(x, y, ...) paste0(x, "-", y, "-bar"))

# Measure performance of double dispatch
bench::mark(bar_S7(x, y), bar_S4(x, y))
#> # A tibble: 2 × 6
#>   expression        min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>   <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 bar_S7(x, y)  12.08µs   13.9µs    68870.        0B     27.6
#> 2 bar_S4(x, y)   6.57µs   7.67µs   125663.        0B     25.1

A potential optimization is caching based on the class names, but lookup should be fast without this.

The following benchmark generates a class hierarchy of different levels and lengths of class names and compares the time to dispatch on the first class in the hierarchy vs the time to dispatch on the last class.

We find that even in very extreme cases (e.g. 100 deep hierarchy 100 of character class names) the overhead is reasonable, and for more reasonable cases (e.g. 10 deep hierarchy of 15 character class names) the overhead is basically negligible.

library(S7)

gen_character <- function (n, min = 5, max = 25, values = c(letters, LETTERS, 0:9)) {
  lengths <- sample(min:max, replace = TRUE, size = n)
  values <- sample(values, sum(lengths), replace = TRUE)
  starts <- c(1, cumsum(lengths)[-n] + 1)
  ends <- cumsum(lengths)
  mapply(function(start, end) paste0(values[start:end], collapse=""), starts, ends)
}

bench::press(
  num_classes = c(3, 5, 10, 50, 100),
  class_nchar = c(15, 100),
  {
    # Construct a class hierarchy with that number of classes
    Text := new_class(parent = class_character)
    parent <- Text
    classes <- gen_character(num_classes, min = class_nchar, max = class_nchar)
    env <- new.env()
    for (x in classes) {
      assign(x, new_class(x, parent = parent), env)
      parent <- get(x, env)
    }

    # Get the last defined class
    cls <- parent

    # Construct an object of that class
    x <- do.call(cls, list("hi"))

    # Define a generic and a method for the last class (best case scenario)
    foo_S7 := new_generic("x")
    method(foo_S7, cls) <- function(x, ...) paste0(x, "-foo")

    # Define a generic and a method for the first class (worst case scenario)
    foo2_S7 := new_generic("x")
    method(foo2_S7, S7_object) <- function(x, ...) paste0(x, "-foo")

    bench::mark(
      best = foo_S7(x),
      worst = foo2_S7(x)
    )
  }
)
#> # A tibble: 20 × 8
#>    expression num_classes class_nchar      min   median `itr/sec` mem_alloc `gc/sec`
#>    <bch:expr>       <dbl>       <dbl> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#>  1 best                 3          15   5.95µs   7.03µs   135940.        0B    27.2 
#>  2 worst                3          15   6.18µs   7.21µs   132580.        0B    26.5 
#>  3 best                 5          15   5.96µs   7.01µs   136247.        0B    27.3 
#>  4 worst                5          15   6.23µs   7.28µs   131432.        0B    26.3 
#>  5 best                10          15   5.99µs   7.07µs   135168.        0B    27.0 
#>  6 worst               10          15   6.38µs   7.42µs   129168.        0B    25.8 
#>  7 best                50          15   6.13µs   7.28µs   131173.        0B    26.2 
#>  8 worst               50          15   7.86µs   9.04µs   106184.        0B    21.2 
#>  9 best               100          15   6.34µs   7.48µs   127763.        0B    25.6 
#> 10 worst              100          15   9.82µs  10.92µs    88644.        0B    17.7 
#> 11 best                 3         100   6.11µs   7.29µs   130485.        0B    26.1 
#> 12 worst                3         100   6.48µs   7.61µs   124491.        0B    24.9 
#> 13 best                 5         100   6.12µs    7.3µs   129701.        0B    25.9 
#> 14 worst                5         100    6.5µs   7.75µs   122231.        0B    24.5 
#> 15 best                10         100    6.1µs   7.24µs   131076.        0B    26.2 
#> 16 worst               10         100   6.88µs   8.06µs   118132.        0B    23.6 
#> 17 best                50         100   6.14µs   7.39µs   125281.        0B    25.1 
#> 18 worst               50         100  11.92µs  13.12µs    73603.        0B    14.7 
#> 19 best               100         100   6.44µs   7.63µs   124228.        0B    24.9 
#> 20 worst              100         100  18.51µs  19.74µs    49349.        0B     9.87

And the same benchmark using double-dispatch

bench::press(
  num_classes = c(3, 5, 10, 50, 100),
  class_nchar = c(15, 100),
  {
    # Construct a class hierarchy with that number of classes
    Text := new_class(parent = class_character)
    parent <- Text
    classes <- gen_character(num_classes, min = class_nchar, max = class_nchar)
    env <- new.env()
    for (x in classes) {
      assign(x, new_class(x, parent = parent), env)
      parent <- get(x, env)
    }

    # Get the last defined class
    cls <- parent

    # Construct an object of that class
    x <- do.call(cls, list("hi"))
    y <- do.call(cls, list("ho"))

    # Define a generic and a method for the last class (best case scenario)
    foo_S7 := new_generic(c("x", "y"))
    method(foo_S7, list(cls, cls)) <- function(x, y, ...) paste0(x, y, "-foo")

    # Define a generic and a method for the first class (worst case scenario)
    foo2_S7 := new_generic(c("x", "y"))
    method(foo2_S7, list(S7_object, S7_object)) <- function(x, y, ...) paste0(x, y, "-foo")

    bench::mark(
      best = foo_S7(x, y),
      worst = foo2_S7(x, y)
    )
  }
)
#> # A tibble: 20 × 8
#>    expression num_classes class_nchar      min   median `itr/sec` mem_alloc `gc/sec`
#>    <bch:expr>       <dbl>       <dbl> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#>  1 best                 3          15   8.31µs  10.04µs    93873.        0B    28.2 
#>  2 worst                3          15    8.8µs  10.43µs    90352.        0B    27.1 
#>  3 best                 5          15   8.35µs  10.03µs    93250.        0B    28.0 
#>  4 worst                5          15   8.97µs  10.62µs    88949.        0B    26.7 
#>  5 best                10          15    8.4µs  10.17µs    92674.        0B    27.8 
#>  6 worst               10          15   9.27µs  10.99µs    87203.        0B    17.4 
#>  7 best                50          15   8.81µs  10.43µs    90241.        0B    27.1 
#>  8 worst               50          15  12.13µs  13.98µs    68628.        0B    20.6 
#>  9 best               100          15   9.05µs   10.8µs    87525.        0B    26.3 
#> 10 worst              100          15  16.09µs  17.86µs    53737.        0B    16.1 
#> 11 best                 3         100   8.64µs  10.46µs    90303.        0B    27.1 
#> 12 worst                3         100   9.68µs  11.37µs    83657.        0B    25.1 
#> 13 best                 5         100   8.46µs  10.17µs    92679.        0B    27.8 
#> 14 worst                5         100    9.6µs  10.61µs    89381.        0B    26.8 
#> 15 best                10         100    8.4µs   8.94µs   107117.        0B    32.1 
#> 16 worst               10         100  10.96µs  11.62µs    82376.        0B    24.7 
#> 17 best                50         100   8.95µs   9.52µs   101260.        0B    30.4 
#> 18 worst               50         100  19.51µs  20.09µs    48684.        0B    14.6 
#> 19 best               100         100   9.13µs   9.67µs    99431.        0B    29.8 
#> 20 worst              100         100  30.64µs  31.26µs    31438.        0B     9.43