Avoid recompiling the term walk for every table type - #355
Open
matthieugomez wants to merge 1 commit into
Open
Conversation
- @nospecialize the data argument of modelcols, schema, concrete_term, checkcol, missing_omit, ModelFrame, modelmatrix, response - Replace comprehensions, generators and broadcasts over the data with loops so no closure captures the table type
matthieugomez
force-pushed
the
nospecialize-data
branch
from
September 2, 2026 16:36
f150282 to
2c8170f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Calling
modelmatrixorModelFrameon a table with a different set of columns (or column types) than before costs ~0.1-0.2s of compilation, even if the formula has already been used.Two reasons:
columntableturns the data into aNamedTuple, whose type encodes the column names and types. This PR adds@nospecializeto the data argument of methods using the data.@nospecializeis not enough on its own, because the term walk uses comprehensions and broadcasts like[modelcols(tt, d) for tt in t.terms]ormodelcols.(ts, Ref(d)). The closure capturesd, so its type includes theNamedTupletype of the table, and everything downstream is re-inferred anyway. This PR replaces them by loops (_modelcols_each).Nothing changes in the API or in the returned values. Callers do lose return-type inference through
modelcols/modelmatrix; I checked that GLM, MixedModels, StandardizedPredictors, RegressionFormulae, Econometrics and FixedEffectModels don't rely on it in their tests.Timings (first call on a new table type, formula
y ~ 1 + a + b + c + e + a&balready used once, Julia 1.12):modelmatrix(f, d), one extra unused columnmodelmatrix(f, d),log(a)in the formula, one extra columnModelFrame(f, d)+modelmatrix(mf), one extra columnModelFrame(f, d)+modelmatrix(mf), two extra columnsOrthogonal to #354, which deals with the per-formula cost.