Title: | The R Code Optimizer |
---|---|
Description: | Automatically apply different strategies to optimize R code. 'rco' functions take R code as input, and returns R code as output. |
Authors: | Juan Cruz Rodriguez [aut, cre], Yihui Xie [ctb] , Nicolás Wolovick [ctb] |
Maintainer: | Juan Cruz Rodriguez <[email protected]> |
License: | GPL-3 |
Version: | 1.0.2 |
Built: | 2024-11-11 15:34:13 UTC |
Source: | https://github.com/jcrodriguez1989/rco |
List of all the optimizer functions:
Conditional Threading opt_cond_thread
Constant Folding opt_constant_folding
Constant Propagation opt_constant_propagation
Dead Code Elimination opt_dead_code
Dead Store Elimination opt_dead_store
Dead Expression Elimination opt_dead_expr
Common Subexpression Elimination opt_common_subexpr
Loop-invariant Code Motion opt_loop_invariant
Memory Allocation opt_memory_alloc
all_optimizers
all_optimizers
An object of class list
of length 9.
Report possible optimizations in '.R' files.
generate_files_opt_report(files, optimizers = rco:::all_optimizers)
generate_files_opt_report(files, optimizers = rco:::all_optimizers)
files |
A character vector with paths to files to optimize. |
optimizers |
A named list of optimizer functions. |
Report possible optimizations in folder containing '.R' files.
generate_folder_opt_report( folder, optimizers = all_optimizers, pattern = "\\.R$", recursive = TRUE )
generate_folder_opt_report( folder, optimizers = all_optimizers, pattern = "\\.R$", recursive = TRUE )
folder |
Path to a directory with files to optimize. |
optimizers |
A named list of optimizer functions. |
pattern |
An optional regular expression. Only file names which match the regular expression will be optimized. |
recursive |
A logical value indicating whether or not files in subdirectories of 'folder' should be optimized as well. |
Report possible optimizations in '.R' code snippet.
generate_text_opt_report(text, optimizers = all_optimizers)
generate_text_opt_report(text, optimizers = all_optimizers)
text |
A character vector with the code to optimize. |
optimizers |
A named list of optimizer functions. |
List of all the optimizer functions, with maximum optimization techniques enabled. Note that using this optimizers could change the semantics of the program!
Conditional Threading opt_cond_thread
Constant Folding opt_constant_folding
Constant Propagation opt_constant_propagation
Dead Code Elimination opt_dead_code
Dead Store Elimination opt_dead_store
Dead Expression Elimination opt_dead_expr
Common Subexpression Elimination opt_common_subexpr
Loop-invariant Code Motion opt_loop_invariant
Memory Allocation opt_memory_alloc
max_optimizers
max_optimizers
An object of class list
of length 9.
Performs one common subexpression elimination pass. Carefully examine the results after running this function!
opt_common_subexpr(texts, n_values = 2, in_fun_call = FALSE)
opt_common_subexpr(texts, n_values = 2, in_fun_call = FALSE)
texts |
A list of character vectors with the code to optimize. |
n_values |
A numeric indicating the minimum number of values to consider a subexpression. |
in_fun_call |
A logical indicating whether it should propagate in function calls. Note: this could change the semantics of the program. |
code <- paste( "a <- b * c + g", "d = b * c * e", sep = "\n" ) cat(opt_common_subexpr(list(code))$codes[[1]]) heron_formula <- paste( "area <- (a/2 + b/2 +c/2) * (a/2 + b/2 + c/2 - a) * (a/2 + b/2 + c/2 - b) *", " (a/2 + b/2 + c/2 - c)", "area <- sqrt(area)", sep = '\n' ) cat(opt_common_subexpr(list(heron_formula))$codes[[1]])
code <- paste( "a <- b * c + g", "d = b * c * e", sep = "\n" ) cat(opt_common_subexpr(list(code))$codes[[1]]) heron_formula <- paste( "area <- (a/2 + b/2 +c/2) * (a/2 + b/2 + c/2 - a) * (a/2 + b/2 + c/2 - b) *", " (a/2 + b/2 + c/2 - c)", "area <- sqrt(area)", sep = '\n' ) cat(opt_common_subexpr(list(heron_formula))$codes[[1]])
Performs one conditional threading pass. Carefully examine the results after running this function!
opt_cond_thread(code)
opt_cond_thread(code)
code |
A list of character vectors with the code to optimize. |
code <- paste( "num <- sample(100, 1)", "even_sum <- 0", "odd_sum_a <- 0", "odd_sum_b <- 0", "if (num %% 2 == 1) {", " odd_sum_a <- odd_sum_a + num", "}", "if (num %% 2 == 1) {", " odd_num_b <- odd_num_b + num", "}", "if (!(num %% 2 == 1)) {", " even_sum <- even_sum + num", "}", sep = "\n" ) cat(opt_cond_thread(list(code))$codes[[1]])
code <- paste( "num <- sample(100, 1)", "even_sum <- 0", "odd_sum_a <- 0", "odd_sum_b <- 0", "if (num %% 2 == 1) {", " odd_sum_a <- odd_sum_a + num", "}", "if (num %% 2 == 1) {", " odd_num_b <- odd_num_b + num", "}", "if (!(num %% 2 == 1)) {", " even_sum <- even_sum + num", "}", sep = "\n" ) cat(opt_cond_thread(list(code))$codes[[1]])
Performs one constant folding pass. Carefully examine the results after running this function!
opt_constant_folding(texts, fold_floats = FALSE, in_fun_call = FALSE)
opt_constant_folding(texts, fold_floats = FALSE, in_fun_call = FALSE)
texts |
A list of character vectors with the code to optimize. |
fold_floats |
A logical indicating if floating-point results should be folded (will reduce precision). |
in_fun_call |
A logical indicating whether it should propagate in function calls. Note: this could change the semantics of the program. |
code <- paste( "i <- 320 * 200 * 32", "x <- i * 20 + 100", sep = "\n" ) cat(opt_constant_folding(list(code))$codes[[1]])
code <- paste( "i <- 320 * 200 * 32", "x <- i * 20 + 100", sep = "\n" ) cat(opt_constant_folding(list(code))$codes[[1]])
Performs one constant propagation pass. Carefully examine the results after running this function!
opt_constant_propagation(texts, in_fun_call = FALSE)
opt_constant_propagation(texts, in_fun_call = FALSE)
texts |
A list of character vectors with the code to optimize. |
in_fun_call |
A logical indicating whether it should propagate in function calls. Note: this could change the semantics of the program. |
code <- paste( "i <- 170", "x <- -170", "y <- x + 124", "z <- i - 124", sep = "\n" ) cat(opt_constant_propagation(list(code))$codes[[1]]) hemisphere_vol <- paste( "pi <- 3.141593 ", "radius <- 25 ", "hemis_vol <- 2/3 * pi * radius ^ 3 ", sep = "\n" ) cat(opt_constant_propagation(list(hemisphere_vol))$codes[[1]])
code <- paste( "i <- 170", "x <- -170", "y <- x + 124", "z <- i - 124", sep = "\n" ) cat(opt_constant_propagation(list(code))$codes[[1]]) hemisphere_vol <- paste( "pi <- 3.141593 ", "radius <- 25 ", "hemis_vol <- 2/3 * pi * radius ^ 3 ", sep = "\n" ) cat(opt_constant_propagation(list(hemisphere_vol))$codes[[1]])
Performs one dead code elimination pass. Carefully examine the results after running this function!
opt_dead_code(texts)
opt_dead_code(texts)
texts |
A list of character vectors with the code to optimize. |
code <- paste( "while (TRUE) {", " break", " dead_code()", "}", sep = "\n" ) cat(opt_dead_code(list(code))$codes[[1]])
code <- paste( "while (TRUE) {", " break", " dead_code()", "}", sep = "\n" ) cat(opt_dead_code(list(code))$codes[[1]])
Performs one dead expression elimination pass. Carefully examine the results after running this function!
opt_dead_expr(texts)
opt_dead_expr(texts)
texts |
A list of character vectors with the code to optimize. |
code <- paste( "foo <- function(x) {", " x ^ 3", " return(x ^ 3)", "}", sep = "\n" ) cat(opt_dead_expr(list(code))$codes[[1]])
code <- paste( "foo <- function(x) {", " x ^ 3", " return(x ^ 3)", "}", sep = "\n" ) cat(opt_dead_expr(list(code))$codes[[1]])
Performs one dead store elimination pass. Carefully examine the results after running this function!
opt_dead_store(texts)
opt_dead_store(texts)
texts |
A list of character vectors with the code to optimize. |
code <- paste( "foo <- function() {", " x <- 128 ^ 2", " return(TRUE)", "}", sep = "\n" ) cat(opt_dead_store(list(code))$codes[[1]]) code <- paste( "sinpi <- function() {", " pi <- 3.1415", " e <- 2.718", " phi <- 1.618", " sin(pi)", "}", sep = "\n" ) cat(opt_dead_store(list(code))$codes[[1]])
code <- paste( "foo <- function() {", " x <- 128 ^ 2", " return(TRUE)", "}", sep = "\n" ) cat(opt_dead_store(list(code))$codes[[1]]) code <- paste( "sinpi <- function() {", " pi <- 3.1415", " e <- 2.718", " phi <- 1.618", " sin(pi)", "}", sep = "\n" ) cat(opt_dead_store(list(code))$codes[[1]])
Performs one loop-invariant code motion pass. Carefully examine the results after running this function!
opt_loop_invariant(texts)
opt_loop_invariant(texts)
texts |
A list of character vectors with the code to optimize. |
code <- paste( "i <- 0", "while (i < n) {", " x <- y + z", " a[i] <- 6 * i + x * x", " i <- i + 1", "}", sep = "\n" ) cat(opt_loop_invariant(list(code))$codes[[1]])
code <- paste( "i <- 0", "while (i < n) {", " x <- y + z", " a[i] <- 6 * i + x * x", " i <- i + 1", "}", sep = "\n" ) cat(opt_loop_invariant(list(code))$codes[[1]])
Performs one memory allocation pass. Carefully examine the results after running this function!
opt_memory_alloc(code)
opt_memory_alloc(code)
code |
A list of character vectors with the code to optimize. |
code <- paste( "v <- NULL", "for (i in 1:5) {", " v[i] <- i^2", "}", sep = "\n" ) cat(opt_memory_alloc(list(code))$codes[[1]])
code <- paste( "v <- NULL", "for (i in 1:5) {", " v[i] <- i^2", "}", sep = "\n" ) cat(opt_memory_alloc(list(code))$codes[[1]])
Performs the desired optimization strategies in the files specified. Carefully examine the results after running this function! If several files interact between them (functions from one file use functions from the other), then optimizing all of them together gives more information to rco.
optimize_files( files, optimizers = all_optimizers, overwrite = FALSE, iterations = Inf )
optimize_files( files, optimizers = all_optimizers, overwrite = FALSE, iterations = Inf )
files |
A character vector with paths to files to optimize. |
optimizers |
A named list of optimizer functions. |
overwrite |
A logical indicating if files should be overwritten, or saved into new files with "optimized_" prefix. |
iterations |
Numeric indicating the number of times optimizers should pass. If there was no change after current pass, it will stop. |
Performs the desired optimization strategies in all the '.R' in a directory. Carefully examine the results after running this function! If several files interact between them (functions from one file use functions from the other), then optimizing all of them together gives more information to rco.
optimize_folder( folder, optimizers = all_optimizers, overwrite = FALSE, iterations = Inf, pattern = "\\.R$", recursive = TRUE )
optimize_folder( folder, optimizers = all_optimizers, overwrite = FALSE, iterations = Inf, pattern = "\\.R$", recursive = TRUE )
folder |
Path to a directory with files to optimize. |
optimizers |
A named list of optimizer functions. |
overwrite |
A logical indicating if files should be overwritten, or saved into new files with "optimized_" prefix. |
iterations |
Numeric indicating the number of times optimizers should pass. If there was no change after current pass, it will stop. |
pattern |
An optional regular expression. Only file names which match the regular expression will be optimized. |
recursive |
A logical value indicating whether or not files in subdirectories of 'folder' should be optimized as well. |
Performs the desired optimization strategies in the text. Carefully examine the results after running this function!
optimize_text(text, optimizers = all_optimizers, iterations = Inf)
optimize_text(text, optimizers = all_optimizers, iterations = Inf)
text |
A character vector with the code to optimize. |
optimizers |
A named list of optimizer functions. |
iterations |
Numeric indicating the number of times optimizers should pass. If there was no change after current pass, it will stop. |
Starts the selected rco Graphical User Interface (GUI).
rco_gui(option)
rco_gui(option)
option |
A character indicating which GUI to open. One from:
|
## Start the GUI ## Not run: rco_gui("code_optimizer") ## End(Not run)
## Start the GUI ## Not run: rco_gui("code_optimizer") ## End(Not run)