Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Optimizing Swift Collections

Optimizing Swift Collections

In this talk, I describe several ways to implement the same simple ordered set protocol in Swift, demonstrating how the language supports a number of surprisingly different approaches to programming. At every step, we trade extra complexity for improved runtime performance, ending on an implementation that is ludicrously fast but also quite difficult to handle.

Presented on dotSwift 2017 in January 2017.

The talk has a repository on GitHub, with full source code and further details:

https://github.com/lorentey/OptimizingCollections

Károly Lőrentey

January 27, 2017
Tweet

More Decks by Károly Lőrentey

Other Decks in Programming

Transcript

  1. 40x

  2. 40x

  3. 9 1 8 11 16 5 12 4 14 17

    4 2 3 5 1 8 9 a b 6 7
  4. 9 1 8 11 16 5 12 4 14 17

    4 2 3 5 1 8 9 a b 6 7
  5. z x y a b c d x z y

    a b c d z y x a b c d x y z a b c d
  6. x z y a b c d z x y

    a b c d x z y a b c d z y x a b c d x y z a b c d
  7. 9 11 12 5 8 1 4 16 14 17

    t t.insert(10)
  8. 9 11 12 5 8 1 4 16 14 17

    t 10 t.insert(10)
  9. 9 11 12 5 8 1 4 16 14 17

    t 10 t.insert(10)
  10. 9 11 12 5 8 1 4 16 14 17

    t 10 t.insert(10)
  11. 9 11 12 5 8 1 4 16 14 17

    t 10 t.insert(10)
  12. 9 11 12 5 8 1 4 16 14 17

    t 10 11ʹ t.insert(10)
  13. 9 11 12 5 8 1 4 16 14 17

    t 10 11ʹ 12ʹ t.insert(10)
  14. 9 11 12 5 8 1 4 16 14 17

    t 10 11ʹ t.inserting(10) 9ʹ 12ʹ t.insert(10)
  15. 9 12 11 5 8 1 4 16 14 17

    t 10 11ʹ 12ʹ t.inserting(10) 9ʹ t.insert(10)
  16. 9 12 11 5 8 1 4 16 14 17

    t 10 11ʹ 12ʹ 9ʹ t.insert(10)
  17. 12 11 5 8 1 4 16 14 17 t

    10 11ʹ 12ʹ 9ʹ t.insert(10)
  18. 11 5 8 1 4 16 14 17 t 10

    11ʹ 12ʹ 9ʹ t.insert(10)
  19. 5 8 1 4 16 14 17 t 10 11ʹ

    12ʹ 9ʹ t.insert(10)
  20. 5 8 1 4 16 14 17 t 10 11ʹ

    12ʹ 9ʹ t.insert(10)
  21. 5 8 1 4 16 14 17 t 10 11ʹ

    12ʹ 9ʹ 3 allocations 3 deallocations 10 atomic refCount ops t.insert(10)
  22. 9 11 12 5 8 1 4 16 14 17

    t.insert(10) t
  23. 9 11 12 5 8 1 4 16 14 17

    t.insert(10) 10 t
  24. 9 11 12 5 8 1 4 16 14 17

    t.insert(10) 10 t 10
  25. 9 11 12 5 8 1 4 16 14 17

    t.insert(10) 10 1 allocation No deallocations No atomic refCount ops t 10
  26. 9 11 12 5 8 1 4 16 14 17

    t.insert(10) 10 1 allocation No deallocations No atomic refCount ops t 10
  27. COW

  28. !

  29. 1 2 3 4 b 4 7 8 9 10

    11 12 5 a c
  30. 1 2 3 4 b 4 7 8 9 10

    11 12 5 a c
  31. 1 2 3 4 b 4 7 8 9 10

    11 12 5 a c
  32. 1 2 3 4 b 4 7 8 9 10

    11 12 5 a c
  33. 1 2 3 4 b 4 7 8 9 10

    11 12 5 a c
  34. 9 c d e f g h i 1 2

    3 4 b 4 7 8 1 1 1 5a 34 7 8 1 2 b 4 1 2 b 4 1 3 47 8 1 2 b 4 1 2 b 4 1 2 Z 4 1 j
  35. 9 c d e f g h i 1 2

    3 4 b 4 7 8 1 1 1 5a 34 7 8 1 2 b 4 1 2 b 4 1 3 47 8 1 2 b 4 1 2 b 4 1 2 Z 4 1 j
  36. 9 c d e f g h i 1 2

    3 4 b 4 7 8 1 1 1 5a 34 7 8 1 2 b 4 1 2 b 4 1 3 47 8 1 2 b 4 1 2 b 4 1 2 Z 4 1 j B-tree
  37. 1 2 34 54 7 8 9 1 2 34

    54 7 8 9 Reuse an existing Collection ♻
  38. 1 2 34 54 7 8 9 1 2 34

    54 7 8 9 Reuse an existing Collection ♻ Functional programming 
 with algebraic data types λ
  39. 1 2 34 54 7 8 9 1 2 34

    54 7 8 9 Reuse an existing Collection ♻ Imperative rewrite with
 COW optimization # Functional programming 
 with algebraic data types λ
  40. 9 c de f gh i j 1 2 34

    54 7 8 9 1 2 34 54 7 8 9 Reuse an existing Collection ♻ Cache-aware data structures and unsafe operations ⚙ Imperative rewrite with
 COW optimization # Functional programming 
 with algebraic data types λ
  41. 22m 40s SortedArray NSOrderedSet AlgebraicTree COWTree BTree/1024 1.8s 7s 22s

    14m 54s 22m 36s Time spent inserting 3 million random values