Re: NSView -drawRect optimization
Re: NSView -drawRect optimization
- Subject: Re: NSView -drawRect optimization
- From: Paul Bruneau <email@hidden>
- Date: Thu, 14 Feb 2008 09:13:53 -0500
On Feb 12, 2008, at 5:49 PM, Graham wrote:
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.
Thank you, Graham, for the very nice response. I am going to
implement code to be smarter about which rects need to be drawn per
your and others' suggestions. I also really like your idea about
reducing the tooltip rect setting.
_______________________________________________
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