Skip to content

Commit 3faebd8

Browse files
committed
add print methods to all functions
1 parent 49234bd commit 3faebd8

39 files changed

Lines changed: 557 additions & 152 deletions

NAMESPACE

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
S3method(print,debrief_call_depth)
4+
S3method(print,debrief_call_stats)
5+
S3method(print,debrief_callers_callees)
6+
S3method(print,debrief_compare)
7+
S3method(print,debrief_compare_many)
8+
S3method(print,debrief_debrief)
9+
S3method(print,debrief_file_summary)
10+
S3method(print,debrief_flame)
11+
S3method(print,debrief_flame_condense)
12+
S3method(print,debrief_focus)
13+
S3method(print,debrief_gc_pressure)
14+
S3method(print,debrief_help)
15+
S3method(print,debrief_hot_lines)
16+
S3method(print,debrief_hot_paths)
17+
S3method(print,debrief_memory)
18+
S3method(print,debrief_recursive)
19+
S3method(print,debrief_source_context)
20+
S3method(print,debrief_suggestions)
321
export(pv_call_depth)
422
export(pv_call_stats)
523
export(pv_callees)

R/antipatterns.R

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ empty_gc_pressure_df <- function() {
8888
#' @param x A profvis object.
8989
#' @param threshold Minimum GC percentage to report (default 10).
9090
#'
91-
#' @return Invisibly returns the GC pressure data frame.
91+
#' @return Invisibly returns a `debrief_gc_pressure` object. Use
92+
#' `capture.output()` to capture the formatted text output.
9293
#'
9394
#' @examples
9495
#' p <- pv_example("gc")
@@ -100,6 +101,18 @@ pv_print_gc_pressure <- function(x, threshold = 10) {
100101
check_empty_profile(x)
101102

102103
gc_data <- pv_gc_pressure(x, threshold = threshold)
104+
obj <- structure(
105+
list(gc_data = gc_data, threshold = threshold),
106+
class = "debrief_gc_pressure"
107+
)
108+
print(obj)
109+
invisible(obj)
110+
}
111+
112+
#' @exportS3Method
113+
print.debrief_gc_pressure <- function(x, ...) {
114+
gc_data <- x$gc_data
115+
threshold <- x$threshold
103116

104117
cat_header("GC PRESSURE")
105118
cat("\n")
@@ -110,7 +123,7 @@ pv_print_gc_pressure <- function(x, threshold = 10) {
110123
threshold
111124
))
112125
cat_help_hint()
113-
return(invisible(gc_data))
126+
return(invisible(x))
114127
}
115128

116129
row <- gc_data[1, ]
@@ -128,5 +141,5 @@ pv_print_gc_pressure <- function(x, threshold = 10) {
128141
"pv_suggestions(p)"
129142
))
130143

131-
invisible(gc_data)
144+
invisible(x)
132145
}

R/call-depth.R

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ pv_call_depth <- function(x) {
4747
#'
4848
#' @param x A profvis object.
4949
#'
50-
#' @return Invisibly returns the call depth data frame.
50+
#' @return Invisibly returns a `debrief_call_depth` object. Use
51+
#' `capture.output()` to capture the formatted text output.
5152
#'
5253
#' @examples
5354
#' p <- pv_example()
@@ -59,11 +60,19 @@ pv_print_call_depth <- function(x) {
5960
check_empty_profile(x)
6061

6162
depth_df <- pv_call_depth(x)
63+
obj <- structure(list(depth_df = depth_df), class = "debrief_call_depth")
64+
print(obj)
65+
invisible(obj)
66+
}
67+
68+
#' @exportS3Method
69+
print.debrief_call_depth <- function(x, ...) {
70+
depth_df <- x$depth_df
6271

6372
if (nrow(depth_df) == 0) {
6473
cat("No profiling data available.\n")
6574
cat_help_hint()
66-
return(invisible(depth_df))
75+
return(invisible(x))
6776
}
6877

6978
cat_header("CALL DEPTH BREAKDOWN")
@@ -99,5 +108,5 @@ pv_print_call_depth <- function(x) {
99108
}
100109
}
101110

102-
invisible(depth_df)
111+
invisible(x)
103112
}

