Skip to content

[FileFormats.MOF] write the document with JSON.Object - #3151

Open
blegat wants to merge 8 commits into
masterfrom
bl/juliac-json
Open

blegat wants to merge 8 commits into
masterfrom
bl/juliac-json

Conversation

@blegat

@blegat blegat commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Writing a model with 100,000 variables and 200,000 constraints (minimum of 5 runs):

Time Allocated
NamedTuple (before) 0.70 s 725 MiB
JSON.Object (after) 0.43 s 409 MiB

JSON.json is about 0.1 s in both cases, where we win is in building the objects.
For instance, moi_to_object(::ConstraintIndex, ...) builds each constraint's object as a Vector{Pair{Symbol,Any}}, then calls NamedTuple(pairs). That computes the NamedTuple's type at run time, for each constraint.
Also, NamedTuple[] is a Vector with an abstract element type, so every element written from it has a type known only at run time.
It also helps with JuliaC compilation since we don't need to implement a different method for each NamedTuple 😇

With `--trim=no`, the JuliaC test does not check that the executable still
works once it is trimmed. With `--trim=unsafe-warn`, JuliaC only compiles the
methods it can reach from `main` through calls that it can infer, so a call
that is dynamically dispatched fails at run time unless its method happens to
be compiled for another reason.

Like JuMPy does for its compiled backends, we first run MyApp in ordinary Julia
with `--trace-dispatch` and every method that was dynamically dispatched to
becomes an entry point of the image, see `entry.jl`. With this, the test passes
without any change to the source code of MOI so the following PRs can focus on
the code paths that the tracing does not cover.
The tracing only covers the code paths that the traced run actually executes.
To test the code paths that it does not cover, we add commands to MyApp that are
not traced. Each of them writes the traced model with one difference (a
duplicate name, another objective type, ...) so that it tests exactly one code
path that the tracing does not contain, and the test compares the file written
by the executable with the one written by Julia.

`_model` builds the traced model, `_run` runs a command with the executable and
with Julia, and the outer testset makes sure that all commands are run even if
one of them fails.
If the tracing does not contain a variable with a starting value, writing a MOF
file in which a variable has a starting value fails because methods are
missing. The objects of the variables and constraints were `NamedTuple`s whose
type depends on which fields are present, e.g., `(name = ..., primal_start =
...)`, and JSON writes each of these `NamedTuple` types with its own methods, so
every combination of fields would need to be traced.

The objects are now `JSON.Object{String,Any}` so JSON writes all of them with
the same methods, whatever their fields. An extension can still return a
`NamedTuple` from `moi_to_object`, JSON then writes it as before. This requires
JSON v1 for `JSON.Object`.

It is also faster since JSON doesn't need to specialize on each `NamedTuple`
type. Writing a model with 100,000 variables and 200,000 constraints (minimum
of 5 runs):

| | Time | Allocated |
| --- | --- | --- |
| `NamedTuple` (before) | 0.70 s | 725 MiB |
| `JSON.Object` (after) | 0.43 s | 409 MiB |
# Conflicts:
#	test/JuliaC/MyApp/src/MyApp.jl
#	test/JuliaC/test_juliac.jl
`Base.inferencebarrier` is not needed: in the varargs `_object`, Julia does not
specialize on the type of each pair anyway, so the type of `value` is already
unknown. What makes the insertion resolved at compile time is that the keys
are declared as `String`, so a single method of `setindex!` matches. Without the
barrier, the JuliaC test gives the same results.
Say which part of the document differs from the traced model, and refer to
#3151 where they are
introduced.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant