Skip to content

fastmean: hoist INTEGER_RO()/REAL_RO() out of the element loops - #7914

Open
m-muecke wants to merge 3 commits into
Rdatatable:masterfrom
m-muecke:fastmean-ptr
Open

m-muecke wants to merge 3 commits into
Rdatatable:masterfrom
m-muecke:fastmean-ptr

Conversation

@m-muecke

@m-muecke m-muecke commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

base::mean() is currently quite a bit faster, REAL_ELT()/INTEGER_ELT() would also work and is used by base R, but was quite a bit slower than hoisting. For reference: https://github.com/r-devel/r-svn/blob/main/src/main/summary.c#L463

@codecov

codecov Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Codecov Report

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

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #7914   +/-   ##
=======================================
  Coverage   99.02%   99.02%           
=======================================
  Files          88       88           
  Lines       17378    17381    +3     
=======================================
+ Hits        17208    17211    +3     
  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.

Comment thread src/fastmean.c Outdated
Comment thread src/fastmean.c Outdated
@MichaelChirico

Copy link
Copy Markdown
Member

Makes sense, are you able to share any benchmark demonstrating the impact?

m-muecke and others added 2 commits October 9, 2026 23:00
Co-authored-by: Michael Chirico <michaelchirico4@gmail.com>
Co-authored-by: Michael Chirico <michaelchirico4@gmail.com>
@m-muecke

m-muecke commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor Author

Apple M2 Pro (arm64), macOS 27.0.1, R 4.6.1, Homebrew clang 23.1.3:

library(data.table)
setDTthreads(1L)
set.seed(1)
N = 1e7
DT = data.table(g = sample(10L, N, TRUE), d = rnorm(N), i = sample(1e6L, N, TRUE))
options(datatable.optimize = 1L)
bench::mark(DT[, mean(d), by = g], DT[, mean(i), by = g], min_iterations = 10, check = FALSE)
setkey(DT, g)
bench::mark(DT[, mean(d), by = g], DT[, mean(i), by = g], min_iterations = 10, check = FALSE)
1e7 rows, 10 groups, optimize=1L master PR
DT[, mean(d), by=g], double, keyed by g 54.1ms 30.0ms 1.81x
DT[, mean(d), by=g], double, unkeyed 103.1ms 81.2ms 1.27x
DT[, mean(i), by=g], integer, keyed by g 43.4ms 20.3ms 2.14x
DT[, mean(i), by=g], integer, unkeyed 87.9ms 64.6ms 1.36x

@MichaelChirico

Copy link
Copy Markdown
Member

Great! Sorry, one more thing to add is base::mean() since that's emphasized in the PR description

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