Skip to content

Commit

Permalink
shave off slight overhead from setdiff()
Browse files Browse the repository at this point in the history
  • Loading branch information
wlandau committed Jan 2, 2025
1 parent fb11ba1 commit 1deed2c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion R/class_branch.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ branch_init <- function(
store = NULL,
index = NULL
) {
deps <- setdiff(unique(c(deps_parent, deps_child)), settings$dimensions)
deps <- setdiff_chr(
x = unique.default(c(deps_parent, deps_child)),
y = settings$dimensions
)
branch_new(
name = name,
command = command,
Expand Down
4 changes: 4 additions & 0 deletions R/utils_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ set_names <- function(x, names) {
x
}

setdiff_chr <- function(x, y) {
x[!duplicated.default(x) & (match(x, y, 0L) == 0L)]
}

sort_chr <- function(x) {
old_locale <- Sys.getlocale(category = "LC_COLLATE")
on.exit(Sys.setlocale(category = "LC_COLLATE", locale = old_locale))
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-utils_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ tar_test("supported_args()", {
)
})

tar_test("setdiff_chr()", {
expect_equal(setdiff_chr("x", character(0L)), "x")
expect_equal(setdiff_chr(character(0L), "x"), character(0L))
expect_equal(setdiff(c("d", "a", "b"), "a"), c("d", "b"))
})

tar_test("sort_chr()", {
expect_equal(sort_chr(c("a", "b")), c("a", "b"))
expect_equal(sort_chr(character(0L)), character(0L))
Expand Down

0 comments on commit 1deed2c

Please sign in to comment.