Fix performance issue in MkAnd(IEnumerable) and eliminate code duplication #7851
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.
The
MkAnd(IEnumerable<BoolExpr> t)
method inContext.cs
had significant performance issues due to multiple redundant enumerations of the input collection:t.All(a => a != null)
- First enumeration to check for nullsCheckContextMatch<BoolExpr>(t)
- Second enumeration for context validationt.ToArray()
- Third enumeration to create arrayt.Count()
- Fourth enumeration to get count after array was already createdThis was particularly problematic when calling
MkAnd
with large collections, as each enumeration has O(n) complexity.Changes Made
Performance Fix
t.ToArray()
(single enumeration)ands.Length
instead oft.Count()
to eliminate redundant enumerationCheckContextMatch((Z3Object[])ands)
for efficiencyCode Duplication Elimination
Following feedback to eliminate duplicated functionality, refactored all IEnumerable methods to delegate to their corresponding params array variants:
MkAnd(IEnumerable<BoolExpr>)
→ callsMkAnd(params BoolExpr[])
MkOr(IEnumerable<BoolExpr>)
→ callsMkOr(params BoolExpr[])
MkAdd(IEnumerable<ArithExpr>)
→ callsMkAdd(params ArithExpr[])
MkMul(IEnumerable<ArithExpr>)
→ callsMkMul(params ArithExpr[])
MkApp(FuncDecl, IEnumerable<Expr>)
→ callsMkApp(FuncDecl, params Expr[])
Before:
After:
This approach:
MkDistinct
Testing
Fixes #7850.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.