It’s all in the predicate

Although I might have ordered a new zippy MacBook – my old PowerBook is still proving its use: highlighting pieces of code that are just too slow.

In the iOnTV UI application the schedule grid (list of what’s on) is a critical part of the main window. I noticed that as the database of program information got larger scrolling performance in the schedule grid (going backwards/forwards in time) was terribly slow on my PowerBook.

A little time with Shark showed that I was spending 80% my time in the ‘fetch schedules on a station between two times’ method. This method used a simple predicate to fetch only those schedule details fulfilling the arguments :

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"((station == %@) AND (time >= %@) AND (time <= %@)) OR ((station == %@) AND (time < %@) AND (endTime > %@))", self, startDate, endDate, self, startDate, startDate];

A little thinking about it and I realized that the logic was overly complex and could be simplified :

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(station == %@) AND (((time >= %@) AND (time <= %@)) OR ((endTime >= %@) AND (endTime <= %@)))", self, startDate, endDate, startDate, endDate];

I don’t need to compare against the station more than once – start with everything on this station, and then consider only those things that start between the two times or end between the two times.

Result ? The fetch is now less than 10% of the CPU usage when changing the start time in the schedule grid ! Much better ! I still have a couple more optimizations to make – but watch those predicates !

Posted under Development, iOnTV

This post was written by awk on February 26, 2008

Leave a Comment

Name (required)

Email (required)

Website

Comments

More Blog Post

Previose Post: