Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is byte alignment (cache line alignment) for Core Animation? Why it matters?

I am loading images on a scroll view in a non-lazy way, so that the stutter behavior is not seen. The code works and the FPS is close to 60.

BUT, I do not understand what is byte alignment (or cache line alignment) for Core Animation?

As mentioned here and here this is an important thing to do. However, I noticed as long as I do the steps mentioned here, byte-alignment or not does not really matter.

Anyone knows what exactly it is?

like image 879
skyahead Avatar asked Jan 10 '23 13:01

skyahead


1 Answers

When the CPU copies something from memory into the CPU cache it does so in chunks. Those chunks are cache lines and they are of a fixed size. When data is stored in the CPU cache, it's store as lines. Making your data fit into the cache line size for your target architecture can be important for performance because it affects data locality.

ARMv7 uses 32 byte cache lines (like PowerPC). The A9 processor uses 64 byte cache lines. Because of this, you will see the most benefit by rendering into a rectangle that is on a 64 byte boundary and has dimensions that are a multiple of 64 bytes.

On the other hand, the graphics accelerator does prefer working with image data that is a square power of two in dimensions. This doesn't have anything to do with cache lines or byte alignment. This is another thing that can have a large impact on performance.

In the specific cases you linked to, the Apple API being called (Core Animation, QT, etc). is performing these kinds of optimizations on the caller's behalf. In the case of CoreAnimation, the caller is giving it data that it is optimizing for the hardware. According to what Path wrote in the documentation you linked to, they suggest giving Core Animation data it will not have to optimize (in this case, optimizing and making a copy) to avoid the optimization step.

So if your images are some multiple of 64 bytes in dimension and each dimension is a square power of two, you're good to go ;) Rendering that image into an area of the screen that is on a 64 byte boundary is also good, but is not always realistic for anything but a full screen application like a game.

That said, use Instruments. Build your application, profile it with Instruments and a representative workload (UIAutomation is great for this). If you see scrolling performance problems Instruments will give you everything you need to zero in on where the bottleneck is.

I can honestly say that all of the scrolling performance problems I have seen have not involved byte alignment or cache lines. Instead it's been other forms of Core Animation abuse (not using rasterization and caching), or doing too much other work on the main thread, etc.

The guidance on the effect of byte alignment on performance is mentioned in the Quartz 2D Programming Guide

This is the format that Core Animation is optimizing images to when it does a copy. If you already have your data in the format Core Animation wants, it will skip the potentially expensive optimization step.

If you want to know more about how the iOS graphics pipeline works, see:

WWDC 2012 Session 238 "iOS App Performance: Graphics and Animations"

WWDC 2012 Session 235 "iOS App Performance: Responsiveness"

WWDC 2011 Session 121 "Understanding UIKit Rendering"

iOS Device Compatibility Reference: OpenGL ES Graphics

like image 101
quellish Avatar answered Jan 19 '23 11:01

quellish