Slide 1

Slide 1 text

DifferenceKit in Action try! Swift Tokyo 2020 Meetup!! Jan 2020

Slide 2

Slide 2 text

Ryo Aoyama CyberAgent, Inc. CATS productivity team Twitter: @ra1028fe5 GitHub: @ra1028 OSS: DifferenceKit, Carbon, swift-mod, etc…

Slide 3

Slide 3 text

DifferenceKit Ver: 1.1.5 Star: 2.3k Fork: 133 A fast and flexible O(n) difference algorithm framework for Swift collection.

Slide 4

Slide 4 text

Works on many production app LINE Mercari Uber CyberAgent Yahoo Japan PayPay dely NIKKEI and more… eureka

Slide 5

Slide 5 text

Why we use diffing algorithm • For performance • For beautiful animation • For declarative programming • For business logic segregation

Slide 6

Slide 6 text

Ordered N-items List UI

Slide 7

Slide 7 text

When insert an item to the list, reloadDate() will calculates for all items. It’s not efficient Reload

Slide 8

Slide 8 text

UI calculations are very slow and will force the main thread. Reload Slow Slow Slow Slow

Slide 9

Slide 9 text

Improving list UI performance by updating only the differences. Reload Slow

Slide 10

Slide 10 text

performBatchUpdates can partially update items with animations let deleteIndexPaths: [IndexPath] let insertIndexPaths: [IndexPath] let moveIndexPaths: (from: IndexPath, to: IndexPath) let reloadIndexPaths: [IndexPath] tableView.performBatchUpdates({ tableView.deleteRows(at: deleteIndexPaths, with: .automatic) tableView.insertRows(at: insertIndexPaths, with: .automatic) tableView.moveRow(at: moveIndexPaths.from, to: tableView.reloadRows(at: reloadIndexPaths, with: .automatic) })

Slide 11

Slide 11 text

Now, do you know which item was inserted?

Slide 12

Slide 12 text

How can I programmatically know how the item array requested to the network has changed?

Slide 13

Slide 13 text

Diffing algorithm Calculates the diffs before and after updating the array. Wiki:

Slide 14

Slide 14 text

DifferenceKit let source: [Item] let target: [Item] let changeset = StagedChangeset(source: source, target: target) tableView.reload(using: changeset, with: .automatic) { data in = data }

Slide 15

Slide 15 text

Paul Heckel’s Algorithm DifferenceKit uses… Paper: A technique for isolating differences between files With O(N) complexity, linear function time can be expected regardless of data size and changes. Ideal for UI calculations, IMO.

Slide 16

Slide 16 text

Paul Heckel’s Algorithm DifferenceKit uses… Optimizations in terms of + • Algorithm • Swift lang

Slide 17

Slide 17 text

Major Diffing Algorithms Author Myers Wu Heckel complexity O(ND) O(NP) O(N) insert ✅ ✅ ✅ delete ✅ ✅ ✅ move ⚠ ⚠ ✅ update ❌ ❌ ✅ shortest? Yes Yes No

Slide 18

Slide 18 text

Swift Library Comparisons Disclaimer: These DO NOT compete for the “Best” of the library.

Slide 19

Slide 19 text

Library Algorithms

Slide 20

Slide 20 text

Performance Comparison • 3x slower • 20x slower • 7x slower • 18x slower • 4500x slower • 5400x slower • 140x slower From 100,000 elements to 10,000 deleted, 10,000 inserted and 2,000 shuffled BLAZING FAST !

Slide 21

Slide 21 text

Functionality Comparison Supported Collection

Slide 22

Slide 22 text

Functionality Comparison Supported Element Diff

Slide 23

Slide 23 text

Functionality Comparison Supported Section Diff

Slide 24

Slide 24 text

DifferenceKit in Future

Slide 25

Slide 25 text

DiffableDataSource is pretty solid solution for diffing in list UI items. But, DifferenceKit is more loosely-coupled, white-boxed and for any usage (mainly UI).

Slide 26

Slide 26 text

SwiftUI New generation, personally, I hope SwiftUI will eliminate the DifferenceKit. Currently, SwiftUI.List has no customizability. We should use UITableView and UICollectionView as before. DifferenceKit will be useful for a while.

Slide 27

Slide 27 text

Thanks Twitter: @ra1028fe5 GitHub: @ra1028 Ryo Aoyama