GithubHelp home page GithubHelp logo

optcoll's Introduction

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.

optcoll's People

Contributors

lorentey avatar floriankugler avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.