Skip to content

Commit e858645

Browse files
optimize ACRule
1 parent 8889473 commit e858645

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/rule.jl

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,15 +465,40 @@ function (acr::ACRule)(term)
465465

466466
T = symtype(term)
467467
args = arguments(term)
468+
is_full_perm = acr.arity == length(args)
469+
if is_full_perm
470+
args_buf = copy(parent(args))
471+
else
472+
args_buf = ArgsT(@view args[1:acr.arity])
473+
end
468474

469475
itr = acr.sets(eachindex(args), acr.arity)
470476

471477
for inds in itr
472-
result = r(Term{T}(f, @views args[inds]))
478+
for (i, ind) in enumerate(inds)
479+
args_buf[i] = args[ind]
480+
end
481+
# this is temporary and only constructed so the rule can
482+
# try and match it - no need to hashcons it.
483+
old_hc = ENABLE_HASHCONSING[]
484+
ENABLE_HASHCONSING[] = false
485+
tempterm = Term{T}(f, args_buf)
486+
ENABLE_HASHCONSING[] = old_hc
487+
# this term will be hashconsed regardless
488+
result = r(tempterm)
473489
if result !== nothing
474490
# Assumption: inds are unique
475-
length(args) == length(inds) && return result
476-
return maketerm(typeof(term), f, [result, (args[i] for i in eachindex(args) if i inds)...], metadata(term))
491+
is_full_perm && return result
492+
inds_set = BitSet(inds)
493+
full_args_buf = ArgsT(@view args[1:(length(args)-acr.arity+1)])
494+
idx = 1
495+
for i in eachindex(args)
496+
i in inds_set && continue
497+
full_args_buf[idx] = args[i]
498+
idx += 1
499+
end
500+
full_args_buf[idx] = result
501+
return maketerm(typeof(term), f, full_args_buf, metadata(term))
477502
end
478503
end
479504
end

0 commit comments

Comments
 (0)