The bagof/3 choker in SWI-Prolog

It seems that SWI-Prolog might still have long
instantiation chains. Here is a new bagof/3 choker:

test :-
   bagof(X, N^(between(1,300,X), between(1,300,N), 
      length(_,N)), _), fail; true.

This is what I get, did abort it after one minute:

/* SWI-Prolog 9.3.14 */
?- time(test).
Action (h for help) ? abort
% 468,739 inferences, 52.016 CPU in 53.069 seconds (98% CPU, 9012 Lips)
% Execution Aborted

Interestingly recent Trealla Prolog can still stand it:

/* Trealla Prolog 2.59.17 */
?- time(test).
% Time elapsed 13.280s, 16158649 Inferences, 1.217 MLips
   true.

There was a recent change in Trealla Prolog to
use term_variables/3 for canonicalization of variables.
SWI-Prolog uses more, it also uses alloc_bind_key_list/2.

But I guess alloc_bind_key_list/2 is too limited, since it only
deals with the variables known when bagof/3 is invoked. On
the other hand it seems Trealla Prolog uses term_variable/3 alone

and it is fortunate to not provoke long instantiation chains.

See also here:

https://github.com/trealla-prolog/trealla/issues/614#issuecomment-2446621289

Edit 09.11.2024
I don’t now what details internally prevent Trealla Prolog
from getting long instantiation chains. I also saw Trealla
Prolog experimenting with this bootstrapping:

term_variables(P1, P2, P3) :-
	term_variables(P1, P4),
	append(P4, P3, P2).

But I don’t know what the final take was.