Hi, thanks I tried the new FFI and it is definitely faster.
Out of curiosity why do the stacks need to be seperate? Would it not be possible to have a ForeignUnsafe for which they share the same stack? Thanks
On Sat, Oct 10, 2015 at 1:39 PM, David Matthews < David.Matthews at prolingua.co.uk> wrote:
I was planning to announce the new foreign function interface on the list but Artella got in there before me.
The old foreign function interface, CInterface, was designed more than twenty years. That was before libffi and the standard basis library and in the days of the old persistent store. I've done some work on it, in particular adapting it to libffi and adding some extra features such as callback functions, but the basic design and code haven't really changed. I decided it was time to update the design and rather than adapt CInterface it was easiest to start from scratch.
The new design, in the Foreign structure, provides very similar high level functions to CInterface. In fact it should be a simple process to convert function calls over. I haven't used the old names because they began with capital letters and there is a convention now that initial capitals are reserved for constructors.
The lower levels are very different. In particular C addresses and the conversion between C and ML values are handled entirely by ML code rather than the run-time system. A consequence of this is that there is no garbage collection of C data; all data structures allocated in C memory must be explicitly freed. The "converters" in Foreign, such as cString, that allocate space to pass values, automatically deallocate the space when the function returns. When a function is passed as an argument the function closure is freed when the calling function returns. Since that isn't always what is required, for example when registering a callback for use later, there is an easy way to override this.
The intention is that this should be much more efficient than CInterface. Nevertheless, there is still a significant overhead in calling a foreign function compared with one in ML. To allow for garbage collection each thread has separate stacks for ML and C code and there is at the very least the need to switch between them. I haven't produced any documentation yet but I've adapted the old example code and produced a new example in mlsource/extra/CInterface/Examples/NewForeignTest.sml.
David
polyml mailing list polyml at inf.ed.ac.uk http://lists.inf.ed.ac.uk/mailman/listinfo/polyml