From 0c1445ea1d19853cb2f7264bf37ae5cd028f879a Mon Sep 17 00:00:00 2001 From: shikokuchuo <53399081+shikokuchuo@users.noreply.github.com> Date: Tue, 15 Jul 2025 20:38:52 +0100 Subject: [PATCH 1/2] Optimize one double coercion --- R/watch.R | 1 - src/watcher.c | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/R/watch.R b/R/watch.R index d1dfddf..2b328e3 100644 --- a/R/watch.R +++ b/R/watch.R @@ -67,7 +67,6 @@ Watcher <- R6Class( if (!is.null(callback) && !is.function(callback)) { callback <- rlang::as_function(callback) } - latency <- as.double(latency) private$watch <- .Call(watcher_create, private$path, callback, latency) } invisible(self) diff --git a/src/watcher.c b/src/watcher.c index a8e4c49..1d48270 100644 --- a/src/watcher.c +++ b/src/watcher.c @@ -109,7 +109,7 @@ static void session_finalizer(SEXP xptr) { SEXP watcher_create(SEXP path, SEXP callback, SEXP latency) { const char *watch_path = Rf_translateChar(STRING_ELT(path, 0)); - const double lat = REAL(latency)[0]; + const double lat = Rf_asReal(latency); FSW_HANDLE handle = fsw_init_session(system_default_monitor_type); if (handle == NULL) From 4bf4264efd7784d3c8b13a18d010f8f94eed8eb3 Mon Sep 17 00:00:00 2001 From: shikokuchuo <53399081+shikokuchuo@users.noreply.github.com> Date: Tue, 15 Jul 2025 20:50:37 +0100 Subject: [PATCH 2/2] Simplify unreachable error paths --- src/watcher.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/watcher.c b/src/watcher.c index 1d48270..1ae5ae9 100644 --- a/src/watcher.c +++ b/src/watcher.c @@ -115,13 +115,14 @@ SEXP watcher_create(SEXP path, SEXP callback, SEXP latency) { if (handle == NULL) watcher_error(handle, "Watcher failed to allocate memory."); - if (fsw_add_path(handle, watch_path) != FSW_OK) - watcher_error(handle, "Watcher path invalid."); + /* no need to test return value of fsw_add_path() as it only errors if the + * path is a null pointer, and Rf_translateChar() cannot return one + */ + fsw_add_path(handle, watch_path); if (XLENGTH(path) > 1) { for (R_xlen_t i = 1; i < XLENGTH(path); i++) { - if (fsw_add_path(handle, Rf_translateChar(STRING_ELT(path, i))) != FSW_OK) - watcher_error(handle, "Watcher path invalid."); + fsw_add_path(handle, Rf_translateChar(STRING_ELT(path, i))); } }