beyond lists - functional kats conf dublin 2015

34
BEYOND LISTS Phillip Trelford, Functional Kats Conference, 2015 @ptrelford, #katsconf

Upload: phillip-trelford

Post on 12-Apr-2017

749 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Beyond Lists - Functional Kats Conf Dublin 2015

BEYOND LISTSPhillip Trelford, Functional Kats Conference, 2015@ptrelford, #katsconf

Page 2: Beyond Lists - Functional Kats Conf Dublin 2015
Page 3: Beyond Lists - Functional Kats Conf Dublin 2015

DATA-DRIVEN APPSLAMP Stack (State in DB) 3-Tier (State in memory & DB)

Page 5: Beyond Lists - Functional Kats Conf Dublin 2015

COMPUTER TIME IN ARBITRARY SECONDS

Source: http://blog.codinghorror.com/the-infinite-space-between-words/

Page 6: Beyond Lists - Functional Kats Conf Dublin 2015

PERFORMANCE IS A FEATURE[Google found that] the page with 10 results took 0.4 seconds to generate. The page with 30 results took 0.9 seconds. Half a second delay caused a 20% drop in traffic. Half a second delay killed user satisfaction.

In A/B tests, [Amazon] tried delaying the page in increments of 100 milliseconds and found that even very small delays would result in substantial and costly drops in revenue.

Source: http://blog.codinghorror.com/performance-is-a-feature/

Page 7: Beyond Lists - Functional Kats Conf Dublin 2015

LEFT-TRUNCATED PRIMES: PERFORMANCE

Source: http://trelford.com/blog/post/C2b2b-vs-C-vs-F-vs-Haskell.aspx

Page 8: Beyond Lists - Functional Kats Conf Dublin 2015

BEWARE THESE PRE- & -POST FIXES Micro Fast Simple Quick(*) Reactive Web Scalable Mongo

Framework Lite Light Domain Extensions Cache XML ORM

Page 9: Beyond Lists - Functional Kats Conf Dublin 2015

BEST PRACTICESPhillip Trelford, Functional Kats Conference, 2015@ptrelford, #katsconf

Page 10: Beyond Lists - Functional Kats Conf Dublin 2015

STRING BUILDERstring a = "abc";string b = "efg";string c = "hij";var d = a + b + c;

string a = "abc";string b = "efg";string c = "hij";var d = new StringBuilder(a);d.Append(b);d.Append(c);string e = d.ToString();

Page 11: Beyond Lists - Functional Kats Conf Dublin 2015

IMMUTABLE LISTSPhillip Trelford, Functional Kats Conference, 2015@ptrelford, #functionalkats

Page 12: Beyond Lists - Functional Kats Conf Dublin 2015

LIST TYPEtype 'a list = | Empty | Node of head:'a * tail:'a list

6 2 7 3 nil

Page 13: Beyond Lists - Functional Kats Conf Dublin 2015

HASKELL QUICKSORT(*)quicksort :: (Ord a) => [a] -> [a]  quicksort [] = []  quicksort (x:xs) =       let smallerSorted = quicksort [a | a <- xs, a <= x]          biggerSorted = quicksort [a | a <- xs, a > x]      in  smallerSorted ++ [x] ++ biggerSorted  

* well it’s short and a sort but it’s not quick!

Source: http://learnyouahaskell.com/recursion

Page 14: Beyond Lists - Functional Kats Conf Dublin 2015

“people are too puritanical about purity”

- Jon Harrop on Quora

Page 15: Beyond Lists - Functional Kats Conf Dublin 2015

LAZINESSPhillip Trelford, Functional Kats Conference, 2015@ptrelford, #katsconf

Page 16: Beyond Lists - Functional Kats Conf Dublin 2015

LAZY SEQUENCESHigher –order functions

Directory.GetFiles(path)

|> Seq.map FileInfo

|> Seq.map (fun x -> x.Name, x.Length)

Query expression

query {

for file in Directory.GetFiles(path) do

let info = FileInfo(file)

select (info.Name, info.Length)

}cSequence comprehensionseq {

for file in Directory.GetFiles(path) ->

let info = FileInfo(file)

info.Name, info.Length

}

