Serialized tables are much larger than in memory: every node keeps its own copy of the column information

オープン
#1,127 コメント 4 件 リアクション 0 件 担当者 1 名 GitHub で見る

@gmbecker がすでに取り組んでいます。

2026年12月1日 から。

  • #1128 @kaipingyang による — マージされずにクローズ
  • #1129 @gmbecker による — オープン

評価

この issue はまだ評価されていません。

説明

Summary

Every node of a TableTree (sub-tables, content tables, label rows and data rows) holds its own col_info (InstantiatedColumnInfo). In memory these copies point to the same object, but serialize(), saveRDS() and transfers to other R processes (callr, future, mirai, ...) do not de-duplicate S4 objects, so the serialized table contains one full copy per node. The serialized size therefore grows with number of nodes × size of the column information. utils::object.size(), and the RStudio Environment pane, count every copy as well, which makes even moderate tables show up as hundreds of MB.

Function-valued cell formats add to this: a function is serialized once per cell that uses it.

Reproducible example (rtables 0.6.17, public data)
library(rtables)

lyt <- basic_table(show_colcounts = TRUE) |>
  split_cols_by("ARM") |>
  split_cols_by("SEX", split_fun = keep_split_levels(c("F", "M"))) |>
  add_overall_col("All") |>
  split_rows_by("STRATA1", label_pos = "visible") |>
  summarize_row_groups() |>
  split_rows_by("RACE", split_fun = drop_split_levels) |>
  analyze(c("AGE", "BMRKR1"))
tbl <- build_table(lyt, ex_adsl)
dim(tbl)
#> [1] 74  7

format(lobstr::obj_size(tbl), units = "MB")    # in memory
#> [1] "490.55 kB"
format(length(serialize(tbl, NULL)), big.mark = ",")
#> [1] "15,550,991"
format(object.size(tbl), units = "MB")
#> [1] "129.8 Mb"

# 163 nodes carry the full column information, ~96 kB serialized each
nodes_with_full_col_info <- function(x, ci) {
  n <- as.integer(identical(col_info(x), ci))
  if (is(x, "VTableTree")) {
    n <- n + nodes_with_full_col_info(x@labelrow, ci)
    if (!is.null(content_table(x))) n <- n + nodes_with_full_col_info(content_table(x), ci)
    for (k in tree_children(x)) n <- n + nodes_with_full_col_info(k, ci)
  }
  n
}
nodes_with_full_col_info(tbl, col_info(tbl))
#> [1] 163
format(length(serialize(col_info(tbl), NULL)), big.mark = ",")
#> [1] "96,310"

163 × 96 kB ≈ 15.7 MB, essentially the whole serialized table.

Impact

On real clinical tables (built with tern on a study with 38 subjects) we measured, for a 901-row × 10-column AE by SOC/PT table: 11.9 MB in memory, 542.6 MB serialized (9.5 s to serialize), and 6.3 GB reported by object.size() / the RStudio Environment pane. Anything that serializes tables pays this cost: saveRDS(), teal.reporter (which saveRDS()s each card when rendering a report), and parallel back ends.

Proposal

A pair of exported helpers that keep the column information once on the table for storage and transfer, and restore it afterwards:

  • tt_slim(tt): stores each distinct column information (compared with identical()) once on the table and gives the nodes small placeholders that refer to it, and does the same for function-valued cell formats. Tables bound with rbind() from separately built tables keep several equivalent but non-identical column informations; each is stored once.
  • tt_rehydrate(tt): puts them back; tt_rehydrate(tt_slim(tt)) is identical() to tt.

On the example above this gives 15.6 MB → 0.43 MB (37×). On the 901-row AE table: 542.6 MB → 7.55 MB, serialization 9.5 s → 0.13 s, tt_slim() 0.8 s, tt_rehydrate() 0.7 s. Over the 33 tables of one study the median reduction was 23× (range 3.7× to 94×); tables with few columns gain less, since their column information is small. For all 33 tables, the restored table was identical() to the original, and toString(), matrix_form(), as_result_df(), row_paths() and the RTF output after a serialize round trip were identical to those of a plain serialize/unserialize round trip of the original.

The same check on every table built while opening all 46 table modules of the teal app these tables come from (104 tables built through the app's own code path): all 104 restored tables were identical() to the originals, and the outputs after a serialize round trip matched a plain round trip for all of them (RTF compared for 96; the other 8 cannot be exported to RTF either way). Total serialized size 2,735 MB → 105 MB. The slowest tt_slim() took 0.8 s, the slowest tt_rehydrate() 0.2 s. (Incidentally, as_result_df() fails on 15 of these tables even before any slimming, with attempt to select less than one element in get1index or invalid 'length' argument; that is unrelated to this proposal and I can report it separately.)

A longer-term alternative would be to avoid storing the column information on every node at all (deriving it from the parent), but that touches many accessors; the helpers above are additive and do not change any existing behaviour.

An implementation with tests, documentation and a NEWS entry is in the pull request linked to this issue.

sessionInfo()
R version 4.4.3 (2025-02-28)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 22.04.5 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;  LAPACK version 3.10.0

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

time zone: UTC
tzcode source: system (glibc)

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

other attached packages:
[1] rtables_0.6.17    formatters_0.5.13

loaded via a namespace (and not attached):
 [1] compiler_4.4.3  backports_1.5.1 fastmap_1.2.0   cli_3.6.6      
 [5] tools_4.4.3     htmltools_0.5.9 stringi_1.8.9   grid_4.4.3     
 [9] checkmate_2.3.4 digest_0.6.39   lifecycle_1.0.5 rlang_1.3.0
主要言語
R
スター
262
フォーク
55
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

pharmaverse/rtables のほかの issue

pharmaverse/rtables の issue をすべて見る

似ている issue

R の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。