better code: data structures...2016/08/08 · be!er code 2! regular types ! goal: implement...
TRANSCRIPT
Better Code: Data StructuresSean Parent | Principal Scientist
© 2015 Adobe Systems Incorporated.
Better Code
2
! Regular Types
! Goal: Implement Complete and Efficient Types
! Algorithms
! Goal: No Raw Loops
! Data Structures
! Goal: No Incidental Data Structures
! Runtime Polymorphism
! Goal: No Raw Pointers
! Concurrency
! Goal: No Raw Synchronization Primitives
! …
© 2015 Adobe Systems Incorporated.
Goal: No incidental data structures
3
© 2015 Adobe Systems Incorporated.
What is an incidental data structure?
4
© 2015 Adobe Systems Incorporated.
What is a data structure?
5
Definition: A data structure is a format for organizing and storing data.
© 2015 Adobe Systems Incorporated.
What is a structure?
6
Definition: A structure on a set consists of additional entities that, in some manner, relate to the set, endowing the
collection with meaning or significance.
[ This slide intentionally left void ]
0100
0100
! !
! !
0100
0100
4
0100
0100
[-8..7]
0100
[-8..7]
0011
01000011
4>3
01000011
hash( ) != hash( )
110000101111011111110111101100110011001110001100101001111010011011010100100001001000110000101000110011000000111001010011010100100110010001101110011100001010011111011101000111001111000001100111110010001011111100110011011100101000111111101001100010110100011010110010101101101000010000010000110100000001000000110110101000011100001100011000000110001100010001010111110011100011101101
© 2015 Adobe Systems Incorporated. All Rights Reserved.
Memory Space
17
110000101111011111110111101100110011001110001100101001111010011011010100100001001000110000101000110011000000111001010011010100100110010001101110011100001010011111011101000111001111000001101001110010001011111100110011011100101000111111100111100010110100011010110010101101101000010000010000110100000001000000110110101000011100001100011000000110001100010001010111110011100011101101
0100
0100
0011
<
110000101111011111110111101100110011001110001100101001111010011011010100100001001000110000101000110011000000111001010011010100100110010001101110011100001010011111011101000111001111000001100111110010001011111100110011011100101000111111101001100010110100011010110010101101101000010000010000110100000001000000110110101000011100001100011000000110001100010001010111110011100011101101
© 2015 Adobe Systems Incorporated. All Rights Reserved.
Memory Space
18
00110100
+0111
© 2015 Adobe Systems Incorporated.
Whole-Part Relationships and Composite Objects
! Connected
! Noncircular
! Logically Disjoint
! Owning
! Standard Containers are Composite Objects
19
Part A
Part B
Part C
Part A
Part B
Part C
Part A
Part B
Part C
Part A’
Part B’
Part A
Part B
Part C
Part A
Part B
Part C
Elements of Programming, Chapter 12
© 2015 Adobe Systems Incorporated.
What is a data structure?
Definition: A structure utilizing value, physical, and representational relationships to encode semantic
relationships on a collection of objects.
The choice of encoding can make a dramatic difference on the performance of operations.
20
© 2015 Adobe Systems Incorporated.
Data Structure Performance
! Hierarchical Memory Structure
! Register Access 0.1 ns
! L1 Cache 0.5 ns
! L2 Cache 7.0 ns
! Memory 100.0 ns
! RAM behaves much like a disk drive
21
3GHz processor, from Chandler Carruth talk - Credit to Jeff Dean
log2 1,000,000,000,000 ≈ 40
© 2015 Adobe Systems Incorporated.
Data Structure Performance
! Locality matters - use arrays or vector
! Parallel Arrays
! Static Lookup Tables
! Closed Hash Maps
! Algorithms
22
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Example: Parallel Array & Algorithms
23
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Gather
24
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Gather
25
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Gather
26
p
l
stable_partition(p, l, s)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Gather
27
p
f
stable_partition(f, p, not1(s))
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Gather
28
stable_partition(f, p, not1(s)) stable_partition(p, l, s)
p
l
f
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Gather
29
stable_partition(f, p, not1(s)) stable_partition(p, l, s)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Gather
30
return { stable_partition(f, p, not1(s)), stable_partition(p, l, s) };
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Gather
31
template <typename I, // I models BidirectionalIterator typename S> // S models UnaryPredicate auto gather(I f, I l, I p, S s) -> pair<I, I> { return { stable_partition(f, p, not1(s)), stable_partition(p, l, s) }; }
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Gather
32
template <typename I, // I models BidirectionalIterator typename S> // S models UnaryPredicate auto gather(I f, I l, I p, S s) -> pair<I, I> { return { stable_partition(f, p, not1(s)), stable_partition(p, l, s) }; }
p
l
f
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
33
l
f
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
34
l
f
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
35
m
l
f
stable_partition(f, m, p)
stable_partition(m, l, p)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
36
m
rotate(stable_partition(f, m, p), m, stable_partition(m, l, p));
stable_partition(f, m, p)
stable_partition(m, l, p)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
37
rotate(stable_partition(f, m, p), m, stable_partition(m, l, p));
return rotate(stable_partition(f, m, p), m, stable_partition(m, l, p));
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
38
return rotate(stable_partition(f, m, p), m, stable_partition(m, l, p));
if (n == 1) return f + p(*f);
return rotate(stable_partition(f, m, p), m, stable_partition(m, l, p));
r
fr
template <typename I, typename P> auto stable_partition(I f, I l, P p) -> I { auto n = l - f; if (n == 0) return f; if (n == 1) return f + p(*f); auto m = f + (n / 2);
return rotate(stable_partition(f, m, p), m, stable_partition(m, l, p)); }
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
39
if (n == 1) return f + p(*f);
return rotate(stable_partition(f, m, p), m, stable_partition(m, l, p));
l
f
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
40
template <typename I, typename P> auto stable_partition(I f, I l, P p) -> I { auto n = l - f; if (n == 0) return f; if (n == 1) return f + p(*f); auto m = f + (n / 2);
return rotate(stable_partition(f, m, p), m, stable_partition(m, l, p)); }
template <typename I, typename P> auto stable_partition_position(I f, I l, P p) -> I { auto n = l - f; if (n == 0) return f; if (n == 1) return f + p(f); auto m = f + (n / 2);
return rotate(stable_partition_position(f, m, p), m, stable_partition_position(m, l, p)); }
l
f
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
41
int a[] = { 1, 2, 3, 4, 5, 5, 4, 3, 2, 1 }; bool b[] = { 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 };
auto p = stable_partition_position(begin(a), end(a), [&](auto i) { return *(begin(b) + (i - begin(a))); });
for (auto f = begin(a), l = p; f != l; ++f) cout << *f << " "; cout << "^ "; for (auto f = p, l = end(a); f != l; ++f) cout << *f << " "; cout << endl;
2 4 4 2 ^ 1 3 5 5 3 1
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Example: Algorithms & Minimal Work
42
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
43
41312795151421161610183
sf
sl
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
44
sf
sl
12345678910111213141516
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
45
sf
sl
XXXXX6789XXXXXXX
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
46
41312795151421161610183
f
l
sf
sl
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
47
41312795151421161610183
f
l
sf nth_element(f, sf, l);
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
48
f
l
sf nth_element(f, sf, l);
21345671412159161013811
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
49
nth_element(f, sf, l);
21345671412159161013811
f
l
sf
≤ *sf
≥ *sf
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
50
nth_element(f, sf, l);
21345671412159161013811
f
l
sl
sf
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
51
nth_element(f, sf, l); ++sf;
21345671412159161013811
f
l
sl
sf
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
52
nth_element(f, sf, l); ++sf;
partial_sort(sf, sl, l);
21345671412159161013811
f
l
sl
sf
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
53
nth_element(f, sf, l); ++sf;
partial_sort(sf, sl, l);
f
l
sl
sf
21345678915141612131011
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
54
nth_element(f, sf, l); ++sf;
partial_sort(sf, sl, l);
f
l
sl
sf
21345678915141612131011
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
55
if (sf == sl) return;
nth_element(f, sf, l); ++sf;
partial_sort(sf, sl, l);
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
56
if (sf == sl) return; if (sf != f) { nth_element(f, sf, l); ++sf; } partial_sort(sf, sl, l);
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
57
template <typename I> // I models RandomAccessIterator void sort_subrange(I f, I l, I sf, I sl) { if (sf == sl) return; if (sf != f) { nth_element(f, sf, l); ++sf; } partial_sort(sf, sl, l); }
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
58
sort_subrange(f, l, sf, sl); f
l
sl
sf
sl
41312795151421161610183
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
59
sort_subrange(f, l, sf, sl); f
l
sl
21345678915141612131011
sf
sl
nl
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
60
sort_subrange(f, l, sf, sl); partial_sort(sl, nl, l);
f
l
sl
21345678915141612131011
sf
nl
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
61
sort_subrange(f, l, sf, sl); partial_sort(sl, nl, l);
f
l
sl
sf
nl
21345678910111216151413
© 2015 Adobe Systems Incorporated.
What is an incidental data structure?
62
Definition: An incidental data structure is a data structure that occurs within a system when there is no object representing
the structure as a whole.
Structures formed in the absence of a whole/part relationship
© 2015 Adobe Systems Incorporated.
Why no incidental data structures?
63
! They cause ambiguities and break our ability to reason about code locally
© 2015 Adobe Systems Incorporated.
Incidental Data Structures
! Delegates
! Message handlers
! Any pointer or reference stored in an object which refers to another object which is not a part
64
© 2015 Adobe Systems Incorporated.
Incidental Data Structures
! Self-referential interface
class UIElement { };
class UIElementCollection { public: void Add(shared_ptr<UIElement>); };
class Panel : public UIElement { public: shared_ptr<UIElementCollection> Children() const; };
panel->Children()->Add(element); panel->Children()->Add(element); panel2->Children()->Add(element); panel->Children()->Add(panel);
65
Panel
Element
PanelPanel Panel2
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
66
A
forest
E
B C D
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
67
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
68
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
69
A
list
B
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
70
A
forest
E
B C D
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
71
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
72
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
73
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
74
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
75
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
76
A
forest
E
B C D
end()begin()
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
77
leading
trailing
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
78
A
forest
E
B C D
end()begin()
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
79
forest<string> f;
f.insert(end(f), "A"); f.insert(end(f), "E");
auto a = trailing_of(begin(f)); f.insert(a, "B"); f.insert(a, "C"); f.insert(a, "D");
A
forest
E
B C D
end()begin()
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
80
forest<string> f;
f.insert(end(f), "A"); f.insert(end(f), "E");
auto a = trailing_of(begin(f)); f.insert(a, "B"); f.insert(a, "C"); f.insert(a, “D");
auto r = depth_range(f); for (auto f = begin(r), l = end(r); f != l; ++f) { cout << string(f.depth() * 4, ' ') << (f.edge() ? "<" : "</") << *f << “>\n"; }
<A> <B> </B> <C> </C> <D> </D> </A> <E> </E>
© 2015 Adobe Systems Incorporated.
Conclusions
81
! Understand the structures created by relationships
! Encapsulate structure invariants in composite types
! Learn to use the tools at your disposal
! And how to create new ones
! Slides and code from talk: ! https://github.com/sean-parent/sean-parent.github.io/wiki/Papers-and-Presentations
! Forest library: ! https://github.com/stlab/adobe_source_libraries
© 2015 Adobe Systems Incorporated.
No incidental data structures
Composite Types
Better Code
82
© 2015 Adobe Systems Incorporated.