[mercury-users] memo and zero-arity functions

Michael Day mikeday at bigpond.net.au
Sat Jul 29 13:10:11 AEST 2006


Hi,

Would it be possible to optimise the implementation of pragma memo for
zero-arity functions, like this:

    :- pragma memo(bigfunc/0).

    :- func bigfunc = complicated_type.

    bigfunc = big_nasty_function_symbol_with_many_arguments(...).

Perhaps the current implementation is optimised for functions with
arguments that can return many different values, as Prince gains a 2%
speed up if I replace this function with an impure one that uses a global
variable to store the single return value, like this:

:- pragma promise_pure(bigfunc/0).

bigfunc = Value :-
    ( if impure value_exists then
        impure get_value = Value
    else
        impure set_value(make_value) = Value
    ).

:- pragma foreign_decl(c, "
extern MR_Word GlobalValue;
").

:- pragma foreign_code(c, "
MR_Word GlobalValue = NULL;
").

:- impure pred value_exists is semidet.

:- pragma foreign_proc(c,
        value_exists,
        [will_not_call_mercury, thread_safe], "
    SUCCESS_INDICATOR = (Value != NULL);
").

:- impure func get_value = value.

:- pragma foreign_proc(c,
        get_value = (Value::out),
        [will_not_call_mercury, thread_safe], "
    Value = GlobalValue;
").

:- impure func set_value(value) = value.

:- pragma foreign_proc(c,
        set_value(Value0::in) = (Value::out),
        [will_not_call_mercury, thread_safe], "
    Value = GlobalValue = Value0;
").

Clearly, adding a single pragma is a lot more elegant and less scary than
a page of impure foreign code, but it would be nice to keep the elegance
without sacrificing any speed.

Cheers,

Michael

-- 
Print XML with Prince!
http://www.princexml.com
--------------------------------------------------------------------------
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