Fixed: _CG and _CP macros were confusing and could degrade performance

Previously, Cappuccino was using preprocessor macros internally for the CGPoint/Size/Rect/Inset/Affine functions, as well as for CPRange. These macros had the same name as the corresponding function, but began with _. The functions were actually defined using the macros.

The motivation behind using macros was to increase performance by reducing function calls. However, there were a number of problems with this approach:

- There was an artificial dichotomy between _CG macros and the corresponding CG functions. We never completely replaced CG function calls with _CG macros. In fact, they were often mixed up in the same file. There was an extra burden on the programmer to remember to use the macro instead of the function.
- If a method call was passed as an argument to a macro, performance could actually be significantly *worse* than a function call. For example, _CGGetRectMakeCopy([view frame]) would expand to `{ origin:{ x:[view frame].origin.x, y:[view frame].origin.y }, size:{ width:[view frame].size.width, height:[view frame].size.height } }`. So instead of a single objj_msgSend and a single simple function call, we ended up with 4 objj_msgSend calls, which are way more expensive than simple function calls.
- Because of this expansion problem, to use macros efficiently required us to remember to use variables for all macro parameters. This didn't happen, and shouldn't have to happen.
- Finally, with modern Javascript engines, function call overhead is so small that it really isn't worth using the macros.

This commit eliminates the _CGGeometry, CGAffineTransformation and CPRange macros and replaces them with function calls.

