Skip to content

Commit 4717e61

Browse files
nelhageNikolajBjorner
authored andcommitted
Add a fast-path to _coerce_exprs. (#7995)
When the inputs are already the same sort, we can skip most of the coercion logic and just return. Currently, `_coerce_exprs` is by far the most expensive part of building up many common Z3 ASTs, so this fast-path is a substantial speedup for many use-cases.
1 parent aad55de commit 4717e61

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/api/python/z3/z3.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,18 @@ def _coerce_expr_merge(s, a):
12451245
else:
12461246
return s
12471247

1248+
def _check_same_sort(a, b, ctx=None):
1249+
if not isinstance(a, ExprRef):
1250+
return False
1251+
if not isinstance(b, ExprRef):
1252+
return False
1253+
if ctx is None:
1254+
ctx = a.ctx
1255+
1256+
a_sort = Z3_get_sort(ctx.ctx, a.ast)
1257+
b_sort = Z3_get_sort(ctx.ctx, b.ast)
1258+
return Z3_is_eq_sort(ctx.ctx, a_sort, b_sort)
1259+
12481260

12491261
def _coerce_exprs(a, b, ctx=None):
12501262
if not is_expr(a) and not is_expr(b):
@@ -1259,6 +1271,9 @@ def _coerce_exprs(a, b, ctx=None):
12591271
if isinstance(b, float) and isinstance(a, ArithRef):
12601272
b = RealVal(b, a.ctx)
12611273

1274+
if _check_same_sort(a, b, ctx):
1275+
return (a, b)
1276+
12621277
s = None
12631278
s = _coerce_expr_merge(s, a)
12641279
s = _coerce_expr_merge(s, b)

0 commit comments

Comments
 (0)