Optimizing Application Architecture (.NET/Java topics)

57
OPTIMIZING YOUR APPLICATION ARCHITECTURE Ravi Okade lides from Philly.NET code camp June 2014

Transcript of Optimizing Application Architecture (.NET/Java topics)

Page 1: Optimizing Application Architecture (.NET/Java topics)

OPTIMIZING YOUR APPLICATION

ARCHITECTURERavi Okade

Slides from Philly.NET code camp June 2014

Page 2: Optimizing Application Architecture (.NET/Java topics)

All architecture is design but not all design is architecture. Architecture represents the significant design decisions that shape a system, where significant is measured by cost of change.

Source: wikiquote.org

Grady Booch

Page 3: Optimizing Application Architecture (.NET/Java topics)

Intro

We will talk about ideas to optimize your application architecture.

We will talk about some theory and some practice! Hope you will like it

Page 4: Optimizing Application Architecture (.NET/Java topics)

Source: wikiquote.org

Donald Knuth

If you find that you're spending almost all your time on theory, start turning some attention to practical things; it will improve your theories. If you find that you're spending almost all your time on practice, start turning some attention to theoretical things; it will improve your practice.

Page 5: Optimizing Application Architecture (.NET/Java topics)

Agenda: (Re)Visiting the programming 101 Data Structures and Algorithms Serialization Synchronization Other stuff (Rx, functional and so

on)

Page 6: Optimizing Application Architecture (.NET/Java topics)

Data Structures and Algorithms

Page 7: Optimizing Application Architecture (.NET/Java topics)

Do you know ..

What sorting algorithm is used by Array.Sort ?

Describe internals of List<T>. Compare and contrast with LinkedList<T>

Page 8: Optimizing Application Architecture (.NET/Java topics)

Array.Sort

Source: http://msdn.microsoft.com/en-us/library/6tf1f0bc.aspx

Page 9: Optimizing Application Architecture (.NET/Java topics)

Quicksort honored as one of top 10 algorithms of 20th centuryin science and engineering.

Click icon to add picture

Source: Wikipedia.org

Quicksort invented by Tony Hoare (1960)

Page 10: Optimizing Application Architecture (.NET/Java topics)

Why does it matter ?

Robert Sedgwick, Princeton University http://www.cs.princeton.edu/~rs/

Page 11: Optimizing Application Architecture (.NET/Java topics)

Lists

List<T> in .NET is highly optimized You typically never have to use anything

else Initial size makes a difference

Page 12: Optimizing Application Architecture (.NET/Java topics)

Consider an array!

Arrays are written using native code Much faster than all other data

structures

Page 13: Optimizing Application Architecture (.NET/Java topics)

Interesting Lists - Skiplist

A Skip list is a data structure that allows fast search within an ordered sequence of elements. Fast search is made possible by maintaining a linked hierarchy of subsequences, each skipping over fewer elements

From Wikipedia - http://en.wikipedia.org/wiki/Skip_list

Page 14: Optimizing Application Architecture (.NET/Java topics)

Skiplist - searching example

From Scott Mitchell’s article in MSDN “An Extensive Examination of Data Structures Using C# 2.0”

Page 15: Optimizing Application Architecture (.NET/Java topics)

.NET Specialized Collections

ListDictionary Implements IDictionary using a singly linked list. Recommended for collections that typically include fewer than 10 items.

HybridDictionary Implements IDictionary by using a ListDictionary while the collection is small, and then switching to a Hashtable when the collection gets large.

NameValueCollection Represents a collection of associated String keys and String values that can be accessed either with the key or with the index.

StringDictionary Implements a hash table with the key and the value strongly typed to be strings rather than objects.Many more – see: System.Collections.Specialized Namespace

Page 16: Optimizing Application Architecture (.NET/Java topics)

No Guarantees! Question everything

https://www.youtube.com/watch?v=YQs6IC-vgmo#t=6

Page 17: Optimizing Application Architecture (.NET/Java topics)
Page 18: Optimizing Application Architecture (.NET/Java topics)
Page 19: Optimizing Application Architecture (.NET/Java topics)

Serialization

Page 20: Optimizing Application Architecture (.NET/Java topics)

Serialization is a solved problem! BinaryFormatter NetDataContractSerializer BinaryNetDataContractSerializer DataContractJsonSerializer XmlSerializer BinaryXmlSerializer DataContractSerializer BinaryDataContractSerializer

Page 21: Optimizing Application Architecture (.NET/Java topics)

Or is it ?

Page 22: Optimizing Application Architecture (.NET/Java topics)

Serialization matters..

Data exchange across clear boundaries works very well with xml/json.

But Lot of data exchange is still internal. Examples:Data Services for Thick clientsStoring data in a cacheDistributed computing, cloud storage

