Search code examples
ios6nsdictionaryuicollectionviewfast-enumerationuicollectionviewlayout

What's the fastest way to hash a very large dataset for UICollectionView Layout...NSIndexPath is too slow


I have a UICollectionViewController with a large dataset (>2000 items) with a custom layout. Using sections, the scrolling performance became extremely choppy. Using Instruments and a few tests, I determined this was due to lookup in the layout (layoutAttributesForElementsInRect: ). I cache layout attributes in prepareLayout, and look them up here like so, in the fastest way I know of:

[elementsInfo enumerateKeysAndObjectsUsingBlock:^(NSIndexPath *indexPath, UICollectionViewLayoutAttributes *attributes, BOOL *innerStop) {

   if (CGRectIntersectsRect(rect, attributes.frame)) [allAttributes addObject:attributes];

}];

I found that ~25% of cpu time was spent enumerating this, mostly on [NSIndexPath isEqual:]. So, I need a faster way to hash these values.

It must be possible, because I did a cross test using the same data with a sectioned UICollectionViewFlowLayout and it was smooth.


Solution

  • Well, turns out using arrays instead of dictionaries, and filtering by an NSPredicate was much faster since in this case the indices were already known.