Serialized tables are much larger than in memory: every node keeps its own copy of the column information
@gmbecker ya está trabajando en esto.
Desde el 1/12/2026.
Evaluación
Este issue todavía no se ha evaluado.
Descripción
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 withidentical()) 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 withrbind()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))isidentical()tott.
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
- Lenguaje dominante
- R
- Estrellas
- 262
- Forks
- 55
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Sin plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de pharmaverse/rtables
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
pharmaverse/rtables#1123 · 1 comentario · 1 reacción ·
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
pharmaverse/rtables#1107 · 1 comentario ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
pharmaverse/rtables#1105 ·
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 90/100
pharmaverse/rtables#1096 ·
-
Dificultad 4/5 3-5 días Aptitud para principiantes 52/100
pharmaverse/rtables#1125 ·
Todos los issues de pharmaverse/rtables
Issues similares
-
bug
Dificultad 1/5 Menos de una hora Aptitud para principiantes 82/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
Los mantenedores suelen responder en 1 día
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 90/100
Bioconductor/BiocParallel#289 ·
-
automation
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
waldronlab/BugSigDBcuration#2224 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 65/100
merliseclyde/BAS#118 ·