From 0fe46cec8640bd96c6da989f4f12f2bf0446b657 Mon Sep 17 00:00:00 2001 From: Matej Kocourek Date: Thu, 7 May 2026 14:16:51 +0200 Subject: [PATCH] Compile with crbcc by default --- rcp/inst/benchmarks/benchmark-cmppkg.R | 14 ++++++++ rcp/src/compile.c | 45 ++++++++++++++++++-------- 2 files changed, 45 insertions(+), 14 deletions(-) create mode 100644 rcp/inst/benchmarks/benchmark-cmppkg.R diff --git a/rcp/inst/benchmarks/benchmark-cmppkg.R b/rcp/inst/benchmarks/benchmark-cmppkg.R new file mode 100644 index 0000000..b57fb3a --- /dev/null +++ b/rcp/inst/benchmarks/benchmark-cmppkg.R @@ -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)) diff --git a/rcp/src/compile.c b/rcp/src/compile.c index c39f31d..64f7a8b 100644 --- a/rcp/src/compile.c +++ b/rcp/src/compile.c @@ -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); @@ -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) @@ -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, @@ -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; } }