Repository navigation
Cache common subexpressions in IndexMap - #2802
Merged
Merged
Conversation
Member
|
I'm going to say no to this one. It seems reasonable that common subexpressions could be supported only in |
Member
Author
|
Direct mode and non-direct mode shouldn't produce different results like this. For the user, |
odow
reviewed
Nov 2, 2026
odow
left a comment
Member
There was a problem hiding this comment.
This needs tests, and a benchmark.
odow
reviewed
Nov 2, 2026
blegat
marked this pull request as ready for review
November 3, 2026 09:09
Member
Author
|
Benchmark import MathOptInterface as MOI
using BenchmarkTools
# f_{k+1} = sin(f_k) * f_k + f_k with f_0 = sin(x): the tree has O(3^n) nodes
# but the DAG has O(n) nodes.
function build(n)
x = MOI.VariableIndex(1)
f = MOI.ScalarNonlinearFunction(:sin, Any[x])
for _ in 1:n
sin_f = MOI.ScalarNonlinearFunction(:sin, Any[f])
prod = MOI.ScalarNonlinearFunction(:*, Any[sin_f, f])
f = MOI.ScalarNonlinearFunction(:+, Any[prod, f])
end
return f
end
# A non-identity map so that `map_indices` does not return `f` directly.
function swap_map()
map = MOI.Utilities.IndexMap()
map[MOI.VariableIndex(1)] = MOI.VariableIndex(2)
return map
end
function bench(n)
f = build(n)
map = swap_map()
println("n = $n, no cache:")
@btime MOI.Utilities.map_indices(Base.Fix1(getindex, $map), $f)
println("n = $n, cache:")
# `setup` with `evals = 1` gives each run an empty cache.
@btime MOI.Utilities.map_indices(m, $f) setup = (m = swap_map()) evals = 1
return
end
|
blegat
force-pushed
the
bl/map_idx_nl
branch
2 times, most recently
from
November 6, 2026 10:04
bbea2d7 to
6967f28
Compare
* Document that the `nl_cache` field is experimental and must only be accessed through `map_indices`. * Use an `IdDict` since common subexpressions are identified by object identity. * Skip the cache when the variable map is the identity (rebased on #3118). * Also use the cache for `VectorNonlinearFunction`. * Keep the `IndexMap(var_map, con_map)` constructor. * Add tests. Benchmark of `map_indices` on `f_{k+1} = sin(f_k) * f_k + f_k` with `f_0 = sin(x)`, whose tree has O(3^n) nodes but whose DAG has O(n) nodes, comparing `map_indices(Base.Fix1(getindex, map), f)` (no cache) with `map_indices(map, f)` (cache): | n | no cache (μs) | cache (μs) | no cache (bytes) | cache (bytes) | |----|---------------|------------|------------------|---------------| | 1 | 0.136 | 0.322 | 896 | 1120 | | 4 | 4.1 | 1.15 | 25008 | 3728 | | 8 | 349 | 2.88 | 1994928 | 7216 | | 12 | 35900 | 5.97 | 161559936 | 12736 |
Replace the Latin abbreviation rejected by Vale. Build the unary-plus expectation from an Expr so the NL reader test follows Julia's display syntax across versions. Validation: test/FileFormats/NL/test_read.jl passes on Julia 1.13.3 and installed nightly 1.15.0-DEV.244; coverage markers confirm the new lines execute. Vale reports no errors on the changed files. Repository-wide JuliaFormatter 2.14.7 leaves no unstaged diff.
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.
Extracted from #2788