Skip to content

Python Interoperability

Vihan edited this page May 18, 2019 · 5 revisions

Python Interoperability

Python interoperability is in its infancy primarily being a series of classes with dynamic behavior which suits python. The difficulty with implementing interop with an untyped language is it does not blend effectively with the safe nature of VSL required for the performance it aims to achieve. For this purpose Python objects can be used natively in VSL as first class constructs however interactions still retain similar overhead to complex Objective-C objects in that a complex mesh is needed in order to effectively blend the VSL and Python memory management.

Compiler Support

Whether the backend should natively support custom/external ref count engines for dynamic objects is still being considered but given the significant complexity and non-determinism it would introduce to the memory optimizer, it would significantly complicate the shortest weighted lifetime analysis that VSL uses in order to make runtime decisions on dynamically confirming the lifetime of certain objects. With external, dynamic, reference counts, VSL is not able to effectively weight external access of references and therefore, to avoid early cleanup, will either defer to full reference counting (which has magnifying effects throughout a program) or to deferred cleanup.

Python

There is a VSL Python module in development which allows for the following:

const np = Python.main.importModule("numpy")

const nums1 = np["array"]([1, 2, 3])
const nums2 = npArray + 2

print(nums2) // array([2, 3, 4])

const vslArray: Int[] = nums2.toArray(elements: .int)

This requires a standard python environment (configurable using PythonDispatch) and is generally configurable for embedded environments. This mostly abandons type safety however since runtime errors will be thrown if attempting to access a non-existent element.