[mercury-users] Strange performance anomaly; can anyone confirm?

Peter Wang novalazy at gmail.com
Mon Mar 28 15:52:12 AEDT 2011


On 2011-03-28, Chris King <colanderman at gmail.com> wrote:
> While writing an n-queens implementation I encountered an anomaly
> whereby simply wrapping the body of a predicate in a dummy if/then
> causes a 2x speedup.  My code is attached (trimmed down as much as
> possible), can anyone confirm?  I'm testing in the asm_fast.gc grade
> on amd64.
> 
> My presumption is simply that the conditional is causing some
> optimization to be triggered, but -O 6 doesn't seem to make a
> difference.  Any thoughts?

Below is the diff of the HLDS dumps (mmc -dall) at a certain stage.
Without the conditional, the set.insert call is not being delayed til
after the set.member check, as would be expected.

The workaround is to write the construction at the end of the
predicate instead of in the head.

Peter

diff -u -r -b SLOWER/n_queens_bugged.hlds_dump.035-modecheck FASTER/n_queens_bugged.hlds_dump.035-modecheck
--- SLOWER/n_queens_bugged.hlds_dump.035-modecheck      2011-03-28 15:33:53.235784125 +1100
+++ FASTER/n_queens_bugged.hlds_dump.035-modecheck      2011-03-28 15:33:14.489867503 +1100
@@ -72,15 +72,21 @@
 :- mode place_rook((builtin.in), (builtin.in), (builtin.in), (builtin.out)) is nondet.
 n_queens_bugged.place_rook(N, Row, Posns, HeadVar__4) :-
   TypeCtorInfo_13 = type_ctor_info("n_queens_bugged", "pos", 0),
+  (if
+    true
+  then
   V_11 = 1,
   int.nondet_int_in_range(V_11, N, Col),
-  V_10 = n_queens_bugged.pos(Row, Col),
-  HeadVar__4 = set.insert(TypeCtorInfo_13, Posns, V_10),
   \+ (
     V_12 = n_queens_bugged.pos(V_9, Col),
     set.member(TypeCtorInfo_13, V_14, Posns),
     V_12 = V_14
-  ).
+    )
+  else
+    fail
+  ),
+  V_10 = n_queens_bugged.pos(Row, Col),
+  HeadVar__4 = set.insert(TypeCtorInfo_13, Posns, V_10).
 
   % pred id 2
   % mode number 0 of predicate `n_queens_bugged.n_rooks'/2 (erroneous):

--------------------------------------------------------------------------
mercury-users mailing list
Post messages to:       mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions:          mercury-users-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the users mailing list