Debian Codegraph @

R/Rprofmem-class.R

raw
1 #' Total number of bytes allocated
2 #'
3 #' @param x An `Rprofmem` object.
4 #' @param ... Not used.
5 #'
6 #' @return A non-negative numeric.
7 #'
8 #' @aliases total.Rprofmem subset.Rprofmem
9 #' @export
10 #' @keywords internal
11 total <- function(x, ...) UseMethod("total")
12
13 #' @export
14 total.Rprofmem <- function(x, ...) {
15 sum(x$bytes, na.rm=TRUE)
16 }
17
18 #' @export
19 c.Rprofmem <- function(...) {
20 args <- list(...)
21
22 what <- NULL
23 bytes <- NULL
24 trace <- NULL
25 threshold <- NULL
26
27 for (arg in args) {
28 stop_if_not(inherits(arg, "Rprofmem"))
29 what <- c(what, arg$what)
30 bytes <- c(bytes, arg$bytes)
31 trace <- c(trace, arg$trace)
32 threshold <- c(threshold, attr(arg, "threshold"))
33 }
34 threshold <- max(threshold)
35 stop_if_not(length(threshold) == 1, is.finite(threshold),
36 is.integer(threshold), threshold >= 0L)
37
38 res <- data.frame(what = what, bytes = bytes, stringsAsFactors = FALSE)
39 res$trace <- trace
40 if (threshold > 0L) {
41 keep <- is.na(bytes) | (bytes >= threshold)
42 stop_if_not(all(is.finite(keep)))
43 res <- res[keep, ]
44 }
45 attr(res, "threshold") <- threshold
46 class(res) <- c("Rprofmem", class(res))
47 ## Sanity check
48 stop_if_not(all(c("what", "bytes", "trace") %in% names(res)))
49
50 res
51 }
52
53 #' @export
54 subset.Rprofmem <- function(x, ...) {
55 res <- NextMethod()
56 attr(res, "expression") <- attr(x, "expression")
57 attr(res, "threshold") <- attr(x, "threshold")
58 res
59 }
60
61 #' @export
62 as.data.frame.Rprofmem <- function(x, ...) {
63 what <- x$what
64 bytes <- x$bytes
65 traces <- unlist(lapply(x$trace, FUN=function(x) {
66 trace <- rev(x)
67 hasName <- !grepl("^<[^>]*>$", trace)
68 trace[hasName] <- sprintf("%s()", trace[hasName])
69 paste(trace, collapse=" -> ")
70 }))
71
72 res <- data.frame(what = what, bytes = bytes, calls = traces,
73 stringsAsFactors = FALSE)
74
75 ## Preserve row names
76 rownames(res) <- rownames(x)
77
78 res
79 } ## as.data.frame()
80
81
82 #' @export
83 print.Rprofmem <- function(x, expr = getOption("profmem.print.expr", TRUE), newpage = getOption("profmem.print.newpage", FALSE), calls = getOption("profmem.print.calls", TRUE), ...) {
84 if (expr && "expression" %in% names(attributes(x))) {
85 cat("Rprofmem memory profiling of:\n")
86 print(attr(x, "expression"))
87 cat("\n")
88 }
89
90 threshold <- attr(x, "threshold")
91 if (is.null(threshold) || threshold == 0L) {
92 cat("Memory allocations:\n")
93 } else {
94 cat(sprintf("Memory allocations (>= %g bytes):\n", threshold))
95 }
96
97 data <- as.data.frame(x, ..., stringsAsFactors = FALSE)
98
99 if (!newpage) {
100 drop <- which(x$what == "new page")
101 if (length(drop) > 0) {
102 cat(sprintf("Number of 'new page' entries not displayed: %d\n",
103 length(drop)))
104 data <- data[-drop, ]
105 }
106 }
107
108 n <- nrow(data)
109 total <- sum(data$bytes, na.rm=TRUE)
110
111 ## Number of digits for indices
112 widx <- floor(log10(n)+1)
113
114 ## Number of digits for bytes
115 wbytes <- floor(log10(total)+1)
116
117 ## Report empty call stack as "internal"
118 data$calls[!nzchar(data$calls)] <- "<internal>"
119
120 data <- rbind(data, list(what = "", bytes = total, calls = ""))
121 rownames(data)[n+1] <- "total"
122
123 if (!calls) {
124 data$calls <- NULL
125 }
126
127 print(data, ...)
128
129 ## Any errors to report on?
130 error <- attr(x, "error")
131 if (!is.null(error)) {
132 cat(sprintf("\nNote, an error occurred while evaluating the expression: %s\n", conditionMessage(error)))
133 }
134
135 invisible(x)
136 } ## print()