Skip to content

Reduce overhead of session watcher task callback  #174

@renkun-ken

Description

@renkun-ken

Currently, we use utils::capture.output(utils::str(obj, max.level = 0, give.attr = FALSE)) to get the string representation of each object in global environment. The overhead will become more significant as there are more symbols in it.

Suppose the global environment contains as many symbols as base environment (more than 1200 visible symbols), the overhead of getting object metadata will be more than 300ms, i.e., on each user input, there will be an extra 300ms to wait for the metadata of objects to be gathered.

To reduce the overhead, we could avoid using capture.output and str and instead create a simple string that contains simple information such as class, type, length, dim to represent the object.

system.time(objs <- eapply(baseenv(), function(obj) {
  utils::capture.output(utils::str(obj, max.level = 0, give.attr = FALSE))
}))
#>    user  system elapsed 
#>   0.335   0.004   0.339
system.time(objs <- eapply(baseenv(), function(obj) {
  list(
    type = typeof(obj),
    length = length(obj),
    class = class(obj)
  )
}))
#>    user  system elapsed 
#>   0.013   0.001   0.013

If the time of writing files are included,

system.time({
  objs <- eapply(baseenv(), function(obj) {
    list(
      type = typeof(obj),
      length = length(obj),
      class = class(obj),
      str = trimws(utils::capture.output(utils::str(obj, max.level = 0, give.attr = FALSE))),
      names = names(obj)
    )
  })
  jsonlite::write_json(objs, "test.json", auto_unbox = TRUE, pretty = FALSE)
})
#>    user  system elapsed 
#>   0.557   0.011   0.570
system.time({
  objs <- eapply(baseenv(), function(obj) {
    list(
      type = typeof(obj),
      length = length(obj),
      class = class(obj),
      names = names(obj)
    )
  })
  jsonlite::write_json(objs, "test.json", auto_unbox = TRUE, pretty = FALSE)
})
#>    user  system elapsed 
#>   0.113   0.000   0.113

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions