Re: NSView -drawRect optimization
Re: NSView -drawRect optimization
- Subject: Re: NSView -drawRect optimization
- From: Graham <email@hidden>
- Date: Wed, 13 Feb 2008 09:49:10 +1100
Hi Paul,
I think there's plenty you can do to optimize drawing here.
I haven't analysed your code to the last line, but it looks as if you
are drawing every rect regardless. The fastest drawing is none at
all, so the first thing would be to only draw what needs to be drawn.
You can check this in a variety of ways. The simplest is to check if
the rect to be drawn intersects the <rect> parameter to -drawRect: -
if not, skip it. (Use the NSIntersectsRect utility).
For this to help, you also only need to call for a refresh only of
those rects that have actually changed when you drag one. Instead of
calling a -setNeedsDisplay: on the view, call setNeedDisplayInrect:
for each rect that moves. (This part of the code you haven't posted
so you might be doing this already).
The simple intersection test might not gain you all that much if a
lot of rects move at a time, since Cocoa will form the union of all
those rects and pass that to -drawRect:, resulting in refreshing more
than necessary. So the next step is to test your rects using -
needsToDrawRect: which is more fine-grained and will eliminate all
rects that haven't changed at all from being drawn.
If setting the tooltip rects is slow, don't do it unless you have to.
The tooltip rects for all static rects doesn't need to change at all,
and for the rest only when the mouse is released, so maybe you could
move this step to the mouseUp: method. Keep track of the rects that
you changed when dragging, then update this list's tooltips on mouse
up. Keep this step out of the drawing loop altogether. I think this
will work since presumably tooltips don't need to be displayed while
dragging, only when you hover over an item - so the stale tooltip
rects during a drag won't affect you.
These steps should give you a dramatic boost in performance. If not,
we can think again.
-------
S.O.S.
On 13/02/2008, at 9:02 AM, Paul Bruneau wrote:
Hi-
I have just deployed my first Cocoa app which is a scheduling
program used by one user in my company. I'm very proud of it as a
first app but I want to make it a little better.
It has a subclass of NSView where I draw all of the "orderSteps"
which are the pieces of work that are required to complete all the
orders on our shop floor. Here is a screenshot of the view in its
window:
http://special-lite.com/satellite/scheduleWindow.png
The user clicks and drags all those little rectangles around in
order to optimize the production. They "bump" each other out of the
way. They limit each others' motion when there is no more room to
bump, etc. There are 226 rectangles on the screenshot, but during
busy times there might be 500-700.
My only concern with it is the frames per second draw speed of the
view (it's all done in -drawRect). I have done pretty good testing
to remove individual segments of code from the loop in order to see
what is taking the time:
- As developed, it draws at about 5.9 frames per second on my 2.66
ghz Mac Pro (user is on a 24" iMac, almost as fast)
- Removing the orderStep draw loop completely gives me about 25 fps
Here is my loop that draws each orderStep on the view. This loop
gets called once for each workCenter that you see on the left side
of the view.
http://objc.pastebin.com/m4fe1a694
In the code are comments that tell how many fps were gained for
each fragment of code that I commented out. All the fragments do
not add up to the 19fps difference so I can assume that my math is
horrible or that the addition of each fragment makes other things
take longer in some way. One thing I know for sure is that -
addToolTipRect takes forever!
If anyone likes looking at newbie code for stupid things, now is
your chance. I'll happily take any tips, comments, or even
derision. My biggest fear is to hear "There's nothing you can do
about it--stuff takes time" to which I would respond, "Then how
does Blizzard do it!? I know they are drawing more than 226
rectangles!"
Thank you
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
40ictinternational.com.au
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden