Skip to content

Commit 2715663

Browse files
Removes unnecessary data.table call from as.data.table.array (#7019)
1 parent 8647d44 commit 2715663

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

.ci/atime/tests.R

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,5 +251,15 @@ test.list <- atime::atime_test_list(
251251
Before = "f339aa64c426a9cd7cf2fcb13d91fc4ed353cd31", # Parent of the first commit https://github.com/Rdatatable/data.table/commit/fcc10d73a20837d0f1ad3278ee9168473afa5ff1 in the PR https://github.com/Rdatatable/data.table/pull/6393/commits with major change to fwrite with gzip.
252252
PR = "3630413ae493a5a61b06c50e80d166924d2ef89a"), # Close-to-last merge commit in the PR.
253253

254-
tests=extra.test.list)
254+
# Test case created directly using the atime code below (not adapted from any other benchmark), based on the PR, Removes unnecessary data.table call from as.data.table.array https://github.com/Rdatatable/data.table/pull/7010
255+
"as.data.table.array improved in #7010" = atime::atime_test(
256+
setup = {
257+
dims = c(N, 1, 1)
258+
arr = array(seq_len(prod(dims)), dim=dims)
259+
},
260+
expr = data.table:::as.data.table.array(arr, na.rm=FALSE),
261+
Slow = "73d79edf8ff8c55163e90631072192301056e336", # Parent of the first commit in the PR (https://github.com/Rdatatable/data.table/commit/8397dc3c993b61a07a81c786ca68c22bc589befc)
262+
Fast = "8397dc3c993b61a07a81c786ca68c22bc589befc"), # Commit in the PR (https://github.com/Rdatatable/data.table/pull/7019/commits) that removes inefficiency
263+
264+
tests=extra.test.list)
255265
# nolint end: undesirable_operator_linter.

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
4. `as.Date()` method for `IDate` no longer coerces to `double` [#6922](https://github.com/Rdatatable/data.table/issues/6922). Thanks @MichaelChirico for the report and PR. The only effect should be on overly-strict tests that assert `Date` objects have `double` storage, which is not in general true, especially from R 4.5.0.
1616

17+
5. `as.data.table()` is slightly more efficient at converting arrays to data.tables, [#7019](https://github.com/Rdatatable/data.table/pull/7019). Thanks @eliocamp.
18+
1719
### BUG FIXES
1820

1921
1. Custom binary operators from the `lubridate` package now work with objects of class `IDate` as with a `Date` subclass, [#6839](https://github.com/Rdatatable/data.table/issues/6839). Thanks @emallickhossain for the report and @aitap for the fix.

R/as.data.table.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ as.data.table.array = function(x, keep.rownames=FALSE, key=NULL, sorted=TRUE, va
9696
dnx = dimnames(x)
9797
# NULL dimnames will create integer keys, not character as in table method
9898
val = if (is.null(dnx)) {
99-
lapply(dx, seq.int)
99+
lapply(dx, seq_len)
100100
} else if (any(nulldnx <- vapply_1b(dnx, is.null))) {
101-
dnx[nulldnx] = lapply(dx[nulldnx], seq.int) #3636
101+
dnx[nulldnx] = lapply(dx[nulldnx], seq_len) #3636
102102
dnx
103103
} else dnx
104104
val = rev(val)
@@ -107,7 +107,8 @@ as.data.table.array = function(x, keep.rownames=FALSE, key=NULL, sorted=TRUE, va
107107
if (value.name %chin% names(val))
108108
stopf("Argument 'value.name' should not overlap with column names in result: %s", brackify(rev(names(val))))
109109
N = NULL
110-
ans = data.table(do.call(CJ, c(val, sorted=FALSE)), N=as.vector(x))
110+
ans = do.call(CJ, c(val, sorted=FALSE))
111+
set(ans, j="N", value=as.vector(x))
111112
if (isTRUE(na.rm))
112113
ans = ans[!is.na(N)]
113114
setnames(ans, "N", value.name)

0 commit comments

Comments
 (0)