diff --git a/CHANGELOG.md b/CHANGELOG.md index 40a0432..430138c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 2.2.1 + +- Reduce the latency of the first results display (about 1.3s to 0.07s): the display is precompiled and no longer compiles for each type of output stream. +- Avoid invalidations from packages loaded by CSV (WeakRefStrings, FilePathsBase): fixed-effect names, coefficient names, and the display use concretely typed strings. + ## 2.2.0 - Fix regressors and the constant that were dropped as collinear because of the units of another regressor, e.g. the constant with a regressor around 1e12. Fix standard errors in the same cases. diff --git a/Project.toml b/Project.toml index 9508abe..bb84683 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "FixedEffectModels" uuid = "9d5cd8c9-2029-5cab-9928-427838db53e3" -version = "2.2.0" +version = "2.2.1" [deps] DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" diff --git a/src/FixedEffectModel.jl b/src/FixedEffectModel.jl index be24866..7a52456 100644 --- a/src/FixedEffectModel.jl +++ b/src/FixedEffectModel.jl @@ -343,20 +343,36 @@ function top(m::FixedEffectModel) end +# Render into a buffer, so that the display compiles once (in the precompile +# workload) rather than once per type of io (terminal, pipe, IOContext, ...) function Base.show(io::IO, m::FixedEffectModel) + buf = IOContext(IOBuffer(), io) + _show(buf, m) + write(io, take!(buf.io)) + return nothing +end + +function _show(io::IOContext{IOBuffer}, m::FixedEffectModel) ct = coeftable(m) #copied from show(iio,cf::Coeftable) - cols = ct.cols; rownms = ct.rownms; colnms = ct.colnms; + cols = ct.cols nc = length(cols) - nr = length(cols[1]) - if length(rownms) == 0 - rownms = [lpad("[$i]",floor(Integer, log10(nr))+3) for i in 1:nr] + nr = length(cols[1])::Int + # concretely typed names: untyped ones compile String conversions that + # packages adding string types (e.g. WeakRefStrings) invalidate + rownms = (isempty(ct.rownms) ? String[lpad("[$i]", floor(Int, log10(nr))+3) for i in 1:nr] : + String[string(x)::String for x in ct.rownms])::Vector{String} + colnms = String[string(x)::String for x in ct.colnms]::Vector{String} + mat = Matrix{Any}(undef, nr, nc + 1) + for i in 1:nr + mat[i, 1] = NoQuote(rownms[i]) + for j in 1:nc + x = cols[j][i] + mat[i, j + 1] = j == ct.pvalcol ? NoQuote(sprint(show, PValue(x))::String) : + j in ct.teststatcol ? TestStat(x) : + x isa AbstractString ? NoQuote(string(x)::String) : x + end end - mat = [j == 1 ? NoQuote(rownms[i]) : - j-1 == ct.pvalcol ? NoQuote(sprint(show, PValue(cols[j-1][i]))) : - j-1 in ct.teststatcol ? TestStat(cols[j-1][i]) : - cols[j-1][i] isa AbstractString ? NoQuote(cols[j-1][i]) : cols[j-1][i] - for i in 1:nr, j in 1:nc+1] io = IOContext(io, :compact=>true, :limit=>false) A = Base.alignment(io, mat, 1:size(mat, 1), 1:size(mat, 2), typemax(Int), typemax(Int), 3) diff --git a/src/FixedEffectModels.jl b/src/FixedEffectModels.jl index 9a7aa89..47021e2 100644 --- a/src/FixedEffectModels.jl +++ b/src/FixedEffectModels.jl @@ -47,6 +47,8 @@ Vcov # cover the categorical and interaction term types (the vector-based # machinery compiles per term type, not per formula shape) reg(df, @formula(y ~ x1 + s + x1&x2 + x1&s)) + # the REPL displays every result + show(IOBuffer(), MIME"text/plain"(), reg(df, @formula(y ~ x1 + fe(id)))) end diff --git a/src/utils/formula.jl b/src/utils/formula.jl index 4403f7b..3e81e6a 100644 --- a/src/utils/formula.jl +++ b/src/utils/formula.jl @@ -167,11 +167,13 @@ function _parse_fixedeffect(data, @nospecialize(t::InteractionTerm)) isempty(fe_terms) && return nothing # x1&x2 from (x1&x2)*id - fe_names = [fesymbol(x) for x in fe_terms] + fe_names = Symbol[fesymbol(x) for x in fe_terms] slope_names = _validate_fe_interactions(data, slope_terms) interaction = _multiply(data, slope_names) fe = FixedEffect((Tables.getcolumn(data, fe_name) for fe_name in fe_names)...; interaction = interaction) - pieces = vcat(["fe_" * string(fe_name) for fe_name in fe_names], string.(slope_terms)) + # a concretely typed vector: joining an untyped one compiles IO calls that + # packages adding IO methods (e.g. FilePathsBase) invalidate, along with fit + pieces = vcat(String["fe_" * string(fe_name) for fe_name in fe_names], String[string(s) for s in slope_names]) return fe, Symbol(join(pieces, "&")), fe_names end diff --git a/src/utils/vectorterms.jl b/src/utils/vectorterms.jl index f1b4aec..803aa1b 100644 --- a/src/utils/vectorterms.jl +++ b/src/utils/vectorterms.jl @@ -172,7 +172,12 @@ function coefnames_vector(ts::Vector{AbstractTerm}) out = String[] for t in ts StatsModels.width(t) > 0 || continue - append!(out, StatsModels.vectorize(coefnames(t))) + # push concretely typed names: appending the untyped coefnames result + # compiles String conversions that packages adding string types (e.g. + # WeakRefStrings) invalidate, along with fit + for nm in StatsModels.vectorize(coefnames(t)) + push!(out, string(nm)::String) + end end return out end