Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions rcp/inst/benchmarks/benchmark-cmppkg.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
message("Starting benchmark...\n")

options("rcp.cmpfun.force_bc_recomp" = TRUE)
# options("rcp.cmpfun.use_original_cmpfun" = TRUE)
library(rcp)

total_start_time <- Sys.time()
rcp:::rcp_cmppkg("compiler")
rcp:::rcp_cmppkg("base")
rcp:::rcp_cmppkg("utils")
total_end_time <- Sys.time()

total_elapsed_ms <- as.numeric(difftime(total_end_time, total_start_time, units = "secs")) * 1000
message(sprintf("Total time: %.2f ms", total_elapsed_ms))
45 changes: 31 additions & 14 deletions rcp/src/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1613,11 +1613,12 @@ static const uint8_t *prepare_notinlined_functions(void)
}

static SEXP original_cmpfun = NULL;
static SEXP used_cmpfun = NULL;

static SEXP compile_to_bc(SEXP f, SEXP options)
{
// Create the function call: compile(f)
SEXP call = PROTECT(Rf_lang3(original_cmpfun, f, options));
SEXP call = PROTECT(Rf_lang3(used_cmpfun, f, options));

// Evaluate the function call in R
SEXP result = Rf_eval(call, R_GlobalEnv);
Expand Down Expand Up @@ -2371,23 +2372,38 @@ SEXP C_rcp_cmpfun(SEXP f, SEXP options)
static void save_original_cmpfun(void)
{
// Get the compiler namespace
SEXP compiler_namespace =
Rf_eval(PROTECT(Rf_lang2(PROTECT(Rf_install("getNamespace")),
PROTECT(Rf_mkString("compiler")))),
R_GlobalEnv);
UNPROTECT(3);
PROTECT(compiler_namespace);
SEXP compiler_namespace = PROTECT(R_FindNamespace(Rf_mkString("compiler")));

// Save the original cmpfun to a global variable
original_cmpfun = Rf_findVarInFrame(compiler_namespace, Rf_install("cmpfun"));
if (original_cmpfun == R_UnboundValue)

UNPROTECT_SAFE(compiler_namespace);
}

static void choose_cmpfun(void)
{
if (is_option_true("rcp.cmpfun.use_original_cmpfun"))
{
used_cmpfun = original_cmpfun;
return;
}
// Try to find the crbcc namespace, falling back gracefully if not available
SEXP getNamespace_call = Rf_lang2(PROTECT(Rf_install("getNamespace")),
PROTECT(Rf_mkString("crbcc")));
UNPROTECT(2);
PROTECT(getNamespace_call);
int error_occurred = 0;
SEXP compiler_namespace = R_tryEvalSilent(getNamespace_call, R_GlobalEnv, &error_occurred);
UNPROTECT_SAFE(getNamespace_call);
if (error_occurred)
{
error("Could not find compiler::cmpfun function");
used_cmpfun = original_cmpfun;
return;
}

PROTECT(original_cmpfun);
R_PreserveObject(original_cmpfun);
UNPROTECT(1);
PROTECT(compiler_namespace);
used_cmpfun = Rf_findVarInFrame(compiler_namespace, Rf_install("cmpfun"));
UNPROTECT_SAFE(compiler_namespace);
R_ShowMessage("Using cmpfun from crbcc package for JIT compilation.");
}

static SEXP cmpfun_call_sexp(void)
Expand Down Expand Up @@ -3169,6 +3185,8 @@ SEXP rcp_init(void)

save_original_cmpfun();

choose_cmpfun();

DEBUG_PRINT("Allignment: LABELS=%d, JUMPS=%d, LOOPS=%d, UNLIKELY_LABELS=%d, "
"UNLIKELY_LOOPS=%d\n",
ALIGNMENT_LABELS, ALIGNMENT_JUMPS, ALIGNMENT_LOOPS,
Expand Down Expand Up @@ -3212,7 +3230,6 @@ void rcp_destr(void)
{
C_rcp_override_cmpfun(original_cmpfun);
DEBUG_PRINT("Restored original cmpfun\n");
R_ReleaseObject(original_cmpfun);
original_cmpfun = NULL;
}
}
Loading