• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Memory optimization of NSAttributedString
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Memory optimization of NSAttributedString


  • Subject: Re: Memory optimization of NSAttributedString
  • From: Charles Jenkins <email@hidden>
  • Date: Wed, 25 Mar 2015 08:17:23 -0400

Thank you, Aki. It’s such an awesome and unexpected feature, I found it hard to believe my test results.


-- 

Charles

On March 23, 2015 at 16:54:11, Aki Inoue (email@hidden) wrote:

Hi Charles

Yes, NSAttributedString does unique attribute dictionaries.

Aki

> On Mar 22, 2015, at 5:39 AM, Charles Jenkins <email@hidden> wrote:
>
> My app uses lots of attributed strings in “subdocuments” which get moved into and out of a text edit window.
>
> I began to wonder if I needed to add a cache for paragraph styles and string attributes, because when each subdocument gets loaded, I’m repetitively deserializing identical attributes to apply to the strings.
>
> Today I wrote the first draft of a test to see what happens when you blindly and repetitively instantiate the same attributes:
>
> func attributedStringsWithNoCache() {
> var array = [NSAttributedString]()
> let fm = NSFontManager.sharedFontManager()
> for index in 1...30 {
> var font = NSFont( name:"Times", size:12.0 );
> font = fm.convertFont( font!, toHaveTrait:NSFontTraitMask.BoldFontMask )
> if ( index % 3 == 0 ) {
> font = fm.convertFont( font!, toHaveTrait:NSFontTraitMask.ItalicFontMask )
> }
> var para = NSMutableParagraphStyle();
> para.lineSpacing = 1.4
> para.alignment = NSTextAlignment.CenterTextAlignment;
> let attributes = [
> "StyleName":"Bold",
> NSFontAttributeName:font!,
> NSParagraphStyleAttributeName:para
> ];
> var attrStr = NSAttributedString( string:"Bold Para \(index)", attributes:attributes );
> array.append( attrStr );
> }
> for index in 1...30 {
> var font = NSFont( name:"Times", size:10.0 );
> var para = NSMutableParagraphStyle();
> para.lineSpacing = 1.2
> para.alignment = NSTextAlignment.LeftTextAlignment;
> let attributes = [
> "StyleName":"Roman",
> NSFontAttributeName:font!,
> NSParagraphStyleAttributeName:para
> ];
> var attrStr = NSAttributedString( string:"Roman Para \(index)", attributes:attributes );
> array.append( attrStr );
> }
> var fontDict = [NSFont:Int]()
> var attrDict = [NSDictionary:Int]()
> var paraDict = [NSParagraphStyle:Int]()
> for attrStr in array {
> attrStr.enumerateAttributesInRange(
> NSRange( location:0, length:attrStr.length ),
> options:NSAttributedStringEnumerationOptions.allZeros,
> usingBlock:{ ( attributes, Range, stop ) -> Void in
> attrDict[ attributes ] = 1
> if let para = attributes[ NSParagraphStyleAttributeName ] as? NSParagraphStyle {
> paraDict[ para ] = 1
> }
> if let font = attributes[ NSFontAttributeName ] as? NSFont {
> fontDict[ font ] = 1
> }
> })
> }
> println( "NO CACHE:\nFound \(fontDict.count) fonts and \(paraDict.count) paragraph styles in \(attrDict.count) attribute dictionaries" )
> }
>
> I expected the result to say it found 3 fonts and 60 paragraph styles in 60 attribute dictionaries. (I know the system uniquifies fonts.) But when I run the test, I get the best result possible—3 fonts and 2 paragraph styles in 3 attribute dictionaries—as if NSAttributedString already uniquifies everything.
>
> Is my test bad somehow, or am I really seeing that the text system already optimizes memory usage by uniquifying all attribute dictionaries, so I can forget about doing any caching of my own?
>
> —
>
> Charles
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


References: 
 >Memory optimization of NSAttributedString (From: Charles Jenkins <email@hidden>)
 >Re: Memory optimization of NSAttributedString (From: Aki Inoue <email@hidden>)

  • Prev by Date: Storyboard warning: internationalization of (null)
  • Next by Date: Display an Alert Sheet at a Specified Screen Position
  • Previous by thread: Re: Memory optimization of NSAttributedString
  • Next by thread: How do I prevent NSUserNotifications from being added to the notification list?
  • Index(es):
    • Date
    • Thread