R/call-stats.R

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ estimate_call_count <- function(func, prof) {
120120
#' @param x A profvis object.
121121
#' @param n Number of functions to show.
122122
#'
123-
#' @return Invisibly returns the call stats data frame.
123+
#' @return Invisibly returns a `debrief_call_stats` object. Use
124+
#' `capture.output()` to capture the formatted text output.
124125
#'
125126
#' @examples
126127
#' p <- pv_example()
@@ -132,11 +133,19 @@ pv_print_call_stats <- function(x, n = 20) {
132133
check_empty_profile(x)
133134

134135
stats <- pv_call_stats(x, n = n)
136+
obj <- structure(list(stats = stats), class = "debrief_call_stats")
137+
print(obj)
138+
invisible(obj)
139+
}
140+
141+
#' @exportS3Method
142+
print.debrief_call_stats <- function(x, ...) {
143+
stats <- x$stats
135144

136145
if (nrow(stats) == 0) {
137146
cat("No profiling data available.\n")
138147
cat_help_hint()
139-
return(invisible(stats))
148+
return(invisible(x))
140149
}
141150

142151
cat_header("CALL STATISTICS")
@@ -176,5 +185,5 @@ pv_print_call_stats <- function(x, n = 20) {
176185
}
177186
}
178187

179-
invisible(stats)
188+
invisible(x)
180189
}

