# Programming Notes

## Implementation Specific Notes

### &acl;

### &lw;

### &cmucl;

## Foreign Object Representation and Access

There are two main approaches used to represent foreign objects: an integer that represents an address in memory, and a object that also includes run-time typing. The advantage of run-time typing is the system can dereference pointers and perform array access without those functions requiring a type at the cost of additional overhead to generate and store the run-time typing. The advantage of integer representation, at least for &acl;, is that the compiler can generate inline code to dereference pointers. Further, the overhead of the run-time type information is eliminated. The disadvantage is the program must then supply the type to the functions to dereference objects and array.

## Optimizing Code Using UFFI

### Background

Two implementions have different techniques to optimize (open-code) foreign objects. &acl; can open-code foreign object access if pointers are integers and the type of object is specified in the access function. Thus, &uffi; represents objects in &acl; as integers which don't have type information.

&cmucl; works best when keeping objects as typed objects. However, it's compiler can open-code object access when the object type is specified in `declare` commands and in `:type` specifiers in `defstruct` and `defclass`.

&lw;, in converse to &acl; and &cmucl; does not do any open coding of object access. &lw;, by default, maintains objects with run-time typing.

### Cross-Implementation Optimization

To fully optimize across platforms, both explicit type information must be passed to dereferencing of pointers and arrays. Though this optimization only helps with &acl;, &uffi; is designed to require this type information be passed the dereference functions. Second, declarations of type should be made in functions, structures, and classes where foreign objects will be help. This will optimize access for &lw;

Here is an example that should both methods being used for maximum cross-implementation optimization:

    (uffi:def-type the-struct-type-def the-struct-type)
    (let ((a-foreign-struct (allocate-foreign-object 'the-struct-type)))
      (declare 'the-struct-type-def a-foreign-struct)
      (get-slot-value a-foreign-struct 'the-struct-type 'field-name))