Dcpyfosqs4gggkqvcjm9
SkillsCast

Composable Caching in Swift

30th March 2017 in London at CodeNode

There are 33 other SkillsCasts available from iOSCon 2017 - The conference for iOS and Swift Developers

Please log in to watch this conference skillscast.

626836374 640

Consider what it means to be a cache. You need to be able to

(1) associate a key with a value and

(2) get some value given a key if such a value exists.

That's basically it. Caches tend to appear in layers. In a CPU, memory reads check L1, then L2, then L3, then RAM. When you want to load an image, you first check RAM, then disk, and finally network.

In a mobile app, if you don't nail your cache code, your users will suffer. Excessive networking causes both battery and data-plan drain. You can help ensure a clean correct implementation by combining caches. Given two caches A and B, A on-top-of B means first check A, fallthrough to B, then write back to A. Now you can define a monoid for caches. Monoids imply easy composition. Easy composition means reasoning about our code becomes easy.

Such an abstraction isn't possible to express both statically and generically in languages like Objective-C and Java, but you can with Swift's strong type system. The caching library Carlos provides the foundation upon which you can build prefetching and other useful transformations. Adopting such a system simplified our codebase: Caches became reusable legos. But the complexity doesn't just all go away, it's just hidden. When an abstraction is so nice, it's tempting to think of the actual caches and cache glue as black-boxes. There was once a reference cycle inside the library that led to large bitmaps leaking and phones running out of memory. It's important to actually think about the implementation details in detail, since these are your building blocks for the rest of the system. When your building blocks are stable, your building is stable.

Caching and prefetching are necessary for mobile apps. In this talk, Brandon will share with you how to think about caches as monoids, how a monoidal caching system can simplify our jobs as software engineers, and what real-world problems you ran into when putting such a system into production.

YOU MAY ALSO LIKE:

Thanks to our sponsors

Composable Caching in Swift

Brandon Kase

"Brandon Kase brings typed functional programming to weird places. He has shipped production code on Android with Kotlin, iOS with Swift and React Native, and Web with JS/Flow/React. Brandon is an iOS core-experience engineer at Pinterest who came across functional programming while pursuing a B.S. in Computer Science from Carnegie Mellon University. Brandon is excited that strong static typing and functional programming are becoming mainstream, and believes that techniques once reserved for academia will help industry produce more reliable software. He is in general fascinated by anything and everything related to well-designed programming languages and libraries.