[mercury-users] First solution in csharp without try/catch

Jeff Thompson jeff at thefirst.org
Tue Jan 17 08:18:35 AEDT 2012

On 1/15/2012 4:59 AM, Mark Brown wrote:
> On 14-Jan-2012, Jeff Thompson<jeff at thefirst.org>  wrote:
>> On 1/13/2012 8:46 PM, Mark Brown wrote:
>>> How does the version
>>> using exceptions compare to the handwritten version using yield?
>> As above, the times for Mercury code are 0.45 seconds for the baseline and
>> 171 seconds if it commits.  For the "yield" code it is 2.33 seconds for the
>> baseline and 2.03 if it commits after one solution.  As expected, since the
>> "yield" code uses the same infrastructure in both cases, it takes less time
>> to search less solutions.  But the baseline Mercury code is faster than the
>> "yield" code which allocates an iterator object on the heap for every
>> "foreach" loop (as opposed to Mercury's more efficient approach of using
>> continuations).
>>                    X>  2    X>  0
>> Mercury 11.07     0.45     171
>> "yield"           2.33     2.03
> Thanks for that - it's useful to see how much yield costs.
> Given these results, I'd back the continuations+exception approach rather
> than yield, since the exception is thrown at most once whereas the number
> of yield iterators can be arbitrarily large.  I.e., the slowdown in the
> first column will eventually outweigh the speedup in the second.
> Unless of course the search is very simple, in which case it may be
> better to write it yourself (see below).

I agree that the continuations approach is more efficient than yield.  
My searches are too complicated to "unwind" the nondet portion in order 
to check for commit explicitly.

The alternative approach, of course, is for the compiler to add "ref 
bool commit" to each multi pred as below to check for commit 
explicitly.  I ran a benchmark and it seems to run as fast as the 
default version of test_0_p_0 within the measurement variation of the 
test runs.  Is it possible to add this? If I wanted to look at modifying 
the compiler to output "ref bool commit", which source code files would 
I start with?

private class Test_0_p_0_env_0
     public bool succeeded;
     public int X_1;
public delegate void MethodPtr2_r0c<A1, A2>(A1 a1, A2 a2, ref bool commit);
public static bool test_0_p_0_commit()
     var env_ptr = new testYield.Test_0_p_0_env_0();
     bool commit = false;
     multiPred_2_p_0(10, testYield.test_0_p_0_3, env_ptr, ref commit);
     return env_ptr.succeeded;
public static void multiPred_2_p_0(int M_1, MethodPtr2_r0c<int, object> 
cont, object cont_env_ptr, ref bool commit)
     cont(1 * M_1, cont_env_ptr, ref commit);
     if (commit) return;
     cont(2 * M_1, cont_env_ptr, ref commit);
     if (commit) return;
private static void test_0_p_0_3(int arg1, object env_ptr_arg, ref bool 
     var env_ptr = (testYield.Test_0_p_0_env_0)env_ptr_arg;

     env_ptr.X_1 = arg1;
     env_ptr.succeeded = (env_ptr.X_1 > 20);
     if (env_ptr.succeeded)
         commit = true;

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