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. Optimizing
    Swift Collections
    Károly Lőrentey
    @lorentey

    View full-size slide

  2. NSOrderedSet

    View full-size slide

  3. 1 4 8 9 11 12 14 16 17

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  6. 9
    1 8 11 16
    5 12
    4 14 17
    left right

    View full-size slide

  7. x
    a b
    y
    c d
    Empty Tree Black node Red node

    View full-size slide

  8. .empty .node(color, a, x, b)
    x
    a b
    y
    c d

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  15. 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

    View full-size slide

  16. 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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    12ʹ
    t.insert(10)

    View full-size slide

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

    t.insert(10)

    View full-size slide

  26. 9
    12
    11
    5
    8
    1
    4
    16
    14 17
    t
    10
    11ʹ
    12ʹ

    t.insert(10)

    View full-size slide

  27. 12
    11
    5
    8
    1
    4
    16
    14 17
    t
    10
    11ʹ
    12ʹ

    t.insert(10)

    View full-size slide

  28. 11
    5
    8
    1
    4
    16
    14 17
    t
    10
    11ʹ
    12ʹ

    t.insert(10)

    View full-size slide

  29. 5
    8
    1
    4
    16
    14 17
    t
    10
    11ʹ
    12ʹ

    t.insert(10)

    View full-size slide

  30. 5
    8
    1
    4
    16
    14 17
    t
    10
    11ʹ
    12ʹ

    t.insert(10)

    View full-size slide

  31. 5
    8
    1
    4
    16
    14 17
    t
    10
    11ʹ
    12ʹ

    3 allocations
    3 deallocations
    10 atomic refCount ops
    t.insert(10)

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  37. COWTree
    root:
    COWNode
    color:
    value: 10
    left:
    right:
    COWNode COWNode

    View full-size slide

  38. O(n)
    Linear Time

    View full-size slide

  39. O(1)
    Constant Time
    O(n)
    Linear Time

    View full-size slide

  40. O(1)
    Constant Time
    O(n)
    Linear Time
    4096

    View full-size slide

  41. 4096 × 8 bytes

    View full-size slide

  42. 4096 × 8 bytes
    = 32 kB

    View full-size slide

  43. 4096 × 8 bytes
    = 32 kB
    = size of L1 cache

    View full-size slide

  44. 1 2 3 4
    5 4 7

    View full-size slide

  45. 1 2 3 4
    5 4 7
    8
    9

    View full-size slide

  46. 1 2 3 4
    5 4 7
    8
    9

    View full-size slide

  47. 1 2 3 4
    5 4 7
    8
    9

    View full-size slide

  48. 1 2 3 4
    5 4 7
    8
    9
    10

    View full-size slide

  49. 1 2 3 4
    5 4 7
    8
    9
    10 11
    12

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  55. 9
    c
    1 2 3 4
    b 4 7
    8
    1 1
    1 5a

    View full-size slide

  56. 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

    View full-size slide

  57. 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

    View full-size slide

  58. 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

    View full-size slide

  59. 3.5x
    4.5x
    2.5x

    View full-size slide

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

    View full-size slide

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

    Functional programming 

    with algebraic data types
    λ

    View full-size slide

  62. 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
    λ

    View full-size slide

  63. 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
    λ

    View full-size slide

  64. Reuse an existing Collection

    View full-size slide

  65. import BTree

    View full-size slide

  66. import BTree

    View full-size slide

  67. 22m 40s
    SortedArray NSOrderedSet AlgebraicTree COWTree BTree/1024
    Time spent inserting 3 million random values

    View full-size slide

  68. 22m 40s
    SortedArray NSOrderedSet AlgebraicTree COWTree BTree/1024
    1.8s
    7s
    22s
    14m 54s
    22m 36s
    Time spent inserting 3 million random values

    View full-size slide

  69. Thank You!
    lorentey

    View full-size slide

  70. Thank You!
    lorentey @lorentey

    View full-size slide

  71. Acknowledgements
    Cow picture courtesy of
    freegreatpicture.com
    Puppy pictures courtesy of
    Floppy the Beagle

    View full-size slide