[m-users.] Fwd: Documenting univ data passing to and from the C FFI

Zoltan Somogyi zoltan.somogyi at runbox.com
Thu May 20 19:40:36 AEST 2021


2021-05-20 18:48 GMT+10:00 "Fabrice Nicol" <fabrnicol at gmail.com>:
>  > "*Why do you need to pass univs across the Mercury-C boundary?*
> *Why do you need to unpack them in C code?"*
> 
> Performance issues.

I am afraid that it seems to me that you are looking for a fast way
to convert lots of data to a slow representation :-(.

Pretty much everything you can do with a univ in Mercury code will start
by checking whether the actual type in the univ is the same as the expected type.
This overhead is fine when univs are rare; it is not so fine if you propose to do it
on 50 Gb of data.

Of course, the performance cost won't matter if these checks keep failing
because you put the wrong typeinfo in a univ, and they will matter even less
if the typeinfo does not match the actual value, since that will lead to a crash.

Basically, you need some code that figures out, with 100% accuracy, the
type of each chunk of data you want to pass across the C-to-Mercury boundary.
I expect that this code will return a type from a limited set of types.
If that is the case, what you want to do is NOT to pair each value with
the typeinfo of its type and turn them into a univ, but to just wrap
an identifying function symbol around it. In other words, the output of your
code should not be a value of type univ, but a value of a type like this:

:- type r_to_mer
    --->    r2m_int(int)
    ;        r2m_float(float)
    ;        r2m_int_list(list(int))
    ;        r2m_string(string)
    ;        ....

Mercury code operating on values of this type would be a lot faster
than Mercury code doing equivalent operations on univs, because
a switch is much faster than a type check.

It would also be much simpler to implement, since all you have to do
would be export to C some Mercury functions that take as input
e.g. a float, and wrap r2m_float() around it.

There may be a chance that Richard O'Keefe has already looked
at data transfer between R and Mercury, and he was on this list
as of about a year ago. Richard, do you have any advice to share?

Zoltan.


More information about the users mailing list