Overview

cocoa-scalararrays

cocoa-scalararrays provides macros for creating NSMutableData backed arrays of C scalar values and structs. They provide similar, if at the moment more limited, facilities as NSMutableArray, and thanks to avoiding boxing values in objects, performace is for some cases superior and you get more concise code.

It's not a framework, just two files meant to be included in your Objective-C project.

Usage

// in suitable header file
SCALAR_ARRAY_INTERFACE_TYPE(double, Double);  
// in suitable .m file, with optional blocks for 
// equality, hash and comparison
SCALAR_ARRAY_IMPLEMENTATION_TYPE(double,
                                 Double,
                                 0.0, 
                                 ^(double v0, double v1) { return (BOOL)(v0 == v1); },
                                 ^(double v) { return (NSUInteger)v ^ 71; },
                                 ^(const void *v0, const void *v1) {
                                     const double dv0 = *(const double*)v0;
                                     const double dv1 = *(const double*)v1;
                                     return dv0 < dv1 ? -1 : ((dv0 > dv1) ? 1 : 0);
                                 });

// using it
ScalarDoubleArray *da = [[[ScalarDoubleArray alloc] init] autorelease];
[da addValue:1.0];

For more, see main.m and ScalarArray.h.

It includes a few types as an example — ScalarDoubleArray, ScalarNSIntegerArray, ScalarCGFloatArray and ScalarCGRectArray — but you're free to define whatever you need.

Performance

You win some performance by not boxing your scalars in NSNumbers etc. If you run the application in the project, it'll measure the performance of storing doubles in a NSMutableArray and in ScalarDoubleArray and output the results.

Here's an example run as an OS X app. First column is name of the test, second column is size of the collection, third column is the average time it took with cocoa-scalararrays and fourth is the time it took with NSMutableArray.

Test times in seconds averaged over 10 repetitions
Test       Size    Scalar   NSMutableArray
add        100     0.000006 0.000047
add        10000   0.001056 0.004821
add        100000  0.006179 0.040167

insert     100     0.000018 0.000035
insert     10000   0.055701 0.003316

rndacc sum 100     0.000004 0.000009
rndacc sum 10000   0.000248 0.000454
rndacc sum 100000  0.002541 0.004692

enum sum   100     0.000007 0.000007
enum sum   10000   0.000118 0.000386
enum sum   100000  0.001186 0.004019

(add == add values to the end, insert == insert values to the beginning, rndacc sum == loop and calculate sum using random access, enum sum == loop and calculate using fast enumeration/block enumeration)

Here are numbers on iPhone 4/iOS 4.3.3:

Test times in seconds averaged over 10 repetitions
Test       Size    Scalar   NSMutableArray
add        100     0.000047 0.000550
add        10000   0.003763 0.056248
add        100000  0.037019 0.545349

insert     100     0.000102 0.000505
insert     10000   0.145675 0.052059

rndacc sum 100     0.000043 0.000048
rndacc sum 10000   0.003315 0.004320
rndacc sum 100000  0.035233 0.050505

enum sum   100     0.000033 0.000034
enum sum   10000   0.002293 0.002669
enum sum   100000  0.025059 0.033552

The 1000000 item insertion test wasn't done because it's just too slow with cocoa-scalararrays. That's one of those "don't do it then" scenarios.

Requirements

The code assumes you can use blocks. It should work in fine with or without garbage collection.

Licensing

cocoa-scalararrays is licensed under the terms of Apache Licence 2.0.