Skip to content

fmelt: create the string var once per measure group instead of once per row - #7917

Open
m-muecke wants to merge 10 commits into
masterfrom
fmelt-mkchar
Open

m-muecke wants to merge 10 commits into
masterfrom
fmelt-mkchar

Conversation

@m-muecke

Copy link
Copy Markdown
Contributor

There is more potential in this file, but that would've needed some refactoring and hence larger diffs.

ibrary(data.table)
setDTthreads(1L)
k = 10
for (n in c(1e2, 1e3, 1e4, 1e5, 1e6)) {
 set.seed(1)
 DT = data.table(id = seq_len(n))
 for (i in seq_len(k)) {
   set(DT, j = paste0("a_", i), value = runif(n))
   set(DT, j = paste0("b_", i), value = runif(n))
 }
 avars = paste0("a_", seq_len(k))
 bvars = paste0("b_", seq_len(k))
 print(bench::mark(
   list_varchar = melt(DT, id.vars = "id", measure.vars = list(avars, bvars), variable.factor = FALSE),
   measure_int = melt(DT, id.vars = "id", measure.vars = measure(value.name, num = as.integer, sep = "_")),
   measure_dbl = melt(DT, id.vars = "id", measure.vars = measure(value.name, num = as.numeric, sep = "_")),
   measure_chr = melt(DT, id.vars = "id", measure.vars = measure(value.name, num, sep = "_")),
   min_iterations = 30,
   min_time = 1.5,
   check = FALSE
 ))
}
rows measure.vars = list(...), variable.factor = FALSE measure(num = as.integer) measure(num = as.numeric) measure(num) (character)
1e2 50µs → 41µs (1.22x) 1.4ms → 1.4ms (1.00x) 1.4ms → 1.4ms (1.01x) 1.4ms → 1.4ms (1.00x)
1e3 167µs → 77µs (2.16x) 1.4ms → 1.4ms (1.01x) 1.5ms → 1.4ms (1.03x) 1.4ms → 1.4ms (1.02x)
1e4 1.3ms → 391µs (3.30x) 1.8ms → 1.5ms (1.22x) 1.7ms → 1.5ms (1.17x) 1.9ms → 1.8ms (1.09x)
1e5 12.6ms → 3.6ms (3.52x) 5.2ms → 1.9ms (2.75x) 4.5ms → 2.0ms (2.30x) 6.4ms → 5.0ms (1.28x)
1e6 124.4ms → 35.5ms (3.50x) 38.7ms → 6.3ms (6.16x) 31.9ms → 6.8ms (4.70x) 51.1ms → 37.3ms (1.37x)

@m-muecke
m-muecke requested a review from tdhock as a code owner October 11, 2026 15:43
@codecov

codecov Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.02%. Comparing base (1d87418) to head (40b5db9).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #7917   +/-   ##
=======================================
  Coverage   99.02%   99.02%           
=======================================
  Files          88       88           
  Lines       17381    17387    +6     
=======================================
+ Hits        17211    17217    +6     
  Misses        170      170           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tdhock

tdhock commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

this looks interesting thanks!!
My intuition is that the speedups should be minimal in realistic (large) data though?
Can you please try converting your benchmark code to an atime_versions test case prototype, to show speed differences with current master?
Docs are here https://github.com/Rdatatable/data.table/wiki/Performance-testing and https://rjournal.github.io/articles/RJ-2026-013/
but basically you convert for loop over n in your code to N argument in atime_versions.

@m-muecke
m-muecke requested a review from Anirban166 as a code owner October 13, 2026 19:05
@m-muecke

m-muecke commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor Author
atime_melt_7917_variable_table atime_melt_7917_variable_factor_FALSE

CI seems to be failing, but this was the local run, there seems to be cedta() issue with atime, but believe got a work around working.

@tdhock

tdhock commented Oct 14, 2026

Copy link
Copy Markdown
Member

thanks. those results look encouraging.
it is normal that atime CI is failing, until we merge #7901.

Comment thread .ci/atime/tests.R Outdated
@m-muecke
m-muecke requested a review from tdhock October 14, 2026 20:51
Comment thread .ci/atime/tests.R Outdated
Co-authored-by: Toby Dylan Hocking <tdhock5@gmail.com>
@tdhock

tdhock commented Oct 15, 2026

Copy link
Copy Markdown
Member

On my machine with this code I get ~2x speedups for one test, but smaller speedups for the other

tinfo <- atime::atime_pkg_test_info("~/R/data.table")
test.cases <- grep("7917", names(tinfo$test.call), value=TRUE)
atime.results <- list()
for(Test in test.cases){
  tcall <- tinfo$test.call[[Test]]
  atime.results[[Test]] <- eval(tcall)
}
library(ggplot2)
Test <- test.cases[2]
plot(predict(atime::references_best(atime.results[[Test]])))+
  ggtitle(Test)+
  theme(text=element_text(size=30))
image image

overall this looks good to me, can you please add a NEWS item?

@tdhock tdhock left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add these comments so we remember to change these commits after merging this PR?

Comment thread .ci/atime/tests.R Outdated
Comment thread .ci/atime/tests.R Outdated
m-muecke and others added 2 commits October 15, 2026 17:24
Co-authored-by: Toby Dylan Hocking <tdhock5@gmail.com>
Co-authored-by: Toby Dylan Hocking <tdhock5@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants