• Stars
    star
    233
  • Rank 172,230 (Top 4 %)
  • Language
    Swift
  • License
    MIT License
  • Created almost 8 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Home of my talk about Optimizing Collections in Swift

Optimizing Collections in Swift

Book Cover

This repository contains the sample code from the book Optimizing Collections.

In this book, we show how to write very efficient Swift collection code. Throughout the book, we benchmark everything — with some surprising results. We implement custom data structures with value semantics and copy-on-write behavior such as sorted arrays, binary trees, red-black trees, and B-trees.

Even if you never implement your own collections, this book helps you reason about the performance of Swift code.

The Code

This repository includes the full source code of every algorithm discussed in the book:

  1. MyOrderedSet: A rudimentary Swift wrapper for the NSOrderedSet class in Foundation.
  2. SortedArray: A simple collection storing elements in a sorted array, with O(n) insertions.
  3. AlgebraicTree: A purely functional implementation of red-black trees using algebraic data types.
  4. COWTree: A procedural, structs-and-classes variant of red-black trees that implements the copy-on-write optimization.
  5. BTree: An implementation of in-memory B-trees, based on my BTree package.

As a bonus, all five data structures implement BidirectionalCollection, and they all have full value semantics.

Note that while this repository contains nice illustrations of various coding techniques, I don't recommend you use any of this code in your own projects. I had to cut some corners to make sure the code remains relatively easy to understand; full implementations would include lot more functionality that would just obfuscate my point here. For example, none of these collections implement support for removing elements, or in fact any of method in SetAlgebra other than contains and insert.

For production-ready implementations of ordered collections, please check out my BTree package instead.

The Video

You can watch the video of my talk at dotSwift 2017 about optimizing collections by clicking on the image below.

dotSwift 2017 - Optimizing Swift Collections

My slides are available on Speaker Deck.

The App

The custom microbenchmarking app I wrote to generate my charts is called Attabench, and it is available in a separate repository. The insertion benchmark I demonstrated is included in the app by default, so you can easily reproduce my results on your own computer.

Attabench screenshot

Tip: try implementing your own optimization ideas, and race them with my code! I'm sure you'll easily beat me if you try.

More Repositories

1

app-architecture

Sample Code of the App Architecture Book
Swift
2,021
star
2

functional-swift

Issue repository for the Functional Swift book
Swift
927
star
3

issue-13-viper

Objective-C
846
star
4

core-data

Sample code for the objc.io Core Data book
Swift
769
star
5

issue-1-lighter-view-controllers

Example project for the 1st issue of objc.io
Objective-C
740
star
6

articles

All current objc.io articles
593
star
7

markdown-playgrounds

A Markdown Editor that can execute Swift code
Swift
575
star
8

thinking-in-swiftui-sample-code

Sample code for the Thinking in SwiftUI book
Swift
318
star
9

swift-talk-backend

Swift
287
star
10

tiny-networking

Tiny Networking Library
Swift
257
star
11

issue-21-core-image-explorer

Sample code for the Introduction to Core Image article in objc.io issue #21
Swift
216
star
12

issue-3-collection-view-layouts

Objective-C
194
star
13

issue-4-full-core-data-application

Objective-C
164
star
14

issue5-view-controller-transitions

Simple demo app showing custom view controller transitions
Objective-C
124
star
15

attributed-string-builder

Attributed String Builders
Swift
108
star
16

issue-2-background-core-data

Objective-C
89
star
17

issue-10-core-data-network-application

Sample project for objc.io article http://www.objc.io/issue-10/networked-core-data-application.html
Objective-C
88
star
18

issue-7-lab-color-space-explorer

Objective-C
74
star
19

issue-12-custom-layer-property-animations

Issue 12: Custom Layer Property Animations
Objective-C
66
star
20

issue-3-auto-layout-debugging

Objective-C
62
star
21

issue-14-xpc

Sample Project for objc.o Article about XPC
Swift
55
star
22

LedgerGUI

Swift
55
star
23

issue-12-interactive-animations-pop

Objective-C
50
star
24

S01E51-playground-driven-development-at-kickstarter

Sample code for Swift Talk episode 51: Playground-Driven Development at Kickstarter
Swift
48
star
25

S01E01-networking

Sample code for Swift Talk episode 1: Networking
Swift
46
star
26

issue-12-interactive-animations-uidynamics

Interactive Animations with UIDynamics
Objective-C
45
star
27

