Repository navigation
Conversation
`_instantiate_operators` ran all of the work of the `MPOHamiltonian` constructors inside `map(collect(local_operators)) do term ... end`. Below that `map` call, type inference applies its recursion limit to every nested `map` over a vector whose element type is not concrete, such as `map(dual, S.spaces)` in `dual(::SumSpace)`. The limited results are not cached, so each function between the two `map` calls is inferred again at every call site (JuliaLang/julia#64862). A comprehension gives the same result without a `map` frame at the root. First call of `InfiniteMPOHamiltonian([P, P], (1, 2) => h, (2, 3) => h)` from that issue: | Julia | before | after | |---------------|------------------|------------------| | 1.10.13 | 10.7 s, 4.5 GiB | 9.4 s, 3.5 GiB | | 1.13.3 | 36.0 s, 16.7 GiB | 16.8 s, 7.6 GiB | | 1.14.0-alpha2 | 29.5 s, 16.4 GiB | 14.7 s, 7.9 GiB | Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
Thanks for opening the PR, this is definitely not something I was aware of. Am I reading this correctly, that the recommended pattern for non-concrete types is a comprehension instead of a map? Is that generally true, and if so, maybe would be useful to add this to the performance section of the docs, I always struggle to motivate map vs comprehensions. Much appreciated though! |
|
I wouldn't say there is any recommended pattern for this. The PR here just works around an existing deficiency in the julia compiler. Ideally, that should be fixed, but in the meantime it seems reasonable to get the inference numbers down here for a fairly low cost (the small rewrite in this PR). |
|
Great, thanks. I'll merge once CI passes, really appreciate the help |
# Conflicts: # src/operators/mpohamiltonian.jl
Codecov Report✅ All modified and coverable lines are covered by tests.
🚀 New features to boost your workflow:
|
_instantiate_operatorsran all of the work of theMPOHamiltonianconstructors inside amap(...) doblock. Below amapcall, type inference applies its recursion limit to nestedmapcalls over vectors whose element type is not concrete (for examplemap(dual, S.spaces)in BlockTensorKit), and since limited results are not cached, every function in between is inferred again at every call site. A comprehension gives the same result without themapframe at the root. The analysis is in JuliaLang/julia#64862; QuantumKitHub/BlockTensorKit.jl#90 fixes the same thing at the other end, and either one alone is enough for this case.First call of
InfiniteMPOHamiltonian([P, P], (1, 2) => h, (2, 3) => h)from that issue:mainChecklist
finite.jl,infinite.jlandmixed.jlintest/hamiltonian)docs/src/): no API change[Unreleased]indocs/src/changelog.md, if this PR is user-facing (new feature, behavior change, bug fix, deprecation, or removal)🤖 Generated with Claude Code