Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Loading FixedPointNumbers invalidates ~7,700 precompiled method instances (7x slower first mtkcompile)

未关闭
#331 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
42/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
julia
领域
performance

调研方向

先从 mwe.jl 和 SnoopCompile 的 invalidation 测量开始,然后检查 src/fixed.jl、src/normed.jl 和 src/FixedPointNumbers.jl 中列出的方法。根据 7,708 次 invalidation 和 mtkcompile 计时,对比更窄的签名或其他更改,同时检查 FixedPointNumbers 的行为是否仍然正确;完成的标准是在没有回归的情况下减少加载影响。

由索引模型根据 Issue 内容生成。

描述

Summary

Loading FixedPointNumbers into a session that already has ModelingToolkit loaded invalidates
7,708 precompiled method instances and makes the first mtkcompile call take 23.59 s
instead of 0.04 s
— a huge slowdown in a script that never uses a fixed-point number.

Most users meet this indirectly: ColorTypes/Colors depend on FixedPointNumbers, so any
Makie-based plotting next to ModelingToolkit pays this cost on the first model build.

MWE

# mwe.jl
using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
if get(ENV, "LOAD_FPN", "0") == "1"
    @eval using FixedPointNumbers      # <-- the only difference
end
@variables x(t)=1.0 y(t)=0.0
@named sys = System([D(x) ~ y, D(y) ~ -x], t)
t0 = time(); mtkcompile(sys); println("mtkcompile: ", round(time()-t0, digits=2), " s")
(test) pkg> st
Status `~/test/Project.toml`
  [53c48c17] FixedPointNumbers v0.9.1
  [961ee093] ModelingToolkit v11.41.0
$ julia --startup-file=no --project=. mwe.jl
mtkcompile: 0,04 s

$ LOAD_FPN=1 julia --startup-file=no --project=. mwe.jl
mtkcompile: 23.59 s

As a control, loading a package of comparable size that does not add methods to Base
generics (using LaTeXStrings) leaves the time unchanged at 0.04 s, so this is not the
general cost of loading a package.

Attribution

using SnoopCompileCore
using ModelingToolkit
invs = @snoop_invalidations using FixedPointNumbers;
using SnoopCompile
length(uinvalidated(invs))   # 7708, from 15 culprit methods

Ranked by countchildren:

children method
14312 promote_rule(::Type{Fixed{T1,f1}}, ::Type{Fixed{T2,f2}}) where {T1,T2,f1,f2} — src/fixed.jl:199
3556 length(r::AbstractUnitRange{N}) where {T<:Unsigned, N<:Normed{T}} — src/normed.jl:304
3089 ceil(::Type{Ti}, x::Normed) where Ti<:Integer — src/normed.jl:277
1668 showarg(io::IO, a::Array{T}, toplevel) where T<:FixedPoint — src/FixedPointNumbers.jl:279
403 reduce_first(::typeof(Base.add_sum), x::FixedPoint) — src/FixedPointNumbers.jl:296
109 -(x::X) where X<:FixedPoint — src/FixedPointNumbers.jl:233

None of these are type piracy — every one is dispatching on a FixedPointNumbers type. The
cost seems to come from adding methods to widely-inferred Base generics (promote_rule,
length, ceil, convert), which discards inference results in already-loaded packages
that call those generics on non-concrete argument types.

Question

Is there room to narrow any of these signatures, or is this something that has to be fixed
on the consuming side (or in Base)? I am mostly filing this for the record, since the
measured impact is large and I could not find an existing issue about it. Happy to run
further measurements if that helps.

Versions

Julia 1.12.7 (Linux, x86_64), FixedPointNumbers 0.8.6, ModelingToolkit 11.17.0,
SnoopCompile 3.2.9.

主要语言
Julia
星标
95
派生
38
平均合并
1 天 7 分钟
30 天内合并 PR
4

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

JuliaMath/FixedPointNumbers.jl 的其他 Issue

查看 JuliaMath/FixedPointNumbers.jl 的全部 Issue

相似的 Issue

更多 Julia Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。