Improve compilation and execution of `bagof/3` and `setof/3` goals