Optimise dcast.data.table to reduce peak memory usage (currently: reaches 4x table size)

Đang mở
#1,069 14 bình luận 0 reaction 1 người được giao Xem trên GitHub

@arunsrinivasan đang làm issue này rồi.

Từ ngày 6/3/2015.

Đánh giá

Issue này chưa được đánh giá.

Mô tả

enhancement reshape

I am using data.table's melt and dcast function to melt a wide table, do some transformation and cast it back to a tidy format.

The Issue

I tested with a small file of ~1GB and was very surprised when R ran out of memory on my i7, 16GB machine. I investigated further and realise that dcast.data.table hit a peak memory of ~4x the long-format file's size.

Running my sample code below, the memory requirements for the process are as follow:

  • Original table = 67mb (screenshot 1)
  • Melted long table = 184mb (incremental peak memory: 269-67 = 202mb)
  • After transformation = 214mb (screenshot 2)
  • Final table after dcast = 123mb (incremental peak memory: 935-214 = 721mb) (screenshot 2)

I added reshape2's dcast for comparison:

  • Incremental peak memory: 834-214 = 620mb

My Request

Can memory usage for dcast.data.table be optimised further?

dcast.data.table is certainly faster than its counterpart but it appears there is some memory overhead. The 1GB file I tested originally is only ~30% of the full dataset, so a dcast.data.table that is more frugal is very much appreciated =)

150306 - dcast mem issue a
150306 - dcast mem issue

Sample Data and Code

library( data.table )
library( reshape2 )
library( stringr )

gc( reset = T )
set.seed(1)
nrow <- 5e5
ColNames <- c( "IDCol1", "IDCol2", "IDCol3", 
               paste0( "A_dd", 1:8 ), paste0( "B_dd", 1:8 ) )

# Create DT
dt <- as.data.table( setNames( c(
  replicate( 2, sample( state.name, nrow, replace = T ), simplify = F ), 
  replicate( 1, 1:nrow, simplify = F ), 
  replicate( 16, round( runif( nrow, 1, 30 ), 2), simplify = F ) ), 
    ColNames ) )
tables()
gc( reset = T )

# Melt dt
L_dt <- melt( dt, id = c( "IDCol1", "IDCol2", "IDCol3" ) )
rm( dt )
gc()

tables()
gc( reset = T )

# Transform dt
L_dt[ , NewCol1 := as.integer( str_extract( L_dt$variable, "[0-9]$" ) ) ]
L_dt[ , variable := str_replace( L_dt$variable, "[0-9]$", "" ) ]
tables()
gc( reset = T )

# Using data.table to re-cast L_dt to W_dt
W_dt1 <- dcast.data.table( L_dt, IDCol1+IDCol2+IDCol3+NewCol1 ~ variable, 
                          value.var = "value" )
gc()
tables()
rm( W_dt1 )
gc( reset = T )


# Using Reshape2 to re-cast L_dt to W_dt
W_dt2 <- dcast( L_dt, IDCol1+IDCol2+IDCol3+NewCol1 ~ variable, 
                          value.var = "value" )
gc()
Ngôn ngữ chính
R
Star
3.9k
Fork
1.1k
Merge trung bình
14 giờ 4 phút
Pull request đã merge (30 ngày)
4

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của Rdatatable/data.table

Tất cả issue của Rdatatable/data.table

Issue tương tự

Thêm issue về R

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.