Skip to content

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

Merged
blegat merged 5 commits into
masterfrom
bl/juliac-json
Nov 27, 2026
Merged

blegat merged 5 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 😇

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
blegat merged commit c031ad3 into master Nov 27, 2026
31 checks passed
@blegat
blegat deleted the bl/juliac-json branch November 27, 2026 12:34
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.
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