Repository navigation
[FileFormats.MOF] write the document with JSON.Object - #3151
Merged
Merged
Conversation
blegat
added a commit
that referenced
this pull request
Nov 23, 2026
Say which part of the document differs from the traced model, and refer to #3151 where they are introduced.
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 |
`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.
blegat
force-pushed
the
bl/juliac-json
branch
from
November 27, 2026 08:14
2adfa02 to
e3b6e66
Compare
blegat
added a commit
that referenced
this pull request
Nov 27, 2026
* [FileFormats.MOF] write the document with JSON.Object
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 |
* simplify
* comment
* [FileFormats.MOF] explain why _object declares String keys
`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.
* Explain what the untraced --starts and --linear commands check
Say which part of the document differs from the traced model, and refer to
#3151 where they are
introduced.
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.
Writing a model with 100,000 variables and 200,000 constraints (minimum of 5 runs):
NamedTuple(before)JSON.Object(after)JSON.jsonis 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 aVector{Pair{Symbol,Any}}, then callsNamedTuple(pairs). That computes theNamedTuple's type at run time, for each constraint.Also,
NamedTuple[]is aVectorwith 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😇