From 6bc77a2a7ea16b9a98916be9af8c5c20504f5bec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Wed, 6 Aug 2025 09:54:57 +0200 Subject: [PATCH 1/3] Cache common subexpressions in IndexMap --- src/Utilities/copy/index_map.jl | 24 +++++++++++++++++++++++- src/Utilities/functions.jl | 14 ++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/Utilities/copy/index_map.jl b/src/Utilities/copy/index_map.jl index 34a446f3b1..13ed5b1faa 100644 --- a/src/Utilities/copy/index_map.jl +++ b/src/Utilities/copy/index_map.jl @@ -20,6 +20,9 @@ struct IndexMap <: AbstractDict{MOI.Index,MOI.Index} typeof(CleverDicts.index_to_key), } con_map::DoubleDicts.IndexDoubleDict + # This field is experimental and may be removed in a future release. Do not + # access it directly; use `map_indices` instead. + nl_cache::IdDict{MOI.ScalarNonlinearFunction,MOI.ScalarNonlinearFunction} end """ @@ -30,7 +33,8 @@ The dictionary-like object returned by [`MOI.copy_to`](@ref). function IndexMap() var_map = CleverDicts.CleverDict{MOI.VariableIndex,MOI.VariableIndex}() con_map = DoubleDicts.IndexDoubleDict() - return IndexMap(var_map, con_map) + nl_cache = IdDict{MOI.ScalarNonlinearFunction,MOI.ScalarNonlinearFunction}() + return IndexMap(var_map, con_map, nl_cache) end function _identity_constraints_map( @@ -121,3 +125,21 @@ function map_indices(map::IndexMap, x::X)::X where {X<:_OnlyVariableIndices} end return map_indices(Base.Fix1(getindex, map), x) end + +# Common subexpressions, i.e., `MOI.ScalarNonlinearFunction` objects that are +# shared between functions, are mapped only once, and the mapped functions +# share the corresponding mapped subexpressions. +function map_indices(map::IndexMap, f::MOI.ScalarNonlinearFunction) + if CleverDicts.is_identity(map.var_map) + return f + end + return map_indices(Base.Fix1(getindex, map), f, map.nl_cache) +end + +function map_indices(map::IndexMap, f::MOI.VectorNonlinearFunction) + if CleverDicts.is_identity(map.var_map) + return f + end + rows = MOI.ScalarNonlinearFunction[map_indices(map, row) for row in f.rows] + return MOI.VectorNonlinearFunction(rows) +end diff --git a/src/Utilities/functions.jl b/src/Utilities/functions.jl index 0c3016bc7b..0186e03d37 100644 --- a/src/Utilities/functions.jl +++ b/src/Utilities/functions.jl @@ -367,7 +367,11 @@ end function map_indices( index_map::F, f::MOI.ScalarNonlinearFunction, + nl_cache = nothing, ) where {F<:Function} + if !isnothing(nl_cache) && haskey(nl_cache, f) + return nl_cache[f] + end root = MOI.ScalarNonlinearFunction(f.head, similar(f.args)) stack = Tuple{MOI.ScalarNonlinearFunction,Int,MOI.ScalarNonlinearFunction}[] for (i, fi) in enumerate(f.args) @@ -380,6 +384,10 @@ function map_indices( while !isempty(stack) parent, i, arg = pop!(stack) if arg isa MOI.ScalarNonlinearFunction + if !isnothing(nl_cache) && haskey(nl_cache, arg) + parent.args[i] = nl_cache[arg] + continue + end child = MOI.ScalarNonlinearFunction(arg.head, similar(arg.args)) for (j, argj) in enumerate(arg.args) if argj isa MOI.ScalarNonlinearFunction @@ -389,10 +397,16 @@ function map_indices( end end parent.args[i] = child + if !isnothing(nl_cache) + nl_cache[arg] = child + end else parent.args[i] = MOI.Utilities.map_indices(index_map, arg) end end + if !isnothing(nl_cache) + nl_cache[f] = root + end return root end From bae5ac89086e16584ba39315aeafad712af43965 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Tue, 3 Nov 2026 09:28:55 +0100 Subject: [PATCH 2/3] Address review: tests, benchmark and experimental comment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 | --- src/Utilities/copy/index_map.jl | 4 ++++ test/Utilities/test_copy.jl | 34 +++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/Utilities/copy/index_map.jl b/src/Utilities/copy/index_map.jl index 13ed5b1faa..2be34bed35 100644 --- a/src/Utilities/copy/index_map.jl +++ b/src/Utilities/copy/index_map.jl @@ -33,6 +33,10 @@ The dictionary-like object returned by [`MOI.copy_to`](@ref). function IndexMap() var_map = CleverDicts.CleverDict{MOI.VariableIndex,MOI.VariableIndex}() con_map = DoubleDicts.IndexDoubleDict() + return IndexMap(var_map, con_map) +end + +function IndexMap(var_map, con_map) nl_cache = IdDict{MOI.ScalarNonlinearFunction,MOI.ScalarNonlinearFunction}() return IndexMap(var_map, con_map, nl_cache) end diff --git a/test/Utilities/test_copy.jl b/test/Utilities/test_copy.jl index f8800e494d..525e4f4d7c 100644 --- a/test/Utilities/test_copy.jl +++ b/test/Utilities/test_copy.jl @@ -1144,6 +1144,40 @@ function test_map_indices_identity_index_map() return end +function test_map_indices_common_subexpressions() + x = MOI.VariableIndex.(1:2) + index_map = MOI.Utilities.IndexMap() + index_map[x[1]] = x[2] + index_map[x[2]] = x[1] + sin_x = MOI.ScalarNonlinearFunction(:sin, Any[x[1]]) + f = MOI.ScalarNonlinearFunction(:+, Any[sin_x, sin_x, x[2]]) + g = MOI.ScalarNonlinearFunction(:*, Any[2.0, sin_x]) + f_dest = MOI.Utilities.map_indices(index_map, f) + @test f_dest ≈ MOI.ScalarNonlinearFunction( + :+, + Any[ + MOI.ScalarNonlinearFunction(:sin, Any[x[2]]), + MOI.ScalarNonlinearFunction(:sin, Any[x[2]]), + x[1], + ], + ) + @test f_dest.args[1] === f_dest.args[2] + g_dest = MOI.Utilities.map_indices(index_map, g) + @test g_dest.args[2] === f_dest.args[1] + @test MOI.Utilities.map_indices(index_map, f) === f_dest + h = MOI.VectorNonlinearFunction([f, g]) + h_dest = MOI.Utilities.map_indices(index_map, h) + @test h_dest.rows[1] === f_dest + @test h_dest.rows[2] === g_dest + # The cache is not used if the variable map is the identity. + identity_map = MOI.Utilities.IndexMap() + identity_map[x[1]] = x[1] + identity_map[x[2]] = x[2] + @test MOI.Utilities.map_indices(identity_map, f) === f + @test MOI.Utilities.map_indices(identity_map, h) === h + return +end + end # module TestCopy.runtests() From 9e3b8819a6f54f9b6001e8ddd8a5b540a2affe91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Fri, 6 Nov 2026 14:57:58 +0100 Subject: [PATCH 3/3] Fix Vale wording and Julia nightly display assertion 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. --- src/Utilities/copy/index_map.jl | 2 +- test/FileFormats/NL/test_read.jl | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Utilities/copy/index_map.jl b/src/Utilities/copy/index_map.jl index 2be34bed35..080b73ac89 100644 --- a/src/Utilities/copy/index_map.jl +++ b/src/Utilities/copy/index_map.jl @@ -130,7 +130,7 @@ function map_indices(map::IndexMap, x::X)::X where {X<:_OnlyVariableIndices} return map_indices(Base.Fix1(getindex, map), x) end -# Common subexpressions, i.e., `MOI.ScalarNonlinearFunction` objects that are +# Common subexpressions, that is, `MOI.ScalarNonlinearFunction` objects that are # shared between functions, are mapped only once, and the mapped functions # share the corresponding mapped subexpressions. function map_indices(map::IndexMap, f::MOI.ScalarNonlinearFunction) diff --git a/test/FileFormats/NL/test_read.jl b/test/FileFormats/NL/test_read.jl index 6e8a456e4e..e9f7e0c747 100644 --- a/test/FileFormats/NL/test_read.jl +++ b/test/FileFormats/NL/test_read.jl @@ -648,8 +648,10 @@ function test_hs071() end model_print = sprint(print, model) _in_ = @static Sys.iswindows() ? "in" : "∈" + # Julia versions differ in how they print unary operators. + unary_plus = Expr(:call, :+, 2.0) for line in [ - "(1.1 * v[1] + 1.4 * v[2] + 1.2 * v[3] + 1.3 * v[4]) + +2.0", + "(1.1 * v[1] + 1.4 * v[2] + 1.2 * v[3] + 1.3 * v[4]) + $unary_plus", "v[1] >= 1.1", "v[2] >= 1.4", "v[3] >= 1.2",