From afcce49455a243d4f8f2c37a4e5d7a5dfe2a964e Mon Sep 17 00:00:00 2001 From: Klaas Pieter Annema Date: Thu, 15 Jul 2010 12:55:22 +0200 Subject: [PATCH] improve CPString sizeOfString performance by caching the calculated result --- AppKit/CPStringDrawing.j | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/AppKit/CPStringDrawing.j b/AppKit/CPStringDrawing.j index 859c23926..fbd174592 100644 --- a/AppKit/CPStringDrawing.j +++ b/AppKit/CPStringDrawing.j @@ -24,6 +24,7 @@ @import "CPPlatformString.j" +var CPStringSizeWithFontInWidthCache = {}; @implementation CPString (CPStringDrawing) @@ -50,7 +51,17 @@ - (CGSize)sizeWithFont:(CPFont)aFont inWidth:(float)aWidth { - return [CPPlatformString sizeOfString:self withFont:aFont forWidth:aWidth]; + var cacheKey = self + [aFont cssString] + aWidth; + size = CPStringSizeWithFontInWidthCache[cacheKey]; + + if (size === undefined) + { + size = [CPPlatformString sizeOfString:self withFont:aFont forWidth:aWidth]; + CPStringSizeWithFontInWidthCache[cacheKey] = size; + } + + return size; } + @end