swift-talk-app-swiftui

Swift Talk App using SwiftUI
Swift
45
star
28

swiftui-challenges

SwiftUI challenges
41
star
29

issue-12-interactive-animations

Interactive Animations on iOS
Objective-C
41
star
30

issue-2-background-networking

Objective-C
40
star
31

issue-4-importing-and-fetching

Objective-C
39
star
32

keychain-item

Keychain Item Property Wrapper
Swift
38
star
33

StaticSite

Simple and blunt static site generator
Swift
37
star
34

issue-2-background-file-io

Objective-C
34
star
35

metal-demo-objcio

Sample code for Issue #18 of objc.io
Objective-C
33
star
36

issue-7-contact-editor

Objective-C
30
star
37

issue-8-quadcopter-navigator

Objective-C
30
star
38

S01E34-reactive-programming

Sample code for Swift Talk episode 34: Reactive Programming
Swift
25
star
39

issue-14-scriptable-apps

Objective-C
24
star
40

thinking-in-swiftui-code

Accompanying code for the book Thinking in SwiftUI
24
star
41

S01E03-loading-view-controllers

Sample code for Swift Talk episode 3
Swift
23
star
42

advanced-swift

Feedback and issue-tracker for objc.io's Advanced Swift book
23
star
43

S01E53-test-driven-reactive-programming-at-kickstarer

Sample code for Swift Talk episode 53: Test-Driven Reactive Programming at Kickstarter
Swift
21
star
44

S01E90-concurrent-map

Sample code for Swift Talk episode 90: Concurrent Map
Swift
19
star
45

S01E25-network-caching

Sample code for Swift Talk episode 25: Network Caching
Swift
18
star
46

issue-7-linguistic-tagging

Objective-C
18
star
47

S01E05-connecting-view-controllers

Sample code for Swift Talk Episode #5: Connecting View Controllers
Swift
17
star
48

S01E06-generic-table-view-controllers

Sample code for Swift Talk Episode #6: Generic Table View Controllers
Swift
16
star
49

issue-6-compiler-tool

Example code for a standalone clang/llvm tool.
C++
16
star
50

S01E26-generic-table-view-controllers-part-2

Sample code for Swift Talk episode 26: Generic Table View Controllers (Part 2)
Swift
15
star
51

collection-view-swiftui

Sample code for Swift Talk episode 168: SwiftUI — Building a Collection View (Part 2)
Swift
15
star
52

thinking-in-swiftui-qa

15
star
53

issue-16-functional-apis

Example playground for the article http://www.objc.io/issue16/functional-swift-apis.html
Swift
13
star
54

s01e47-testable-view-models-at-kickstarter

Sample code for Swift Talk episode 47: View Models at Kickstarter
Swift
12
star
55

swift-talk-shared

Swift
11
star
56

issue-12-interactive-animations-osx

Objective-C
11
star
57

S01E57-certificate-pinning

Sample code for Swift Talk episode 57: Certificate Pinning
Swift
10
star
58

S01E43-view-controller-refactoring

Swift
10
star
59

S01E02-rendering-commonmark

Sample code for Swift Talk episode 2: Rendering CommonMark
Swift
10
star
60

S01E62-testable-view-controllers-with-reducers

Sample code for Swift Talk episode 62: Testable View Controllers with Reducers
Swift
10
star
61

S01E145-setting-up-a-document-based-app

Sample code for Swift Talk episode 145: Markdown Playgrounds — Setting Up a Document Based App
Swift
10
star
62

issue-9-string-parsing

Objective-C
9
star
63

S01E75-auto-layout-with-key-paths

Sample code for Swift Talk episode 75: Auto Layout with Key Paths
Swift
9
star
64

S01E49-deeplinking-at-kickstarter

Swift
8
star
65

S01E133-tiny-networking-library-revisited

Sample code for Swift Talk episode 133: Networking: Tiny Networking Library Revisited
Swift
8
star
66

S01E174-animation-curves

Sample code for Swift Talk episode 174: SwiftUI — Animation Curves
Swift
7
star
67

S01E07-stack-views-with-enums

Sample code for Swift Talk episode #7: Stack Views with Enums
Swift
7
star
68

issue-14-plugins

Objective-C
7
star
69

S01E117-showing-hiding-sections

Sample code for Swift Talk episode 117: Building a Form Library: Showing & Hiding Sections
Swift
7
star
70

