Skip to content

Commit 41f4b66

Browse files
authored
C-level optimizations (#29)
* Optimize one double coercion * Simplify unreachable error paths
1 parent 8930555 commit 41f4b66

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

R/watch.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ Watcher <- R6Class(
6767
if (!is.null(callback) && !is.function(callback)) {
6868
callback <- rlang::as_function(callback)
6969
}
70-
latency <- as.double(latency)
7170
private$watch <- .Call(watcher_create, private$path, callback, latency)
7271
}
7372
invisible(self)

src/watcher.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,19 +109,20 @@ static void session_finalizer(SEXP xptr) {
109109
SEXP watcher_create(SEXP path, SEXP callback, SEXP latency) {
110110

111111
const char *watch_path = Rf_translateChar(STRING_ELT(path, 0));
112-
const double lat = REAL(latency)[0];
112+
const double lat = Rf_asReal(latency);
113113

114114
FSW_HANDLE handle = fsw_init_session(system_default_monitor_type);
115115
if (handle == NULL)
116116
watcher_error(handle, "Watcher failed to allocate memory.");
117117

118-
if (fsw_add_path(handle, watch_path) != FSW_OK)
119-
watcher_error(handle, "Watcher path invalid.");
118+
/* no need to test return value of fsw_add_path() as it only errors if the
119+
* path is a null pointer, and Rf_translateChar() cannot return one
120+
*/
121+
fsw_add_path(handle, watch_path);
120122

121123
if (XLENGTH(path) > 1) {
122124
for (R_xlen_t i = 1; i < XLENGTH(path); i++) {
123-
if (fsw_add_path(handle, Rf_translateChar(STRING_ELT(path, i))) != FSW_OK)
124-
watcher_error(handle, "Watcher path invalid.");
125+
fsw_add_path(handle, Rf_translateChar(STRING_ELT(path, i)));
125126
}
126127
}
127128

0 commit comments

Comments
 (0)