Re: [Pyobjc-dev] conversion of tuples to structs
Brought to you by:
ronaldoussoren
From: Ronald O. <ous...@ci...> - 2003-09-13 06:19:53
|
On 12 sep 2003, at 22:39, b.bum wrote: > On Sep 11, 2003, at 5:36 PM, Tobias Sargeant wrote: >> I appreciate that the symmetry of the pythonifying/depythonifying is >> neat, but it would be extremely useful (for example) to be able to >> pass >> Numeric arrays representing points to objc functions without the >> overhead >> (both in terms of execution speed and in terms of typing) of >> converting >> them to tuples. > > (You'll need to do a cvs update to grab a version of PyObjC that can > support what is described below) > > Over Christmas, I did some graphics performance tests and found that > the fastest possible way to get points across the bridge and onto the > screen was through the use of something like.... > > points = array.array('f', [0.0,0.0,1.0,1.0] * colorIter) > > That is, allocate points as an array of type float, initialized to > contain the sequence 0.0,0.0,1.0,1.0 repeated colorIter times. Then, > update the contents as needed and invoke.... > > NSRectFillList(points, colorIter) > > ... to draw. This is possible because NSRectFillList() is bridged in > a fashion that accepts both tuples and the array.array() construct. > The array.array() construct is very fast mostly because it can be > passed directly to NSRectFillList() without any conversion. > > Except that the current version of the bridge breaks this with the > comment: > > /* Upto PyObjC 1.0b2 compat_NSRectFillList was the wrapper for > NSRectFillList > * this implementation is not consistant w.r.t. the rest of the > bridge and > * therefore depricated. > */ > > Which is really unfortunate because the new version has to allocate a > chunk of memory, then loop through every tuple and turn it into an > array of floats -- this really adds up when pumping through 100s of > thousands of rectangles. Given that 1x1 rectangles are the most > efficient way of pushing non-anti-aliased points or lines to the > screen, this situation is common in graphic intensive Cocoa apps. > > Some numbers: I wrote a little app that implements the Hopalong > algorithm to draw lots of pretty dots on the screen. Using the Tuple > based conversion, I see.... > [ that the array.array version is about 6x faster ] > > The same carries through to the rest of the APIs that take (NSPoint > **) or (NSRect **) type arrays of points/rects/etc... This change in speed seems a good reason to support array.array or Numberic arrays to represent arrays of structs. The best way to do this would be adding a (pair of) function(s) to the core bridge and pyobjc-api.h, and use those in the wrapper modules. Something like: PyObject* pyRects; int rectCount; NSRect* rectArray; // set pyRects and rectCount rectArray = PyObjC_MakeObjCArray(@encode(NSRect), pyRects, rectCount); // use rectArray PyObjC_FreeObjCArray(rectArray); Ronald |