S01E08-networking-post-requests

Sample code for Swift Talk episode 8: Networking: POST Requests
Swift
7
star
71

S01E27-typed-notifications-part-1

Sample code for Swift Talk episode 27: Typed Notifications (Part 1)
Swift
6
star
72

DynamicLayout

Swift
6
star
73

S01E109-ios-remote-debugger-connecting-with-bonjour

Sample code for Swift Talk episode 109: iOS Remote Debugger: Connecting with Bonjour
Swift
6
star
74

S01E253-flow-layout-revisited

Sample code for Swift Talk 253: SwiftUI – Flow Layout Revisited
Swift
6
star
75

S01E225-swiftui-layout-explained-view-protocols-and-shapes

Sample code for Swift Talk 225: SwiftUI Layout Explained – View Protocols and Shapes
Swift
6
star
76

S01E333-sticky-headers-for-scroll-views

Sample code for Swift Talk 333: Sticky Headers for Scroll Views
Swift
6
star
77

S01E73-view-bindings-in-pure-swift

Sample code for Swift Talk episode 73: View Bindings in Pure Swift
Swift
6
star
78

S01E71-type-safe-file-paths-with-phantom-types

Sample code for Swift Talk episode 71: Type-Safe File Paths with Phantom Types
Swift
5
star
79

S01E66-the-elm-architecture-part-1

Sample code for Swift Talk episode 66: The Elm Architecture (Part 1)
Swift
5
star
80

S01E45-server-side-swift-routing

Swift
5
star
81

S01E28-typed-notifications-part-2

Sample code for Swift Talk episode 28: Typed Notifications (Part 2)
Swift
5
star
82

retro-rampage

Swift
5
star
83

S01E235-swiftui-layout-explained-layout-priorities

Sample code for Swift Talk 235: SwiftUI Layout Explained – Layout Priorities
Swift
5
star
84

S01E19-from-runtime-programming-to-functions

Sample code for Swift Talk episode 19: From Runtime Programming to Functions
Swift
5
star
85

S01E04-rendering-commonmark-part-2

Swift Talk Episode 4: Rendering CommonMark (Part 2)
Swift
5
star
86

S01E175-building-a-shopping-cart-animation

Sample code for Swift Talk episode 175: SwiftUI — Building a Shopping Cart Animation
Swift
5
star
87

libpq

Swift
4
star
88

S01E29-protocols-class-hierarchies

Sample code for Swift Talk episode 29: Protocols & Class Hierarchies
Swift
4
star
89

S01E156-a-first-look-at-swiftui

Sample code for Swift Talk episode 156: SwiftUI — A First Look
Swift
4
star
90

S01E149-swift-syntax-highlighting

Sample code for Swift Talk episode 149: Markdown Playgrounds — Swift Syntax Highlighting
Swift
4
star
91

S01E179-building-a-shopping-cart-cleanup-and-refactoring

Sample code for Swift Talk episode 179: SwiftUI — Building a Shopping Cart — Cleanup & Refactoring
Swift
4
star
92

S01E229-swiftui-layout-explained-flexible-frames

Sample code for Swift Talk 229: SwiftUI Layout Explained – Flexible Frames
Swift
4
star
93

S01E86-sharing-state-between-view-controllers-in-mvc-part-1

Sample code for Swift Talk episode 86: Sharing State Between View Controllers in MVC (Part 1)
Swift
4
star
94

S01E168-building-a-collection-view-part-2

Sample code for Swift Talk episode 168: SwiftUI — Building a Collection View (Part 2)
Swift
4
star
95

S01E406-detecting-visible-cells

Sample code for Swift Talk 406: Detecting Visible Cells
Swift
4
star
96

S01E36-futures

Sample Code for Swift Talk Episode 36: Futures
Swift
4
star
97

S01E211-quick-open-simple-fuzzy-matching

Sample code for Swift Talk 211: Quick Open — Simple Fuzzy Matching
Swift
4
star
98

S01E183-lap-times

Sample code for Swift Talk episode 183: SwiftUI Stopwatch — Lap Times
Swift
3
star
99

S01E137-testing-networking-code

Sample code for Swift Talk episode 136: Networking — Testing Networking Code
Swift
3
star
100

S01E63-mutable-shared-structs-part-2

Sample code for Swift Talk episode 63: Mutable Shared Structs (Part 2)
Swift
3
star