Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dif/2 has too much overhead #2

Open
aarroyoc opened this issue Nov 7, 2021 · 1 comment
Open

dif/2 has too much overhead #2

aarroyoc opened this issue Nov 7, 2021 · 1 comment

Comments

@aarroyoc
Copy link
Owner

aarroyoc commented Nov 7, 2021

Comparing two implementations of string_//1:

A:

string_([X|Xs]) -->
    [X],
    {
        \+ member(X, "{}%")
    },
    string_(Xs).

string_([]) -->
    [].
Listening at port 8080
2021-11-07 (13:51:23) get /dice.svg
   % CPU time: 0.049 seconds
   % CPU time: 0.053 seconds
2021-11-07 (13:51:24) get /dice.svg
   % CPU time: 0.039 seconds
   % CPU time: 0.042 seconds
2021-11-07 (13:51:25) get /dice.svg
   % CPU time: 0.047 seconds
   % CPU time: 0.050 seconds
2021-11-07 (13:51:25) get /dice.svg
   % CPU time: 0.038 seconds
   % CPU time: 0.040 seconds
2021-11-07 (13:51:26) get /dice.svg
   % CPU time: 0.037 seconds
   % CPU time: 0.039 seconds
2021-11-07 (13:51:27) get /dice.svg
   % CPU time: 0.038 seconds
   % CPU time: 0.040 seconds

A behaves well, is fast.

B:

string_([X|Xs]) -->
    [X],
    {
        maplist(dif(X), "{}%")
    },
    string_(Xs).

string_([]) -->
    [].
?- listen(8080).
Listening at port 8080
2021-11-07 (13:52:26) get /
2021-11-07 (13:52:26) get /htmx.min.js
2021-11-07 (13:52:28) get /dice.svg
   % CPU time: 1.056 seconds
   % CPU time: 1.058 seconds
2021-11-07 (13:52:31) get /dice.svg
   % CPU time: 1.077 seconds
   % CPU time: 1.079 seconds

B is much much slower.

@triska
Copy link

triska commented Nov 10, 2021

dif/2 seems not too costly, it seems the problem is the meta-call induced by maplist/2. This can be compiled away with an auxiliary predicate that simply explicitly invokes dif/2 for each of the list elements. A goal expansion could do this automatically, for all instantiated partial goals that occur in the first argument of maplist/2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants