Skip to content

chore: improve initialization perfomance - #2856

Merged
triceo merged 15 commits into
TimefoldAI:mainfrom
zepfred:perf/valuerange
Dec 3, 2026
Merged

triceo merged 15 commits into
TimefoldAI:mainfrom
zepfred:perf/valuerange

Conversation

@zepfred

@zepfred zepfred commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Several places built, deduplicated and validated full value ranges when all they needed was a count. On large datasets, especially when entities have their own value ranges, that made setup and solution updates slower than necessary. This PR reads sizes directly where it safely can, and builds value ranges only when the values themselves are needed.

Changes

  1. Skip shared entity value ranges when building the solution range.
    Entities that share the same value range instance (for example, one list of all values) are expanded into the composite value range only once.
  2. Count entity value range values without building the value range.
    The problem size statistics read the size of each entity's value range directly from the provider, calling it once per entity.
    Counting runs the same cheap checks as building: null value range, null first or last element, and a Set with undefined iteration order or a null element.
    Value ranges from the solution are still built, deduplicated and cached when counted, because the list variable state needs them anyway.
  3. Validate value ranges only when necessary.
    A new score director builder option, withAssertValueRange, is on by default, independently of lookup; the solver always validates.
    It is off where the solution is already validated or nothing relies on it:
    move thread score directors (they work on a clone of a validated solution), redundant score-from-scratch assertions,
    and shadow-variable-only updates (SolutionManager.update(solution, UPDATE_SHADOW_VARIABLES_ONLY) and SolutionManager.updateShadowVariables(solution)), documented in the upgrade recipe.
    Even without validation, setWorkingSolution still fails when the lists hold more values than the value range, naming the value outside of it, or reporting a value assigned more than once.
    It also still detects a null @ProblemFactCollectionProperty.
  4. Compute problem size statistics only when requested.
    They are no longer computed in setWorkingSolution, and the value count and the problem scale are computed in a single pass over the entities.
  5. Read the Construction Heuristic's step limit from the list variable state.
    For a list variable, the number of unassigned values now comes from the list variable state in O(1),
    instead of a pass over all entities at every Construction Heuristic start, including the nested one in every Ruin&Recreate move.

Fixes

  • Adding, changing or removing a problem fact now rebuilds the value ranges, as adding or removing an entity already did. Before, a cached value range could stay stale after such a change, for example after replacing the solution's value list.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@triceo

triceo commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

I have not reviewed the PR yet, will do that next week. But I have some notes based on the description:

Out-of-range assigned values are no longer reported when lookup is disabled.

Out-of-range assigned values are a bug. If we can no longer report that bug to users, then the performance optimization has gone too far.

Counting an entity's value range before it is built doesn't validate the provider's result, so a Set with a null element is counted as-is until the range is built.

The count must never be incorrect. Which is why we first build the ranges - so that the count will always match reality.

I do not like the consequences of these changes. How much performance are we really gaining in exchange? Considering we have not seen complaints about solver cold start performance, I am not in favor of introducing these behavior changes.

@zepfred

zepfred commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor Author

I have not reviewed the PR yet, will do that next week. But I have some notes based on the description:

Out-of-range assigned values are no longer reported when lookup is disabled.

Out-of-range assigned values are a bug. If we can no longer report that bug to users, then the performance optimization has gone too far.

Counting an entity's value range before it is built doesn't validate the provider's result, so a Set with a null element is counted as-is until the range is built.

The count must never be incorrect. Which is why we first build the ranges - so that the count will always match reality.

I do not like the consequences of these changes. How much performance are we really gaining in exchange? Considering we have not seen complaints about solver cold start performance, I am not in favor of introducing these behavior changes.

I added a flag to the score director to control when we validate the value range. This is still a draft, and I'm checking whether we can skip validation in certain scenarios without causing an inconsistent state.

The results show that the enrichment process decreased from 21 seconds to 6 seconds in my local tests, and Marek was able to reproduce these results as well.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the solver initialization and solution-update optimizations.

No open issues 🎉

Review effort: Balanced

@triceo triceo left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Some of my comments were marked as resolved, but not actually resolved.
  • The validation code in AbstractScoreDirector needs to be refactored to handle a single concept in a single place.
  • Github is showing me Git conflicts, although I'm not exactly sure why.

@triceo
triceo merged commit ff4432c into TimefoldAI:main Dec 3, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add SolutionUpdatePolicy skipping unnecessary initialization

4 participants