Navigation:  Programming Cookbook > External Interfacing > External Arrays >

Arrays of Scalar Values

Previous pageReturn to chapter overviewNext page

The scalar value arrays represent single dimensioned arrays of native data types, generally numerics. They can be instantiated with buffer space reserved in Smalltalk memory with the class #new: message (the argument specifies the number of elements), or as a reference to external memory with the class #fromAddress:length: message. They can also be instantied directly from Smalltalk objects and collections using the class #with: and #withAll: messages, although the elements need to be of a type whose value can be stored in that type of external array.

For example:

DWORDArray withAll: (1 to: 1000)

 

DOUBLEArray

Represents single dimensioned arrays of double precision (64 bit) IEEE floats.

DWORDArray

Represents single dimensioned arrays of 32-bit unsigned integers.

FLOATArray

Represents single dimensioned arrays of single precision (32 bit) IEEE floats.

PointerArray

Represents single dimensioned arrays of 32-bit pointers to some other external type.

SDWORDArray

Represents single dimensioned arrays of 32-bit signed integers.

SWORDArray

Represents single dimensioned arrays of 16-bit signed integers.

WORDArray

 

It is worth noting that value array, in particular DOUBLEArrays and FLOATArrays, can consume considerably less memory than an equivalent Array of Smalltalk objects. This is because the overhead of allocating each as an individual object and providing it with header information can be as large or larger than the size of the object's value if the object is small. Garbage collector overhead is also reduced because a smaller the number of objects and references need be traversed.

Binary filing of ExternalArrays is also very fast and compact, for example:

| a |

a := (1.0 to: 1000.0) asArray.

a binaryStoreBytes size. "More objects means larger STB file: 16025"

Time millisecondsToRun: [a binaryStoreBytes]. "And longer to create it: 159"

Time microsecondsToRun: [100 timesRepeat: [a at: 1]]    "But access is fast:21"

 

Versus:

| a |

a := DOUBLEArray withAll: (1.0 to: 1000.0).

a binaryStoreBytes size.        "More compact: 8047"

Time millisecondsToRun: [a binaryStoreBytes].    " Very fast to store: 11"

Time microsecondsToRun: [100 timesRepeat: [a at: 1]]    " But slower to access: 115"

 

One can clearly see that ExternalArrays may also have a role outside external interfacing, especially in applications that store and process large quantities of floating point numbers.

Accessing the elements of an ExternalArray is, however, significantly slower, because there is a need to instantiate an object to wrap the value. This may be an issue when computations are implemented in Smalltalk, but the reverse is true when passing arrays of values to external DLLs for processing because the values will already be in a form which the external function can directly access.