Page 17: Beyond Lists - Functional Kats Conf Dublin 2015

CONVERT INTO LINQ-EXPRESSION

Page 18: Beyond Lists - Functional Kats Conf Dublin 2015

NESSOS LINQ OPTIMISERLinqOptimizer compiles declarative LINQ queries into fast loop-based imperative code. The compiled code has fewer virtual calls and heap allocations, better data locality and speedups of up to 15x

Page 19: Beyond Lists - Functional Kats Conf Dublin 2015

STREAMS

Page 20: Beyond Lists - Functional Kats Conf Dublin 2015

CLASH OF THE LAMBDAS

Page 21: Beyond Lists - Functional Kats Conf Dublin 2015

REACTIVE EXTENSIONS

Page 22: Beyond Lists - Functional Kats Conf Dublin 2015

SUM OF SQUARESReactive Extensions (C#) F# Observable module Nessos Streamslet rxValue = data .ToObservable() .Where(fun x -> x%2L = 0L) .Select(fun x -> x * x) .Sum() .ToEnumerable() |> Seq.head

// Real: 00:00:02.895, CPU: 00:00:02.843, GC gen0: 120, gen1: 0, gen2: 0

let obsValue = data |> Observable.ofSeq |> Observable.filter (fun x -> x%2L = 0L) |> Observable.map (fun x -> x * x) |> Observable.sum |> Observable.first

// Real: 00:00:00.479, CPU: 00:00:00.468, GC gen0: 18, gen1: 0, gen2: 0

let streamValue = data |> Stream.ofArray |> Stream.filter (fun x -> x%2L = 0L) |> Stream.map (fun x -> x * x) |> Stream.sum

// Real: 00:00:00.130, CPU: 00:00:00.109, GC gen0: 0, gen1: 0, gen2: 0

Page 23: Beyond Lists - Functional Kats Conf Dublin 2015

ASSUME NOTHING – PROFILE EVERYTHING Be scientific

Do test multiple implementations Don’t set out to confirm your bias Instrument and profile your code

Page 24: Beyond Lists - Functional Kats Conf Dublin 2015

LOOKUPSPhillip Trelford, Functional Kats Conference, 2015@ptrelford, #katsconf

Page 25: Beyond Lists - Functional Kats Conf Dublin 2015

SORTED DICTIONARY VS MAP VS ARRAY

Source: http://theburningmonk.com/benchmarks/

Page 26: Beyond Lists - Functional Kats Conf Dublin 2015

BRUTE FORCE WITH HYBRID DICTIONARYHybridDictionary attempts to optimize Hashtable. It implements a linked list and hash table data structure, switching over to the second from the first when the number of elements increases past a certain threshold.

https://www.simple-talk.com/blogs/2011/10/21/some-non-generic-collections/

Page 28: Beyond Lists - Functional Kats Conf Dublin 2015

FANCY DATA STRUCTURESPhillip Trelford, Functional Kats Conference, 2015@ptrelford, #katsconf

Page 29: Beyond Lists - Functional Kats Conf Dublin 2015

HAND ME THE ROPEString representation Insert Plan Length vs Median

Time

Source: http://www.ibm.com/developerworks/library/j-ropes/

Page 30: Beyond Lists - Functional Kats Conf Dublin 2015

UNROLLED LINKED LIST A linked-list of arrays

Page 31: Beyond Lists - Functional Kats Conf Dublin 2015

B-TREES A tree of arrays

Page 32: Beyond Lists - Functional Kats Conf Dublin 2015

SUMMING UPPhillip Trelford, Functional Kats Conference, 2015@ptrelford, #katsconf

Page 33: Beyond Lists - Functional Kats Conf Dublin 2015

SOME CONCLUSIONS Prefer safe and simple defaults - immutability Assume nothing – profile everything Mutable arrays are fast Build fast data structures from arrays Be pragmatic and they’ll be your friend C/C++ & Scala have extensive mutable data structure libraries

Page 34: Beyond Lists - Functional Kats Conf Dublin 2015

QUESTIONS? Twitter

@ptrelford Blog

http://trelford.com/blog F# Koans:

http://tinyurl.com/fsharpkoans