BREAKING CHANGE:
The macros are no longer available. They could only be used with compiled code, but if there is any user code that used them, they will have to be replaced with the corresponding functions.
This commit is contained in:
Aparajita Fishman
2013-03-13 12:22:10 -04:00
parent 10b588a19c
commit afd5925499
74 changed files with 1276 additions and 1234 deletions
+34 -36
View File
@@ -20,8 +20,6 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#import "../Foundation/CPRange.h"
@import <Foundation/CPArray.j>
@import <Foundation/CPData.j>
@import <Foundation/CPIndexSet.j>
@@ -127,8 +125,8 @@ var HORIZONTAL_MARGIN = 2;
_maxNumberOfRows = 0;
_maxNumberOfColumns = 0;
_minItemSize = _CGSizeMakeZero();
_maxItemSize = _CGSizeMakeZero();
_minItemSize = CGSizeMakeZero();
_maxItemSize = CGSizeMakeZero();
[self setBackgroundColors:nil];
@@ -152,11 +150,11 @@ var HORIZONTAL_MARGIN = 2;
_numberOfColumns = CPNotFound;
_numberOfRows = CPNotFound;
_itemSize = _CGSizeMakeZero();
_itemSize = CGSizeMakeZero();
_selectionIndexes = [CPIndexSet indexSet];
_storedFrameSize = _CGSizeMakeZero();
_storedFrameSize = CGSizeMakeZero();
_needsMinMaxItemSizeUpdate = YES;
_uniformSubviewsResizing = NO;
@@ -504,7 +502,7 @@ var HORIZONTAL_MARGIN = 2;
if (!lazyFlag ||
_numberOfColumns !== oldNumberOfColumns ||
_numberOfRows !== oldNumberOfRows ||
!_CGSizeEqualToSize(_itemSize, oldItemSize))
!CGSizeEqualToSize(_itemSize, oldItemSize))
[self displayItems:_items frameSize:_storedFrameSize itemSize:_itemSize columns:_numberOfColumns rows:_numberOfRows count:count];
}
@@ -513,7 +511,7 @@ var HORIZONTAL_MARGIN = 2;
{
var width = aSuperviewSize.width,
height = aSuperviewSize.height,
itemSize = _CGSizeMakeCopy(_minItemSize),
itemSize = CGSizeMakeCopy(_minItemSize),
maxItemSizeWidth = _maxItemSize.width,
maxItemSizeHeight = _maxItemSize.height,
itemsCount = [_items count],
@@ -552,8 +550,8 @@ var HORIZONTAL_MARGIN = 2;
if (maxItemSizeHeight > 0)
itemSizeHeight = MIN(itemSizeHeight, maxItemSizeHeight);
_itemSize = _CGSizeMake(MAX(_minItemSize.width, itemSize.width), MAX(_minItemSize.height, itemSizeHeight));
_storedFrameSize = _CGSizeMake(MAX(width, _minItemSize.width), height);
_itemSize = CGSizeMake(MAX(_minItemSize.width, itemSize.width), MAX(_minItemSize.height, itemSizeHeight));
_storedFrameSize = CGSizeMake(MAX(width, _minItemSize.width), height);
_numberOfColumns = numberOfColumns;
_numberOfRows = numberOfRows;
countRef(MIN(itemsCount, numberOfColumns * numberOfRows));
@@ -574,7 +572,7 @@ var HORIZONTAL_MARGIN = 2;
if (idx >= displayCount)
{
[view setFrameOrigin:_CGPointMake(-anItemSize.width, -anItemSize.height)];
[view setFrameOrigin:CGPointMake(-anItemSize.width, -anItemSize.height)];
return;
}
@@ -584,7 +582,7 @@ var HORIZONTAL_MARGIN = 2;
y += _verticalMargin + anItemSize.height;
}
[view setFrameOrigin:_CGPointMake(x, y)];
[view setFrameOrigin:CGPointMake(x, y)];
[view setFrameSize:anItemSize];
x += anItemSize.width + _horizontalMargin;
@@ -687,12 +685,12 @@ var HORIZONTAL_MARGIN = 2;
if (aSize === nil || aSize === undefined)
[CPException raise:CPInvalidArgumentException reason:"Invalid value provided for minimum size"];
if (_CGSizeEqualToSize(_minItemSize, aSize))
if (CGSizeEqualToSize(_minItemSize, aSize))
return;
_minItemSize = _CGSizeMakeCopy(aSize);
_minItemSize = CGSizeMakeCopy(aSize);
if (_CGSizeEqualToSize(_minItemSize, _CGSizeMakeZero()))
if (CGSizeEqualToSize(_minItemSize, CGSizeMakeZero()))
_needsMinMaxItemSizeUpdate = YES;
[self tile];
@@ -712,10 +710,10 @@ var HORIZONTAL_MARGIN = 2;
*/
- (void)setMaxItemSize:(CGSize)aSize
{
if (_CGSizeEqualToSize(_maxItemSize, aSize))
if (CGSizeEqualToSize(_maxItemSize, aSize))
return;
_maxItemSize = _CGSizeMakeCopy(aSize);
_maxItemSize = CGSizeMakeCopy(aSize);
// if (_maxItemSize.width == 0 || _maxItemSize.height == 0)
// _needsMinMaxItemSizeUpdate = YES;
@@ -787,9 +785,9 @@ var HORIZONTAL_MARGIN = 2;
newSelectedRange = nil;
if (index < firstSelectedIndex)
newSelectedRange = _CPMakeRange(index, (firstSelectedIndex - index) + 1);
newSelectedRange = CPMakeRange(index, (firstSelectedIndex - index) + 1);
else
newSelectedRange = _CPMakeRange(firstSelectedIndex, (index - firstSelectedIndex) + 1);
newSelectedRange = CPMakeRange(firstSelectedIndex, (index - firstSelectedIndex) + 1);
indexes = [[self selectionIndexes] copy];
[indexes addIndexesInRange:newSelectedRange];
@@ -938,9 +936,9 @@ var HORIZONTAL_MARGIN = 2;
// Create and position the drop indicator view.
if (!_dropView)
_dropView = [[_CPCollectionViewDropIndicator alloc] initWithFrame:_CGRectMake(-8, -8, 0, 0)];
_dropView = [[_CPCollectionViewDropIndicator alloc] initWithFrame:CGRectMake(-8, -8, 0, 0)];
[_dropView setFrameSize:_CGSizeMake(10, _itemSize.height + _verticalMargin)];
[_dropView setFrameSize:CGSizeMake(10, _itemSize.height + _verticalMargin)];
[self addSubview:_dropView];
}
@@ -976,14 +974,14 @@ var HORIZONTAL_MARGIN = 2;
[[CPPasteboard pasteboardWithName:CPDragPboard] declareTypes:dragTypes owner:self];
var dragImageOffset = _CGSizeMakeZero(),
var dragImageOffset = CGSizeMakeZero(),
view = [self _draggingViewForItemsAtIndexes:_selectionIndexes withEvent:_mouseDownEvent offset:dragImageOffset];
[view setFrameSize:_itemSize];
[view setAlphaValue:0.7];
var dragLocation = [self convertPoint:locationInWindow fromView:nil],
dragPoint = _CGPointMake(dragLocation.x - _itemSize.width / 2 , dragLocation.y - _itemSize.height / 2);
dragPoint = CGPointMake(dragLocation.x - _itemSize.width / 2 , dragLocation.y - _itemSize.height / 2);
[self dragView:view
at:dragPoint
@@ -1115,12 +1113,12 @@ Not supported. Use -collectionView:dataForItemsAtIndexes:fortype:
_currentDragOperation = dragOperation;
var frameOrigin,
dropviewFrameWidth = _CGRectGetWidth([_dropView frame]);
dropviewFrameWidth = CGRectGetWidth([_dropView frame]);
if (_currentDropIndex == -1 || _currentDragOperation == CPDragOperationNone)
frameOrigin = _CGPointMake(-dropviewFrameWidth, 0);
frameOrigin = CGPointMake(-dropviewFrameWidth, 0);
else if (_currentDropIndex == 0)
frameOrigin = _CGPointMake(0, 0);
frameOrigin = CGPointMake(0, 0);
else
{
var offset;
@@ -1137,7 +1135,7 @@ Not supported. Use -collectionView:dataForItemsAtIndexes:fortype:
var rect = [self frameForItemAtIndex:dropIndex];
frameOrigin = _CGPointMake(_CGRectGetMaxX(rect) + offset, rect.origin.y - _verticalMargin);
frameOrigin = CGPointMake(CGRectGetMaxX(rect) + offset, rect.origin.y - _verticalMargin);
}
[_dropView setFrameOrigin:frameOrigin];
@@ -1184,8 +1182,8 @@ Not supported. Use -collectionView:dataForItemsAtIndexes:fortype:
- (void)drawRect:(CGRect)aRect
{
var context = [[CPGraphicsContext currentContext] graphicsPort],
width = _CGRectGetWidth(aRect),
circleRect = _CGRectMake(1, 1, width - 2, width - 2);
width = CGRectGetWidth(aRect),
circleRect = CGRectMake(1, 1, width - 2, width - 2);
CGContextSetStrokeColor(context, [CPColor colorWithHexString:@"4886ca"]);
CGContextSetFillColor(context, [CPColor whiteColor]);
@@ -1199,8 +1197,8 @@ Not supported. Use -collectionView:dataForItemsAtIndexes:fortype:
//then draw the line
CGContextBeginPath(context);
CGContextMoveToPoint(context, FLOOR(width / 2), _CGRectGetMinY(aRect) + width);
CGContextAddLineToPoint(context, FLOOR(width / 2), _CGRectGetHeight(aRect));
CGContextMoveToPoint(context, FLOOR(width / 2), CGRectGetMinY(aRect) + width);
CGContextAddLineToPoint(context, FLOOR(width / 2), CGRectGetHeight(aRect));
CGContextStrokePath(context);
}
@@ -1225,9 +1223,9 @@ Not supported. Use -collectionView:dataForItemsAtIndexes:fortype:
// if the direction is backward (-1) check with the bottom anchor
if (aDirection === -1)
[indexes addIndexesInRange:_CPMakeRange(anIndex, bottomAnchor - anIndex + 1)];
[indexes addIndexesInRange:CPMakeRange(anIndex, bottomAnchor - anIndex + 1)];
else
[indexes addIndexesInRange:_CPMakeRange(topAnchor, anIndex - topAnchor + 1)];
[indexes addIndexesInRange:CPMakeRange(topAnchor, anIndex - topAnchor + 1)];
}
else
indexes = [CPIndexSet indexSetWithIndex:anIndex];
@@ -1240,7 +1238,7 @@ Not supported. Use -collectionView:dataForItemsAtIndexes:fortype:
{
var frame = [self frameForItemsAtIndexes:[self selectionIndexes]];
if (!_CGRectIsNull(frame))
if (!CGRectIsEmpty(frame))
[self scrollRectToVisible:frame];
}
@@ -1388,10 +1386,10 @@ var CPCollectionViewMinItemSizeKey = @"CPCollectionViewMinItemSizeK
{
[super encodeWithCoder:aCoder];
if (!_CGSizeEqualToSize(_minItemSize, _CGSizeMakeZero()))
if (!CGSizeEqualToSize(_minItemSize, CGSizeMakeZero()))
[aCoder encodeSize:_minItemSize forKey:CPCollectionViewMinItemSizeKey];
if (!_CGSizeEqualToSize(_maxItemSize, _CGSizeMakeZero()))
if (!CGSizeEqualToSize(_maxItemSize, CGSizeMakeZero()))
[aCoder encodeSize:_maxItemSize forKey:CPCollectionViewMaxItemSizeKey];
[aCoder encodeInt:_maxNumberOfRows forKey:CPCollectionViewMaxNumberOfRowsKey];