Skip to content

Instantly share code, notes, and snippets.

@boredzo
Created November 14, 2011 06:57
Show Gist options
  • Save boredzo/1363410 to your computer and use it in GitHub Desktop.
Save boredzo/1363410 to your computer and use it in GitHub Desktop.
New CPU Usage object cache mechanism
New CPU Usage cache
Currently: Parallel C arrays of Cocoa text system objects, colors, etc.
drawRect: looks up the objects it needs in those arrays, indexed by the CPU usage percentage, and creates and caches whatever it doesn't already have.
Problem: drawRect: has a slow path where the cache does not contain the needed objects yet, so it must create them. Even the check itself slows down every pass through drawRect:.
Solution: A single array containing blocks.
Initially, this would be filled with all the same block, each of which creates the needed objects, creates a block that captures those objects and does the drawing, and replaces itself in the cache with the new block.
drawRect: simply calls the appropriate block in the array, unconditionally. The slow-path block will move itself out of the way when it's no longer needed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment