Skip to content

Performance #1513

Answered by triska
Fflath asked this question in Q&A
Jun 17, 2022 · 7 comments · 9 replies
Discussion options

You must be logged in to vote

sum_list/2 is currently quite slow: It internally uses foldl/4, and that currently doesn't compile away the dynamic call. You can implement sum_list/2 manually much faster:

my_sum_list([], 0).
my_sum_list([L|Ls], S) :-
        sum_list_(Ls, L, S).

sum_list_([], S, S).
sum_list_([L|Ls], S0, S) :-
        S1 is S0 + L,
        sum_list_(Ls, S1, S).

Even further speedups can be achieved by moving the implementation to Rust, and that may be worth doing for a very small set of very frequently needed predicates (such as length/2). In general, it is better to instead work on improving the Prolog engine, since that will benefit all programs instead of only these few primitives.

Replies: 7 comments 9 replies

Comment options

You must be logged in to vote
2 replies
@Fflath
Comment options

@triska
Comment options

Comment options

You must be logged in to vote
2 replies
@Fflath
Comment options

@triska
Comment options

Comment options

You must be logged in to vote
1 reply
@triska
Comment options

Comment options

You must be logged in to vote
4 replies
@mthom
Comment options

@triska
Comment options

@mthom
Comment options

@triska
Comment options

Answer selected by Fflath
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants