Skip to content

Commit 53a91db

Browse files
authored
Merge pull request #77 from m-muecke/is-true
perf: replace `identical(x, TRUE)` with faster `isTRUE(x)`/`isFALSE(x)`
2 parents 17ca8f3 + 51e9dba commit 53a91db

File tree

4 files changed

+24
-24
lines changed

4 files changed

+24
-24
lines changed

R/Filterable.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ Filterable <- R6::R6Class(
2828
r <- f[["filter"]](event)
2929
}
3030

31-
if (identical(r, TRUE)){
31+
if (isTRUE(r)){
3232
# do nothing
33-
} else if (identical(r, FALSE)){
33+
} else if (isFALSE(r)){
3434
return(FALSE)
3535
} else {
3636
warning(

R/Logger.R

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ Logger <- R6::R6Class(
212212

213213
# Check if LogEvent should be created
214214
if (
215-
identical(level[[1]] > get("threshold", envir = self), TRUE) ||
216-
identical(getOption("lgr.logging_suspended"), TRUE)
215+
isTRUE(level[[1]] > get("threshold", envir = self)) ||
216+
isTRUE(getOption("lgr.logging_suspended"))
217217
){
218218
return(invisible(msg))
219219
}
@@ -305,7 +305,7 @@ Logger <- R6::R6Class(
305305
#' @description Log an Event fatal priority
306306
#' @param msg,...,caller see `$log()`
307307
fatal = function(msg, ..., caller = get_caller(-8L)){
308-
if (identical(get("threshold", envir = self) < 100L, TRUE))
308+
if (isTRUE(get("threshold", envir = self) < 100L))
309309
return(invisible())
310310

311311
get("log", envir = self)(
@@ -321,7 +321,7 @@ Logger <- R6::R6Class(
321321
#' @description Log an Event error priority
322322
#' @param msg,...,caller see `$log()`
323323
error = function(msg, ..., caller = get_caller(-8L)){
324-
if (identical(get("threshold", envir = self) < 200L, TRUE))
324+
if (isTRUE(get("threshold", envir = self) < 200L))
325325
return(invisible())
326326

327327
get("log", envir = self)(
@@ -337,7 +337,7 @@ Logger <- R6::R6Class(
337337
#' @description Log an Event warn priority
338338
#' @param msg,...,caller see `$log()`
339339
warn = function(msg, ..., caller = get_caller(-8L)){
340-
if (identical(get("threshold", envir = self) < 300L, TRUE))
340+
if (isTRUE(get("threshold", envir = self) < 300L))
341341
return(invisible())
342342

343343
get("log", envir = self)(
@@ -353,7 +353,7 @@ Logger <- R6::R6Class(
353353
#' @description Log an Event info priority
354354
#' @param msg,...,caller see `$log()`
355355
info = function(msg, ..., caller = get_caller(-8L)){
356-
if (identical(get("threshold", envir = self) < 400L, TRUE))
356+
if (isTRUE(get("threshold", envir = self) < 400L))
357357
return(invisible())
358358

359359
get("log", envir = self)(
@@ -369,7 +369,7 @@ Logger <- R6::R6Class(
369369
#' @description Log an Event debug priority
370370
#' @param msg,...,caller see `$log()`
371371
debug = function(msg, ..., caller = get_caller(-8L)){
372-
if (identical(get("threshold", envir = self) < 500L, TRUE))
372+
if (isTRUE(get("threshold", envir = self) < 500L))
373373
return(invisible())
374374

375375
get("log", envir = self)(
@@ -385,7 +385,7 @@ Logger <- R6::R6Class(
385385
#' @description Log an Event trace priority
386386
#' @param msg,...,caller see `$log()`
387387
trace = function(msg, ..., caller = get_caller(-8L)){
388-
if (identical(get("threshold", envir = self) < 600L, TRUE))
388+
if (isTRUE(get("threshold", envir = self) < 600L))
389389
return(invisible())
390390

391391
get("log", envir = self)(
@@ -781,7 +781,7 @@ LoggerGlue <- R6::R6Class(
781781
public = list(
782782

783783
fatal = function(..., caller = get_caller(-8L), .envir = parent.frame()){
784-
if (identical(get("threshold", envir = self) < 100L, TRUE))
784+
if (isTRUE(get("threshold", envir = self) < 100L))
785785
return(invisible())
786786

787787
force(.envir)
@@ -795,7 +795,7 @@ LoggerGlue <- R6::R6Class(
795795
},
796796

797797
error = function(..., caller = get_caller(-8L), .envir = parent.frame()){
798-
if (identical(get("threshold", envir = self) < 200L, TRUE))
798+
if (isTRUE(get("threshold", envir = self) < 200L))
799799
return(invisible())
800800

801801
get("log", envir = self)(
@@ -808,7 +808,7 @@ LoggerGlue <- R6::R6Class(
808808
},
809809

810810
warn = function(..., caller = get_caller(-8L), .envir = parent.frame()){
811-
if (identical(get("threshold", envir = self) < 300L, TRUE))
811+
if (isTRUE(get("threshold", envir = self) < 300L))
812812
return(invisible())
813813

814814
get("log", envir = self)(
@@ -821,7 +821,7 @@ LoggerGlue <- R6::R6Class(
821821
},
822822

823823
info = function(..., caller = get_caller(-8L), .envir = parent.frame()){
824-
if (identical(get("threshold", envir = self) < 400L, TRUE))
824+
if (isTRUE(get("threshold", envir = self) < 400L))
825825
return(invisible())
826826

827827
get("log", envir = self)(
@@ -834,7 +834,7 @@ LoggerGlue <- R6::R6Class(
834834
},
835835

836836
debug = function(..., caller = get_caller(-8L), .envir = parent.frame()){
837-
if (identical(get("threshold", envir = self) < 500L, TRUE))
837+
if (isTRUE(get("threshold", envir = self) < 500L))
838838
return(invisible())
839839

840840
force(.envir)
@@ -848,7 +848,7 @@ LoggerGlue <- R6::R6Class(
848848
},
849849

850850
trace = function(..., caller = get_caller(-8L), .envir = parent.frame()){
851-
if (identical(get("threshold", envir = self) < 600L, TRUE))
851+
if (isTRUE(get("threshold", envir = self) < 600L))
852852
return(invisible())
853853

854854
force(.envir)
@@ -913,8 +913,8 @@ LoggerGlue <- R6::R6Class(
913913

914914
# Check if LogEvent should be created
915915
if (
916-
identical(level[[1]] > get("threshold", envir = self), TRUE) ||
917-
identical(getOption("lgr.logging_suspended"), TRUE)
916+
isTRUE(level[[1]] > get("threshold", envir = self)) ||
917+
isTRUE(getOption("lgr.logging_suspended"))
918918
){
919919
return(invisible(msg))
920920
}

R/utils-sfmisc.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ assert <- function(
112112
call. = FALSE,
113113
domain = NULL
114114
){
115-
if (identical(cond, TRUE)){
115+
if (isTRUE(cond)){
116116
return(TRUE)
117-
} else if (identical(cond, FALSE)){
117+
} else if (isFALSE(cond)){
118118
if (identical(length(list(...)), 0L)){
119119
msg <- paste0("`", deparse(match.call()[[2]]), "`", " is not 'TRUE'")
120120
stop(msg, call. = call., domain = domain)
@@ -323,7 +323,7 @@ is_bool <- function(x){
323323
#' @noRd
324324
#'
325325
is_scalar_bool <- function(x){
326-
identical(x, TRUE) || identical(x, FALSE)
326+
is.logical(x) && length(x) == 1L && !is.na(x)
327327
}
328328

329329

@@ -356,14 +356,14 @@ is_scalar_integerish <- function(x){
356356

357357

358358
is_n <- function(x){
359-
is_scalar_integerish(x) && identical(x > 0, TRUE)
359+
is_scalar_integerish(x) && isTRUE(x > 0)
360360
}
361361

362362

363363

364364

365365
is_n0 <- function(x){
366-
is_scalar_integerish(x) && identical(x >= 0, TRUE)
366+
is_scalar_integerish(x) && isTRUE(x >= 0)
367367
}
368368

369369

R/utils.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ CannotInitializeAbstractClassError <- function(
231231

232232
# stricter & faster thant base R version & support for R < 3.5
233233
isFALSE <- function(x){
234-
identical(x, FALSE)
234+
is.logical(x) && length(x) == 1L && !is.na(x) && !x
235235
}
236236

237237

0 commit comments

Comments
 (0)