Page 23: Optimizing Application Architecture (.NET/Java topics)

Popular protocols

Protocol Buffer (Protobuf)Created 2001; Opensourced 2008Designed and used extensively by Google

Apache ThriftCreated in 2007Used extensively in Facebook

Apache Avro Created by LinkedIn and open sourced in

2011

See a good overview of different protocols hereSee performance comparison of various formats here

Page 24: Optimizing Application Architecture (.NET/Java topics)

So many ways to serialize!Serialization time for various protocols

https://code.google.com/p/thrift-protobuf-compare/wiki/Benchmarking

Page 25: Optimizing Application Architecture (.NET/Java topics)

ProtoBuf.NET Size comparison

prot

obuf

-net

Binar

yFor

mat

ter

SoapF

orm

atte

r

XmlS

eria

lizer

DataC

ontra

ctSer

ializ

er

DataC

ontra

ctJs

onSer

ializ

er

5

251

928

170 212

32

Page 26: Optimizing Application Architecture (.NET/Java topics)

ProtoBuf.NET Time comparison

prot

obuf

-net

Binary

Form

atte

r

SoapF

orm

atte

r

XmlS

erial

izer

DataC

ontra

ctSer

ialize

r

DataC

ontra

ctJs

onSer

ialize

r

Page 27: Optimizing Application Architecture (.NET/Java topics)

ProtoBuf.NET Performance

Serializer Size Serialization Time

Deserialization Time

Protobuf-net 5 842 3,535

BinaryFormatter 251 11,618 15,178

SoapFormatter 928 47,179 79,218

XmlSerializer 170 15,373 22,216

DataContractSerializer 212 3,778 11,455

DataContractJsonSerializer 32 4,129 18,728

https://code.google.com/p/protobuf-net/wiki/Performance

Page 28: Optimizing Application Architecture (.NET/Java topics)

ProtoBuf.NET Attributes

From ProtoBuf.Net Quickstart (Download from SVN here code). This example project is in QuickStart folder

Page 30: Optimizing Application Architecture (.NET/Java topics)

ProtoBuf schema

package QuickStart;

message Contact { optional string Name = 1; optional string ContactDetails = 2;}message Customer { optional string CustomerId = 1; optional string Name = 2; optional double MaximumOrderAmount = 3; repeated Contact Contacts = 4;}

Page 31: Optimizing Application Architecture (.NET/Java topics)

ProtoBuf.NET Serialization without attributes

Page 32: Optimizing Application Architecture (.NET/Java topics)

Protobuf Demo

Serialize/Deserialize example in .NET Protobuf Schema Using data serialized using .NET in Java

Page 33: Optimizing Application Architecture (.NET/Java topics)

Avro Comparison Dynamic typing: Avro does not require that code be

generated. Data is always accompanied by a schema that permits full processing of that data without code generation, static datatypes, etc.

Untagged data: Since the schema is present when data is read, considerably less type information need be encoded with data, resulting in smaller serialization size.

Schema evolution:  Avro requires schemas when data is written or read. Most interesting is that you can use different schemas for serialization and deserialization, and Avro will handle the missing/extra/modified fields.

Page 34: Optimizing Application Architecture (.NET/Java topics)

Avro Demo

Using Avro Schema and GenericRecord (Both in .NET and Java)

Comparing the output sizes of various Codecs (Java sample)

Serialize in Java and consume in .NET and vice-versa.

Page 35: Optimizing Application Architecture (.NET/Java topics)

Simplicity does not precede complexity, but follows it.

Alan J. Perlis (1922 – 1990)

First recipient of the Turing Award.

Page 36: Optimizing Application Architecture (.NET/Java topics)

If you have a procedure with 10 parameters, you probably missed some.

Alan J. Perlis

Page 37: Optimizing Application Architecture (.NET/Java topics)

SynchronizationAvoiding and Eliminating

Page 38: Optimizing Application Architecture (.NET/Java topics)

Avoid using locks. Simply say "no" to locks. Locks slow programs, reduce their scalability, and are the source of bugs in parallel programs.

James Reinders (Intel)From Rules for Parallel Programming for Multicore

Page 39: Optimizing Application Architecture (.NET/Java topics)

Reduced lock and lock-less collections ConcurrentDictionary Immutable Collections in .NET 4.5

Page 40: Optimizing Application Architecture (.NET/Java topics)

ConcurrentDictionary

Available in .NET 4.0 Needs adjustment to your code

Page 41: Optimizing Application Architecture (.NET/Java topics)

Immutable Collections in .NET 4.5 System.Collections.Immutable namespace

