You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Speeding up the creation of Instance from a several megabyte string #199
I'm creating a somewhat large model and creating a Model via the add_string method. Then, quite a bit of time is spent on creating an instance of Instance. My Minizinc instance when written on the disk takes about 8-9 megabytes.
The docs don't seem to touch upon the issue, but are there some general guidelines or best practices to apply when handling instances of this size (or larger)?
That 8-9MB on disk is the tell — most of it is almost certainly parameter data serialized as literal MiniZinc text in your add_string calls. The library writes that whole blob to a temp .mzn, and the CLI re-parses and re-flattens it on every Instance, so cost tracks the text size, not the problem's logical size.
Two things usually help a lot:
Separate model from data. Keep the .mzn (logic) small and stable and feed parameter arrays as data, not text — assign via instance["x"] = value or a .dzn. Flattening a short model with bound data is far cheaper than flattening a multi-MB literal.
Watch array literals/comprehensions that blow up during flattening — that's where mzn2fzn goes super-linear, not parsing.
If you solve many similar instances, flatten once and only swap the data instead of rebuilding the string. Worth timing flatten vs solve separately first to confirm where the time actually goes.
Activity
Whatsonyourmind commentedon Jun 9, 2026
That 8-9MB on disk is the tell — most of it is almost certainly parameter data serialized as literal MiniZinc text in your add_string calls. The library writes that whole blob to a temp .mzn, and the CLI re-parses and re-flattens it on every Instance, so cost tracks the text size, not the problem's logical size.
Two things usually help a lot:
Separate model from data. Keep the .mzn (logic) small and stable and feed parameter arrays as data, not text — assign via instance["x"] = value or a .dzn. Flattening a short model with bound data is far cheaper than flattening a multi-MB literal.
Watch array literals/comprehensions that blow up during flattening — that's where mzn2fzn goes super-linear, not parsing.
If you solve many similar instances, flatten once and only swap the data instead of rebuilding the string. Worth timing flatten vs solve separately first to confirm where the time actually goes.