2. 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
3. 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
4. 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.
5. Agenda: (Re)Visiting the
programming 101
Data Structures and Algorithms
Serialization
Synchronization
Other stuff (Rx, functional and so on)
7. Do you know ..
What sorting algorithm is used by
Array.Sort ?
Describe internals of List<T>. Compare
and contrast with LinkedList<T>
9. Quicksort honored as
one of top 10 algorithms
of 20th century
in science and
engineering.
Source: Wikipedia.org
Quicksort invented by Tony Hoare
(1960)
10. Why does it matter ?
Robert Sedgwick, Princeton University http://www.cs.princeton.edu/~rs/
11. Lists
List<T> in .NET is highly optimized
You typically never have to use anything
else
Initial size makes a difference
12. Consider an array!
Arrays are written using native code
Much faster than all other data
structures
13. 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
14. Skiplist - searching example
From Scott Mitchell’s article in MSDN “An Extensive Examination of Data Structures Using C# 2.0”
15. .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
22. 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 clients
Storing data in a cache
Distributed computing, cloud storage
23. Popular protocols
Protocol Buffer (Protobuf)
Created 2001; Opensourced 2008
Designed and used extensively by Google
Apache Thrift
Created in 2007
Used extensively in Facebook
Apache Avro
Created by LinkedIn and open sourced in
2011
See a good overview of different protocols here
See performance comparison of various formats here
24. So many ways to serialize!
Serialization time for various protocols
https://code.google.com/p/thrift-protobuf-compare/wiki/Benchmarking
33. 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.
34. 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.
41. 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
44. 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 Cohesion
Mostly Quoted from: http://www.artima.com/weblogs/viewpost.jsp?thread=331531
45. 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
46. "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
48. Think outside the box
Consider Functional
Consider Rx
Read up on new developments in the
Big Data/High speed computing fronts:
AKKA, Apache Spark, DataTorrent
Disruptor pattern, Mechanical Sympathy,
Work Stealing pattern
50. Consider Functional
Helps you think differently
Immutable data-structures
No side affects
Functions don’t care about state
Partial evaluations (higher order
functions)
51. 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
52. 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
53. Consider Rx
Great for pub-sub scenarios with
impedance mismatch:
Async
Event 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
54. Learning Rx
MSDN has very good examples
Excellent book by Lee Campbell (Free!)
55. Rx in 15 minutes
http://channel9.msdn.com/Blogs/Charles/Erik-Meijer-Rx-in-15-Minutes