ImmutableDictionary<TKey, TValue>ImmutableSortedDictionary<TKey, TValue>ImmutableHashSet<T>ImmutableList<T>ImmutableQueue<T>ImmutableSortedSet<T>ImmutableStack<T>

Needs adjustment. See this Also immutable collections are slower.

Benchmark! Also see this nice series of blogposts by Eric Lippert

Page 42: Optimizing Application Architecture (.NET/Java topics)

Immutable<T> Demo

Page 43: Optimizing Application Architecture (.NET/Java topics)

Follow conventional wisdom

Page 44: Optimizing Application Architecture (.NET/Java topics)

Preaching .. DRY - Don’t repeat yourself Abstraction Principle KISS (Keep it simple, stupid!) Avoid Creating a YAGNI (You aren’t going to need

it) Do the simplest thing that could possibly work Don’t make me think Open/Closed Principle Write Code for the Maintainer Principle of least astonishment Single Responsibility Principle Minimize Coupling Maximize CohesionMostly Quoted from: http://www.artima.com/weblogs/viewpost.jsp?thread=331531

Page 45: Optimizing Application Architecture (.NET/Java topics)

More preaching Hide Implementation Details Law of Demeter Avoid Premature Optimization Code Reuse is Good Separation of Concerns Embrace Change Write Unit tests, know your mock Have good code coverage Use Dependency Injection Lint your code, cover your coverage (80%+) Use frequent builds/test runs

Mostly Quoted from: http://www.artima.com/weblogs/viewpost.jsp?thread=331531

Page 46: Optimizing Application Architecture (.NET/Java topics)

"How to test?" is a question that cannot be answered in general. "When to test?" however, does have a general answer: as early and as often as possible.

Bjarne Stroustrup

Page 47: Optimizing Application Architecture (.NET/Java topics)

01010100 01101000 01101001 01101110 01101011 00100000 01101111 01110101 01110100 01110011 01101001 01100100 01100101 00100000 01110100 01101000 01100101 00100000 01100010 01101111 01111000

Page 48: Optimizing Application Architecture (.NET/Java topics)

Think outside the box

Consider Functional Consider Rx Read up on new developments in

the Big Data/High speed computing fronts:AKKA, Apache Spark, DataTorrentDisruptor pattern, Mechanical Sympathy,

Work Stealing pattern

Page 49: Optimizing Application Architecture (.NET/Java topics)

Object-oriented programming is an exceptionally bad idea which could only have originated in California.

Edsger Dijkstra

http://en.wikiquote.org/wiki/Talk:Edsger_W._Dijkstra

Page 50: Optimizing Application Architecture (.NET/Java topics)

Consider Functional

Helps you think differently Immutable data-structures No side affects Functions don’t care about state Partial evaluations (higher order

functions)

Page 51: Optimizing Application Architecture (.NET/Java topics)

QuickSort in functional languages

Haskell (1990)

qsort [] = [] qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++ qsort (filter (>= x) xs)

F# (2005)

let rec quicksort l = match l with | [] -> [] | h::t -> quicksort (List.filter (fun x -> x < h) t)

@ h :: quicksort (List.filter (fun x -> x >= h) t)

http://en.wikibooks.org/wiki/Algorithm_Implementation/Sorting/Quicksort

Page 52: Optimizing Application Architecture (.NET/Java topics)

C# is functional too! Quicksort using Linq

public IEnumerable<T> Quicksort(List<T> v, IComparer<T> comparer) { if (v.Count < 2)

return v; T pivot = v[v.Count / 2]; return Quicksort(v.Where(x => comparer.Compare(x, pivot) < 0),

comparer) .Concat(new T[] { pivot }) .Concat(Quicksort(v.Where(x => comparer.Compare(x, pivot) > 0),

comparer)); }

http://en.wikibooks.org/wiki/Algorithm_Implementation/Sorting/Quicksort

Page 53: Optimizing Application Architecture (.NET/Java topics)

Consider Rx

Great for pub-sub scenarios with impedance mismatch:AsyncEvent streams

Well suited for front-end (WPF, Winforms) It makes your code simple and safe If you like Linq, you will love Rx! Rx is getting popular and is now available

for Java and JavaScript

Page 54: Optimizing Application Architecture (.NET/Java topics)

Learning Rx

MSDN has very good examples Excellent book by Lee Campbell (Free!)

Page 55: Optimizing Application Architecture (.NET/Java topics)

Rx in 15 minutes

http://channel9.msdn.com/Blogs/Charles/Erik-Meijer-Rx-in-15-Minutes

Page 57: Optimizing Application Architecture (.NET/Java topics)

Fools ignore complexity. Pragmatists suffer it. Some can avoid it. Geniuses remove it.

Alan J. Perlis

</End>