R/callers-callees.R

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ find_adjacent_functions <- function(
121121
#' @param func The function name to analyze.
122122
#' @param n Maximum number of callers/callees to show.
123123
#'
124-
#' @return Invisibly returns a list with `callers` and `callees` data frames.
124+
#' @return Invisibly returns a `debrief_callers_callees` object. Use
125+
#' `capture.output()` to capture the formatted text output.
125126
#'
126127
#' @examples
127128
#' p <- pv_example()
@@ -141,6 +142,32 @@ pv_print_callers_callees <- function(x, func, n = 10) {
141142
func_total_time <- length(func_times) * pd$interval_ms
142143
func_pct <- round(100 * length(func_times) / pd$total_samples, 1)
143144

145+
obj <- structure(
146+
list(
147+
callers = callers,
148+
callees = callees,
149+
func = func,
150+
func_times = func_times,
151+
func_total_time = func_total_time,
152+
func_pct = func_pct,
153+
n = n
154+
),
155+
class = "debrief_callers_callees"
156+
)
157+
print(obj)
158+
invisible(obj)
159+
}
160+
161+
#' @exportS3Method
162+
print.debrief_callers_callees <- function(x, ...) {
163+
callers <- x$callers
164+
callees <- x$callees
165+
func <- x$func
166+
func_times <- x$func_times
167+
func_total_time <- x$func_total_time
168+
func_pct <- x$func_pct
169+
n <- x$n
170+
144171
cat_header(sprintf("FUNCTION ANALYSIS: %s", func))
145172
cat("\n")
146173
cat(sprintf(
@@ -194,5 +221,5 @@ pv_print_callers_callees <- function(x, func, n = 10) {
194221
}
195222
cat_next_steps(suggestions)
196223

197-
invisible(list(callers = callers, callees = callees))
224+
invisible(x)
198225
}

R/compare.R

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ pv_compare <- function(before, after, n = 20) {
106106
#' @param after A profvis object (after optimization).
107107
#' @param n Number of functions to show in detailed comparison.
108108
#'
109-
#' @return Invisibly returns the comparison list.
109+
#' @return Invisibly returns a `debrief_compare` object. Use
110+
#' `capture.output()` to capture the formatted text output.
110111
#'
111112
#' @examples
112113
#' p1 <- pv_example()
@@ -119,6 +120,15 @@ pv_print_compare <- function(before, after, n = 15) {
119120
check_profvis(after)
120121

121122
comp <- pv_compare(before, after, n = n)
123+
obj <- structure(list(comp = comp, n = n), class = "debrief_compare")
124+
print(obj)
125+
invisible(obj)
126+
}
127+
128+
#' @exportS3Method
129+
print.debrief_compare <- function(x, ...) {
130+
comp <- x$comp
131+
n <- x$n
122132

123133
cat_header("PROFILE COMPARISON")
124134
cat("\n")
@@ -215,7 +225,7 @@ pv_print_compare <- function(before, after, n = 15) {
215225
cat_next_steps(suggestions)
216226
}
217227

218-
invisible(comp)
228+
invisible(x)
219229
}
220230

221231
#' Compare multiple profvis profiles
@@ -306,7 +316,8 @@ pv_compare_many <- function(...) {
306316
#'
307317
#' @param ... Named profvis objects to compare, or a single named list.
308318
#'
309-
#' @return Invisibly returns the comparison data frame.
319+
#' @return Invisibly returns a `debrief_compare_many` object. Use
320+
#' `capture.output()` to capture the formatted text output.
310321
#'
311322
#' @examples
312323
#' p1 <- pv_example()
@@ -315,6 +326,14 @@ pv_compare_many <- function(...) {
315326
#' @export
316327
pv_print_compare_many <- function(...) {
317328
result <- pv_compare_many(...)
329+
obj <- structure(list(result = result), class = "debrief_compare_many")
330+
print(obj)
331+
invisible(obj)
332+
}
333+
334+
#' @exportS3Method
335+
print.debrief_compare_many <- function(x, ...) {
336+
result <- x$result
318337

319338
cat_header("MULTI-PROFILE COMPARISON")
320339
cat("\n")
@@ -344,5 +363,5 @@ pv_print_compare_many <- function(...) {
344363

345364
cat("\n* = fastest\n")
346365

347-
invisible(result)
366+
invisible(x)
348367
}

R/debrief.R

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ pv_debrief <- function(x, n = 10) {
6464
#' @param n_paths Number of hot paths to show (default 5).
6565
#' @param n_memory Number of memory hotspots to show (default 5).
6666
#'
67-
#' @return Invisibly returns the result of [pv_debrief()].
67+
#' @return Invisibly returns a `debrief_debrief` object. Use
68+
#' `capture.output()` to capture the formatted text output.
6869
#'
6970
#' @examples
7071
#' p <- pv_example()
@@ -95,6 +96,52 @@ pv_print_debrief <- function(
9596
memory_funcs <- pv_memory(x)
9697
memory_lines <- if (has_source) pv_memory_lines(x) else NULL
9798

99+
obj <- structure(
100+
list(
101+
interval_ms = interval_ms,
102+
total_samples = total_samples,
103+
total_time_ms = total_time_ms,
104+
has_source = has_source,
105+
file_contents = file_contents,
106+
self_time = self_time,
107+
total_time = total_time,
108+
hot_lines = hot_lines,
109+
hot_paths = hot_paths,
110+
memory_funcs = memory_funcs,
111+
memory_lines = memory_lines,
112+
n_functions = n_functions,
113+
n_lines = n_lines,
114+
n_paths = n_paths,
115+
n_memory = n_memory,
116+
debrief = pv_debrief(
117+
x,
118+
n = max(n_functions, n_lines, n_paths, n_memory)
119+
)
120+
),
121+
class = "debrief_debrief"
122+
)
123+
print(obj)
124+
invisible(obj)
125+
}
126+
127+
#' @exportS3Method
128+
print.debrief_debrief <- function(x, ...) {
129+
interval_ms <- x$interval_ms
130+
total_samples <- x$total_samples
131+
total_time_ms <- x$total_time_ms
132+
has_source <- x$has_source
133+
file_contents <- x$file_contents
134+
self_time <- x$self_time
135+
total_time <- x$total_time
136+
hot_lines <- x$hot_lines
137+
hot_paths <- x$hot_paths
138+
memory_funcs <- x$memory_funcs
139+
memory_lines <- x$memory_lines
140+
n_functions <- x$n_functions
141+
n_lines <- x$n_lines
142+
n_paths <- x$n_paths
143+
n_memory <- x$n_memory
144+
98145
# Print output
99146
cat_header("PROFILING SUMMARY")
100147
cat("\n")
@@ -152,7 +199,7 @@ pv_print_debrief <- function(
152199
suggestions <- c(suggestions, "pv_suggestions(p)", "pv_help()")
153200
cat_next_steps(suggestions)
154201

155-
invisible(pv_debrief(x, n = max(n_functions, n_lines, n_paths, n_memory)))
202+
invisible(x)
156203
}
157204

158205
# Print helpers for debrief

R/file-summary.R

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ pv_file_summary <- function(x) {
4848
#'
4949
#' @param x A profvis object.
5050
#'
51-
#' @return Invisibly returns the file summary data frame.
51+
#' @return Invisibly returns a `debrief_file_summary` object. Use
52+
#' `capture.output()` to capture the formatted text output.
5253
#'
5354
#' @examples
5455
#' p <- pv_example()
@@ -60,12 +61,23 @@ pv_print_file_summary <- function(x) {
6061
check_empty_profile(x)
6162

6263
summary_df <- pv_file_summary(x)
64+
obj <- structure(
65+
list(summary_df = summary_df),
66+
class = "debrief_file_summary"
67+
)
68+
print(obj)
69+
invisible(obj)
70+
}
71+
72+
#' @exportS3Method
73+
print.debrief_file_summary <- function(x, ...) {
74+
summary_df <- x$summary_df
6375

6476
if (nrow(summary_df) == 0) {
6577
cat("No source location data available.\n")
6678
cat("Use devtools::load_all() to enable source references.\n")
6779
cat_help_hint()
68-
return(invisible(summary_df))
80+
return(invisible(x))
6981
}
7082

7183
cat_header("FILE SUMMARY")
@@ -90,5 +102,5 @@ pv_print_file_summary <- function(x) {
90102
))
91103
}
92104

93-
invisible(summary_df)
105+
invisible(x)
94106
}

0 commit comments

Comments
 (0)