Fast Looping in Prolog

When coding my prolog VM I noticed that the overhead of a prolog call is in the order of 2M execution per second, and this is typically what I see when executing simple prolog perdicates in swi prolog as well. So all complexity that one need to care for simply means a hefty work load for the solver. I can do about 20M operations a second for simple math operations where the stack can be used, and this can be increased with the help of analyzing type flows. The goal is to go up to 100M operations per second as close to the new guile speeds on simple loops. Now, this means that inlining will have dramatic effect on speed of prolog programs and a way to do JIT compiling (still on the VM and later natively) would be helpfull. Anyway here is a simple addition to the prolog I use on the VM:

% A fast member
member(X,L) :-
  recur * lp((LL,L)),
    ( 
      LL=[A|LLL] -> 
        (
           A=X;
           lp(LLL)
        )
    ).

% A fast summer 
sumall(L,S) :-
 recur * lp((SS,0),(LL,L)),
   LL=[X|LLL] ->
     (
        SSS is SS + X,
        lp(SSS,LLL)
     );
     S=SS.

With this I get really fast implementations when benchmarking. Now this will not work for normal prolog uses, only in the case when lp i slocated in a tail context and when the bound variables inside the recur statement can be alocated to the stack will it work. This is a tool for the expert.

Happy Hacking

links

social