Check for a var_table being empty directly.

This yields a speedup of about 2.4% when compiling options.m.

compiler/polymorphism_clause.m:
    Don't count the variables in a var_table when we need only
    an emptyness test.

library/tree234.m:
    Make the code counting map elements partially tail recursive.
This commit is contained in:
Zoltan Somogyi
2025-11-20 14:47:46 +11:00
parent ee9c7d3a84
commit 2bbda5abfa
2 changed files with 29 additions and 19 deletions

View File

@@ -427,9 +427,8 @@ produce_clause_existq_tvars(PredInfo, HeadVars, UnconstrainedTVars,
% Figure out the bindings for any unconstrained existentially quantified
% type variables in the head.
var_table_count(VarTable0, NumVarsInDb0),
( if
NumVarsInDb0 = 0
var_table_is_empty(VarTable0)
then
% This can happen for compiler generated procedures.
map.init(PredToActualTypeSubst)

View File

@@ -3721,24 +3721,35 @@ sorted_keys_match_in_tree(four(K0, _V0, K1, _V1, K2, _V2, T0, T1, T2, T3),
%---------------------------------------------------------------------------%
count(T) = N :-
tree234.count(T, N).
tree234.acc_count(T, 0, N).
count(empty, 0).
count(two(_, _, T0, T1), N) :-
tree234.count(T0, N0),
tree234.count(T1, N1),
N = 1 + N0 + N1.
count(three(_, _, _, _, T0, T1, T2), N) :-
tree234.count(T0, N0),
tree234.count(T1, N1),
tree234.count(T2, N2),
N = 2 + N0 + N1 + N2.
count(four(_, _, _, _, _, _, T0, T1, T2, T3), N) :-
tree234.count(T0, N0),
tree234.count(T1, N1),
tree234.count(T2, N2),
tree234.count(T3, N3),
N = 3 + N0 + N1 + N2 + N3.
count(T, N) :-
tree234.acc_count(T, 0, N).
:- pred acc_count(tree234(K, V)::in, int::in, int::out) is det.
acc_count(T, !N) :-
(
T = empty
;
T = two(_, _, T0, T1),
!:N = !.N + 1,
acc_count(T0, !N),
acc_count(T1, !N)
;
T = three(_, _, _, _, T0, T1, T2),
!:N = !.N + 2,
acc_count(T0, !N),
acc_count(T1, !N),
acc_count(T2, !N)
;
T = four(_, _, _, _, _, _, T0, T1, T2, T3),
!:N = !.N + 3,
acc_count(T0, !N),
acc_count(T1, !N),
acc_count(T2, !N),
acc_count(T3, !N)
).
%---------------------------------------------------------------------------%