Skip to content

Commit 2311646

Browse files
committed
add fix, test and NEWS
1 parent ce97148 commit 2311646

3 files changed

Lines changed: 17 additions & 0 deletions

File tree

NEWS.md

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

3333
3. `fread("file://...")` works for file URIs with spaces, [#7550](https://github.com/Rdatatable/data.table/issues/7550). Thanks @aitap for the report and @MichaelChirico for the PR.
3434

35+
4. Grouping operations with constant `list()` expressions in `j` are now optimized to avoid per-group allocation overhead, [#712](https://github.com/Rdatatable/data.table/issues/712). Thanks @macrakis for the report and @ben-schwen for the fix.
36+
3537
## data.table [v1.18.0](https://github.com/Rdatatable/data.table/milestone/37?closed=1) 23 December 2025
3638

3739
### BREAKING CHANGE

R/data.table.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,6 +1653,14 @@ replace_dot_alias = function(e) {
16531653
if ( getOption("datatable.optimize")>=1L && (is.call(jsub) || (is.name(jsub) && jsub %chin% c(".SD", ".N"))) ) { # Ability to turn off if problems or to benchmark the benefit
16541654
# Optimization to reduce overhead of calling lapply over and over for each group
16551655
oldjsub = jsub
1656+
1657+
# Optimization: unwrap constant list() expressions to avoid per-group allocation
1658+
# e.g., list(1) -> 1, where the value is a simple atomic constant
1659+
if (jsub %iscall% "list" && length(jsub) == 2L && !is.null(jsub[[2L]]) && !is.call(jsub[[2L]]) && is_constantish(jsub[[2L]])) {
1660+
jsub = jsub[[2L]]
1661+
if (verbose) catf("Optimized j from list(constant) to bare constant\n")
1662+
}
1663+
16561664
funi = 1L # Fix for #985
16571665
# converted the lapply(.SD, ...) to a function and used below, easier to implement FR #2722 then.
16581666
.massageSD = function(jsub) {

inst/tests/tests.Rraw

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21978,3 +21978,10 @@ local({
2197821978
test(2357.1, fread(f), DT)
2197921979
test(2357.2, fread(paste0("file://", f)), DT)
2198021980
})
21981+
21982+
# dt[, j=list(var), by] is slower than dt[, j=var, by], #712
21983+
dt = data.table(x=rep(1:3, 2L), y=1L)
21984+
test(2358.1, dt[, .(1), by=x, verbose=TRUE], dt[, 1, by=x], output="lapply optimization changed j from")
21985+
dt = data.table(x=1:5, key="x")
21986+
test(2358.2, dt[dt, list(1), by=.EACHI, verbose=TRUE], dt[dt, 1, by=.EACHI], output="lapply optimization changed j from")
21987+
test(2358.3, dt[dt, list(x), by=.EACHI, verbose=TRUE], dt[dt, x, by=.EACHI], output="lapply optimization changed j from")

0 commit comments

Comments
 (0)