]> Repositorios git - scryer-prolog.git/commitdiff
ENHANCED: more memory efficient call_nth/2 (#1264)
authorMarkus Triska <[email protected]>
Sat, 2 Apr 2022 08:31:56 +0000 (10:31 +0200)
committerMarkus Triska <[email protected]>
Sat, 2 Apr 2022 15:42:45 +0000 (17:42 +0200)
I hope that this will allow more extensive tests of Scryer Prolog. Enjoy!

src/lib/iso_ext.pl

index 6a1f007aac507133dcb548b03127192a04e8cb4c..f503c4a2d6431b059248558971a83b6bec688933 100644 (file)
@@ -210,28 +210,33 @@ call_nth(Goal, N) :-
         )
     ;   true
     ),
-    setup_call_cleanup(call_nth_nesting(ID),
+    setup_call_cleanup(call_nth_nesting(C, ID),
                        (   Goal,
-                           retract(i_call_nth_nesting(ID,N0)),
+                           bb_get(ID, N0),
                            N1 is N0 + 1,
-                           asserta(i_call_nth_nesting(ID,N1)),
+                           bb_put(ID, N1),
                            (   integer(N) ->
                                N = N1,
                                !
                            ;   N = N1
                            )
                        ),
-                       (   retract(i_call_nth_nesting(ID,_)),
-                           retract(i_call_nth_counter(ID))
+                       (   bb_get(i_call_nth_counter, C) ->
+                           C1 is C - 1,
+                           bb_put(i_call_nth_counter, C1)
+                       ;   true
                        )).
 
-call_nth_nesting(ID) :-
-    (   i_call_nth_counter(ID0) ->
-        ID is ID0 + 1
-    ;   ID = 0
+call_nth_nesting(C, ID) :-
+    (   bb_get(i_call_nth_counter, C0) ->
+        C is C0 + 1
+    ;   C = 0
     ),
-    asserta(i_call_nth_nesting(ID, 0)),
-    asserta(i_call_nth_counter(ID)).
+    number_chars(C, Cs),
+    atom_chars(Atom, Cs),
+    atom_concat(i_call_nth_nesting_, Atom, ID),
+    bb_put(ID, 0),
+    bb_put(i_call_nth_counter, C).
 
 
 copy_term_nat(Source, Dest) :-