iOS CoreText Slowing Down Plot Render on First Draw?

Mar 10, 2014 at 6:24 PM
Just pulled down the latest where CoreText is now being used for the text...everything looks great, but it feels like we lost a good deal of speed while rendering the plots. I have a noticeable lag from launching the application to first render.

Hooking up Instruments, I can dig down and see all of the time is being spent in CoreText inside OxyPlot.

This is noticeable on the simulator, and becomes in your face on the device.

In the example OxyPlot program check out the 'LineSeries' examples. Pick a plot, any plot, and you'll have a bit of a pause, and then the graph draws. After this, pick any other LineSeries and you won't have the lag, and it draws fine. Same thing happens on device and in the simulator. Using Instruments to profile I see the same results, a lot of time spent down in CoreText.

If you kill the application, start it up again, and pick a different plot you'll see the same results, long pause, draw plot, and then any other plot after that will draw fine.

Thoughts?
Coordinator
Mar 10, 2014 at 9:24 PM
Thanks for the report! I did not notice this on my device, but I have only been testing simple examples.
I think we need to avoid disposing CTLine, NSAttributedString and CTFont objects all the time. And maybe the fonts can be cached? I think the UIFont.Fromname call should also be removed.

ps. is the text aligned correctly horizontally? to me it looked a little bit off.
Mar 10, 2014 at 11:33 PM
The title text did seem off center by a bit...yes.
Mar 20, 2014 at 2:09 AM
I went back to Instruments with my app, it appears most of the time is spent in
public override OxySize MeasureText (string text, string fontFamily, double fontSize, double fontWeight)
specifically
var attributedString = new NSAttributedString (text, new CTStringAttributes {
                ForegroundColorFromContext = true,
                Font = new CTFont (fontFamily, (float)fontSize)
            });
The time killer is the new CTFont (fontFamily, (float)fontSize)

Does anyone have a preferred method for caching the possible different font sizes/font families?
Mar 20, 2014 at 2:53 PM
I played around with caching CTFont and did not get the performance benefit I thought I might. After a little more digging I turned up http://stackoverflow.com/questions/6062420/core-text-performance.

tldr
The more attributes a string has the longer it takes to draw.

I've since changed the code to:
var attributedString = new NSAttributedString (text, new CTStringAttributes {
//              ForegroundColorFromContext = true,
//              Font = new CTFont (fontFamily, (float)fontSize)
            });
Commenting out the color and font greatly improves performance, I'd say it is as fast as it was before moving to CoreText.
Apr 5, 2014 at 3:35 PM
Latest release has put this issue to bed...plots no longer lag on first draw!

Nice work!
Marked as answer by benhysell on 4/5/2014 at 7:40 AM
Apr 5, 2014 at 3:37 PM