- elem.max_consumption_weight = std::max(elem.max_consumption_weight, value);
- if (cnst->sharing_policy_ != Constraint::SharingPolicy::FATPIPE)
- elem.consumption_weight += value;
- else
- elem.consumption_weight = std::max(elem.consumption_weight, value);
-
- // We need to check that increasing value of the element does not cross the concurrency limit
- if (var->sharing_penalty_ != 0.0) {
- if (cnst->get_concurrency_slack() < elem.get_concurrency()) {
- double penalty = var->sharing_penalty_;
- disable_var(var);
- for (Element const& elem2 : var->cnsts_)
- on_disabled_var(elem2.constraint);
- var->staged_penalty_ = penalty;
- xbt_assert(not var->sharing_penalty_);
- }
- elem.increase_concurrency();
+ bool reuse_elem = elem_it != end(var->cnsts_) && not force_creation;
+ if (reuse_elem && var->sharing_penalty_ != 0.0) {
+ /* before changing it, decreases concurrency on constraint, it'll be added back later */
+ elem_it->decrease_concurrency();
+ }
+ Element& elem = reuse_elem ? expand_add_to_elem(*elem_it, cnst, consumption_weight)
+ : expand_create_elem(cnst, var, consumption_weight);
+
+ // Check if we need to disable the variable
+ if (var->sharing_penalty_ != 0) {
+ /* increase concurrency in constraint that this element uses.
+ * as we don't check if constraint has reached its limit before increasing,
+ * we can't check the correct state at increase_concurrency, anyway
+ * it'll check if the slack is smaller than 0 just below */
+ elem.increase_concurrency(false);
+ if (cnst->get_concurrency_slack() < 0) {
+ double penalty = var->sharing_penalty_;
+ disable_var(var);
+ for (Element const& elem2 : var->cnsts_)
+ on_disabled_var(elem2.constraint);
+ var->staged_sharing_penalty_ = penalty;
+ xbt_assert(not var->sharing_penalty_);