`[.data.table` is very slow with a single column

Open
#5,650 15 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
r
Domain
data, performance

Research direction

Start by reproducing the reported benchmark with data.table 1.14.2, comparing x[index, a] with x[["a"]][index] and enabling verbose output where possible. Investigate the single-column subset path responsible for the slowdown; done means the data.table expression no longer shows the reported performance gap and the benchmark remains correct.

Written by the indexing model from the issue text.

Description

performance

# Minimal reproducible example; please be sure to set verbose=TRUE where possible!

library(bench)
x <- data.frame(a = runif(10000), b= as.character(runif(10000)))
index <- runif(10000) <= 0.5
mark(x[index, "a"], x[["a"]][index])
# A tibble: 2 × 13
#    expression      min median `itr/sec` mem_alloc `gc/sec` n_itr  n_gc total_time
#    <bch:expr>   <bch:> <bch:>     <dbl> <bch:byt>    <dbl> <int> <dbl>   <bch:tm>
# 1 "x[index, \… 78.1µs 80.6µs    11587.    98.4KB     21.2  5469    10      472ms
# 2 "x[[\"a\"]]…   71µs 73.2µs    13240.    98.4KB     23.5  6207    11      469ms
# More or less the same !
library(data.table)
x <- as.data.table(x)
mark(x[index, a], x[["a"]][index])
# A tibble: 2 × 13
#   expression     min  median `itr/sec` mem_alloc `gc/sec` n_itr  n_gc total_time
#   <bch:expr> <bch:t> <bch:t>     <dbl> <bch:byt>    <dbl> <int> <dbl>   <bch:tm>
# 1 "x[index,… 360.4µs 377.1µs     2403.   219.3KB     8.28  1161     4      483ms
# 2 "x[[\"a\"…  71.4µs  73.9µs    12920.    98.4KB    23.9   5949    11      460ms
# Five times slower !!!

# Output of sessionInfo()

R version 4.1.2 (2021-11-01)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.2 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
 [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=es_ES.utf8        LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=es_ES.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] data.table_1.14.2 bench_1.1.3      

loaded via a namespace (and not attached):
 [1] matrixStats_0.61.0 fansi_1.0.2        utf8_1.2.2         irace_3.5.1.9000  
 [5] R6_2.5.1           lifecycle_1.0.3    magrittr_2.0.2     pillar_1.9.0      
 [9] profmem_0.6.0      rlang_1.0.6        cli_3.6.0          vctrs_0.5.2       
[13] tools_4.1.2        glue_1.6.2         compiler_4.1.2     pkgconfig_2.0.3   
[17] tibble_3.2.1
Dominant language
R
Stars
3.9k
Forks
1.1k
Avg merge
14h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.