You will go to a great start. You have profiled the code, isolated the actual bottleneck, and are now focused on how to solve it.
First question: which distribution is expensive? Obviously, you should focus on this first.
There are several effective ways to work with sparse arrays. First, consider NSPointerArray , which is designed to hold NULL values. It does not promise to be effective for sparse arrays, but @bbum (who knows such things) suggests that it is .
Next, take a look at NSHashMap , which is certainly efficient for sparse collections (this is a dictionary) and supports non-object keys (i.e. you don't need to create NSNumber
).
Finally, if distribution is really your problem, there are various tricks for it to work. The most common is the reuse of objects, rather than the destruction of one and the creation of another. Here's how UITableViewCell
(and NSCell
works differently).
Finally, if you switch to Core Foundation objects, you can create your own custom memory allocator, but it really is a last resort.
Note that 10.6 supports ARC (without nulling weak links). ARC dramatically improves performance over many common memory management patterns. For example, the very common βsave + abstract + returnβ scheme is highly optimized in ARC. ("Save" does not exist in the language in ARC, but it still exists in the compiler, and ARC is much faster than manual execution). I highly recommend switching to ARC in any code you can.
source share