number of clauses grows despite retractall

30 views
Skip to first unread message

Eric Kow

unread,
Oct 21, 2014, 11:14:34 AM10/21/14
to swi-p...@googlegroups.com
Hi everybody,

I'm trying to track down some performance problems with my program (it slows down and eats more memory as it loops through a large number of files, each input being essentially independent of each other).

Using the statistics procedure, I notice that the number of 'clauses' grows on each iteration.

I notice also, as in this code snippet below, that retracting the clauses does not make a difference
(as a sanity check, not asserting them in the first place keeps them constant).

Is this to be expected?
Are there any space/performance implications for my program if it does a lot of asserting and retracting of clauses?

Thanks!

Eric

--------------8<----------------------

maxcount(10000000).

main :- dynamic(foo/1),
        do_loop(0).

do_loop(N) :- maxcount(Max), N >= Max.
do_loop(N) :-
statistics(clauses, Count),
write(N), write('\t'), write(Count), nl,
assert(foo(N)),
retractall(foo),
Np1 is N+1,
do_loop(Np1).

Jan Wielemaker

unread,
Oct 21, 2014, 11:25:00 AM10/21/14
to Eric Kow, swi-p...@googlegroups.com
On 10/21/2014 05:14 PM, Eric Kow wrote:
> Hi everybody,
>
> I'm trying to track down some performance problems with my program (it
> slows down and eats more memory as it loops through a large number of
> files, each input being essentially independent of each other).
>
> Using the statistics procedure, I notice that the number of 'clauses'
> grows on each iteration.
>
> I notice also, as in this code snippet below, that retracting the
> clauses does not make a difference
> (as a sanity check, not asserting them in the first place keeps them
> constant).
>
> Is this to be expected?
> Are there any space/performance implications for my program if it does a
> lot of asserting and retracting of clauses?
>
> Thanks!
>
> Eric
>
> --------------8<----------------------
>
> maxcount(10000000).
>
> main :- dynamic(foo/1),
> do_loop(0).

dynamic/1 is a directive. SWI-Prolog also accepts it as
a goal, but it is still better style to use

:- dynamic foo/1.

> do_loop(N) :- maxcount(Max), N >= Max.
> do_loop(N) :-
> statistics(clauses, Count),
> write(N), write('\t'), write(Count), nl,
> assert(foo(N)),
> retractall(foo),

This retracts foo/0, while you assert to foo/1. retractall/1 on an
undefined predicate is not at error, but creates the predicate as
dynamic. If you do

retractall(foo(_)),

it runs nicely in constant space. Note that foo/0 and foo/1 are two
completely distinct predicates.

Cheers --- Jan

> Np1 is N+1,
> do_loop(Np1).
>
> --
> You received this message because you are subscribed to the Google
> Groups "SWI-Prolog" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to swi-prolog+...@googlegroups.com
> <mailto:swi-prolog+...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/swi-prolog.
> For more options, visit https://groups.google.com/d/optout.

Eric Kow

unread,
Oct 21, 2014, 11:31:11 AM10/21/14
to Jan Wielemaker, swi-p...@googlegroups.com
Many thanks, Jan!

Hmm, that does solve the problem for my example snippet, but in my
real code (where I had remembered the distinction and did something
like retractall(foo(_,_,_,_)) for the clause in question, I'm still
experiencing this sort of growth.

But this narrows things down a bit. I'll hopefully follow up when I
have a better idea what's going on.

Cheers,

Eric

PS: thanks also for the directive tip
--
Eric Kow <http://erickow.com>

Pierpaolo Bernardi

unread,
Oct 21, 2014, 11:44:06 AM10/21/14
to Eric Kow, Jan Wielemaker, swi-p...@googlegroups.com
On Tue, Oct 21, 2014 at 5:31 PM, Eric Kow <eric...@gmail.com> wrote:
> Many thanks, Jan!
>
> Hmm, that does solve the problem for my example snippet, but in my
> real code (where I had remembered the distinction and did something
> like retractall(foo(_,_,_,_)) for the clause in question, I'm still
> experiencing this sort of growth.

Can't you just issue a "listing" and see what clauses are present in the db?
(Eventualy directing the output to a file, if it is overwhelming)

Eric Kow

unread,
Oct 21, 2014, 1:00:40 PM10/21/14
to swi-p...@googlegroups.com, eric...@gmail.com, J.Wiel...@vu.nl
On Tuesday, 21 October 2014 16:44:06 UTC+1, Pierpaolo Bernardi wrote:
Can't you just issue a "listing" and see what clauses are present in the db?
(Eventualy directing the output to a file, if it is overwhelming)

FOUND IT! Thank-you so much for your simple and sensible debugging suggestion.

In diffing the two runs, I discovered that for every foo(...) the code was asserting (and retracting), there was also a bar(foo(...)) that it was asserting (but not retracting).

Hooray for constant space!

Thanks again :-)

Eric 

Eric Kow

unread,
Oct 21, 2014, 1:45:33 PM10/21/14
to swi-p...@googlegroups.com, eric...@gmail.com
Hi all, apologies for the potential repeat (google-groups ate my reply?)

On Tuesday, 21 October 2014 16:44:06 UTC+1, Pierpaolo Bernardi wrote:
Can't you just issue a "listing" and see what clauses are present in the db?
(Eventualy directing the output to a file, if it is overwhelming)

FOUND IT! Thank-you so much for your simple and sensible debugging suggestion.

In diffing the two runs, I discovered that for every foo(...) the code was asserting (and retracting), there was also a bar(foo(...)) that it was asserting (but not retracting).

Hooray for constant space!

Thanks again :-) 

Eric


Reply all
Reply to author
Forward
0 new messages