• 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: Aki Inoue <email@hidden>
  • Date: Mon, 23 Mar 2015 13:54:07 -0700

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


  • Follow-Ups:
    • Re: Memory optimization of NSAttributedString
      • From: Charles Jenkins <email@hidden>
References: 
 >Memory optimization of NSAttributedString (From: Charles Jenkins <email@hidden>)

  • Prev by Date: Re: Why is NSString.UTF8String unavailable in Swift?
  • Next by Date: Re: Swapping views in print panel
  • Previous by thread: Memory optimization of NSAttributedString
  • Next by thread: Re: Memory optimization of NSAttributedString
  • Index(es):
    • Date
    • Thread