[maccode commit] r278 - Waving the mouse over side tabs is notably slow. Shark says this is due to string height c...

1 view
Skip to first unread message

codesite...@google.com

unread,
Mar 23, 2009, 4:15:18 PM3/23/09
to mac...@googlegroups.com
Author: catfish.man
Date: Mon Mar 23 13:12:12 2009
New Revision: 278

Modified:
trunk/Utilities/PSMTabBarControl/source/PSMAdiumTabStyle.m

Log:
Waving the mouse over side tabs is notably slow. Shark says this is due to
string height calculation, so this introduces a primitive (but effective
according to profiling) cache for string heights

Modified: trunk/Utilities/PSMTabBarControl/source/PSMAdiumTabStyle.m
==============================================================================
--- trunk/Utilities/PSMTabBarControl/source/PSMAdiumTabStyle.m (original)
+++ trunk/Utilities/PSMTabBarControl/source/PSMAdiumTabStyle.m Mon Mar 23
13:12:12 2009
@@ -411,31 +411,37 @@

- (float)heightOfAttributedString:(NSAttributedString *)inAttributedString
withWidth:(float)width
{
- NSTextStorage *textStorage;
- NSTextContainer *textContainer;
- NSLayoutManager *layoutManager;
- float height;
-
- //Setup the layout manager and text container
- textStorage = [[NSTextStorage alloc]
initWithAttributedString:inAttributedString];
- textContainer = [[NSTextContainer alloc]
initWithContainerSize:NSMakeSize(width, 1e7)];
- layoutManager = [[NSLayoutManager alloc] init];
-
- //Configure
- [textContainer setLineFragmentPadding:0.0];
- [layoutManager addTextContainer:textContainer];
- [textStorage addLayoutManager:layoutManager];
-
- //Force the layout manager to layout its text
- (void)[layoutManager glyphRangeForTextContainer:textContainer];
-
- height = [layoutManager
usedRectForTextContainer:textContainer].size.height;
-
- [textStorage release];
- [textContainer release];
- [layoutManager release];
-
- return height;
+ static NSMutableDictionary *cache;
+ if (!cache)
+ cache = [[NSMutableDictionary alloc] init];
+ if ([cache count] > 100) //100 items should be trivial in terms of memory
overhead, but sufficient
+ [cache removeAllObjects];
+ NSNumber *cachedHeight = [cache objectForKey:inAttributedString];
+ if (cachedHeight)
+ return [cachedHeight floatValue];
+ else {
+ NSTextStorage *textStorage = [[NSTextStorage alloc]
initWithAttributedString:inAttributedString];
+ NSTextContainer *textContainer = [[NSTextContainer alloc]
initWithContainerSize:NSMakeSize(width, 1e7)];
+ NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
+
+ //Configure
+ [textContainer setLineFragmentPadding:0.0];
+ [layoutManager addTextContainer:textContainer];
+ [textStorage addLayoutManager:layoutManager];
+
+ //Force the layout manager to layout its text
+ (void)[layoutManager glyphRangeForTextContainer:textContainer];
+
+ float height = [layoutManager
usedRectForTextContainer:textContainer].size.height;
+
+ [textStorage release];
+ [textContainer release];
+ [layoutManager release];
+
+ [cache setObject:[NSNumber numberWithFloat:height]
forKey:inAttributedString];
+
+ return height;
+ }
}

- (void)drawObjectCounterInCell:(PSMTabBarCell *)cell
withRect:(NSRect)myRect

Reply all
Reply to author
Forward
0 new messages