Quarter Life Crisis

The world according to Sven-S. Porst

« WeekendMainComputer TV »

Cocoa Speed

374 words

This one is mainly a question to those people who know their ways in Cocoa and have some – hopefully more – experience with it than I do. The issue is the speed at which events of the mouse moving are received by an application.

More concretely: I want to have an application in which I can scribble things using my graphics tablet and then save what I've scribbled as a PDF file. Achieving that in principle is ridiculously easy in Cocoa and a matter of minutes rather than hours to generate an NSView subclass that translates mouse movements into NSBezierPaths. Unfortunately, performance seems to be a problem here:

My program doesn't receive sufficiently many events with the current mouse position to ensure a faithful representation of the mouse movement when the mouse i move rapidly. An example for this is when you're quickly drawing a circle and all that appear on screen is a jerky mess. This reduces the usabilit of the whole thing significantly on the one hand and is a bad thing as even an ugly Java application for the same task performs smoothly on the same machine.

The main reason for this seems to be the re-drawing of the paths. A little and completely unscientific test I made gave the following: when deactivating the screen refresh while drawing is in progress (i.e. not sending setNeedsDisplayInRect:), I'll get mouseDragged: messages every 7 to 20 ms. When the display is continuously updated that they are only sent every 50-80 ms. (All of this is quite irregular...)

How can this be improved? Is there a way to draw in another thread or so at a lower priority to ensure the maximum information about the mouse movement is retrieved? Can there be some way of cleverly caching the NSBezierPath to minimise the computations needed for updating the display while keeping the whole thing a vector graphics affair? As I haven't done any optimisations so far, I don't know answers to any of these questions. Hints are welcome.

For the next step, let me ask the following: Has anyone thought about what would be the best way to actually use curves in the bézier paths rather than just joining up points with straight lines?

September 27, 2003, 19:28

Add your comment

« WeekendMainComputer TV »

Comments on




This page

Out & About

pinboard Links


Received data seems to be invalid. The wanted file does probably not exist or the guys at last.fm changed something.