Presentation is loading. Please wait.

Presentation is loading. Please wait.

Herb Sutter.  What  What makes “C++11 feel like a new language”  Why  Values and tenets for language design  Contrast between native C++ and managed.

Similar presentations


Presentation on theme: "Herb Sutter.  What  What makes “C++11 feel like a new language”  Why  Values and tenets for language design  Contrast between native C++ and managed."— Presentation transcript:

1 Herb Sutter

2

3  What  What makes “C++11 feel like a new language”  Why  Values and tenets for language design  Contrast between native C++ and managed Java/.NET languages

4  Corollary: Lots of what people “know” about C++ is no longer true.  Changes to coding style/idioms/guidance.  That’s why it feels new. Style/idioms/guidance define a language.  Features that significantly change style/idioms/guidance include: Core Language autorange-for lambdasmove semantics uniform initialization Library smart pointers async & future

5 string flip( string s ) { reverse( begin(s), end(s) ); return s; } int main() { vector > v; v.push_back( async([]{ return flip(",olleH"); }) ); v.push_back( async([]{ return flip( ".gnaL"); }) ); v.push_back( async([]{ return flip("\n!TXEN"); }) ); for( auto& e : v ) { cout << e.get(); }

6 string flip( string s ) { reverse( begin(s), end(s) ); return s; } int main() { vector > v; v.push_back( async([]{ return flip(",olleH"); }) ); v.push_back( async([]{ return flip( ".gnaL"); }) ); v.push_back( async([]{ return flip("\n!TXEN"); }) ); for( auto& e : v ) { cout << e.get(); } always there: scoped lifetimes by construction (stack, member) strongly exception-safe deterministic always there: scoped lifetimes by construction (stack, member) strongly exception-safe deterministic

7 string flip( string s ) { reverse( begin(s), end(s) ); return s; } int main() { vector > v; v.push_back( async([]{ return flip(",olleH"); }) ); v.push_back( async([]{ return flip( ".gnaL"); }) ); v.push_back( async([]{ return flip("\n!TXEN"); }) ); for( auto& e : v ) { cout << e.get(); }

8 string flip( string s ) { reverse( begin(s), end(s) ); return s; } int main() { vector > v; v.push_back( async([]{ return flip(",olleH"); }) ); v.push_back( async([]{ return flip( ".gnaL"); }) ); v.push_back( async([]{ return flip("\n!TXEN"); }) ); for( auto& e : v ) { cout << e.get(); } asynchrony 201x’s best friend asynchrony 201x’s best friend

9 string flip( string s ) { reverse( begin(s), end(s) ); return s; } int main() { vector > v; v.push_back( async([]{ return flip(",olleH"); }) ); v.push_back( async([]{ return flip( ".gnaL"); }) ); v.push_back( async([]{ return flip("\n!TXEN"); }) ); for( auto& e : v ) { cout << e.get(); } lambdas invasion of the functionals capture by ref or by value lambdas invasion of the functionals capture by ref or by value

10 string flip( string s ) { reverse( begin(s), end(s) ); return s; } int main() { vector > v; v.push_back( async([]{ return flip(",olleH"); }) ); v.push_back( async([]{ return flip( ".gnaL"); }) ); v.push_back( async([]{ return flip("\n!TXEN"); }) ); for( auto& e : v ) { cout << e.get(); }

11 string flip( string s ) { reverse( begin(s), end(s) ); return s; } int main() { vector > v; v.push_back( async([]{ return flip(",olleH"); }) ); v.push_back( async([]{ return flip( ".gnaL"); }) ); v.push_back( async([]{ return flip("\n!TXEN"); }) ); for( auto& e : v ) { cout << e.get(); }

12 // <-- look ma, no * string flip( string s ) { reverse( begin(s), end(s) ); return s; } int main() { vector > v; v.push_back( async([]{ return flip(",olleH"); }) ); v.push_back( async([]{ return flip( ".gnaL"); }) ); v.push_back( async([]{ return flip("\n!TXEN"); }) ); for( auto& e : v ) { cout << e.get(); } move semantics = the semantics of value types (no pointers! declared lifetime!) + the efficiency of reference types (no deep copying!) move semantics = the semantics of value types (no pointers! declared lifetime!) + the efficiency of reference types (no deep copying!)

13 string flip( string s ) { reverse( begin(s), end(s) ); return s; } int main() { vector > v; v.push_back( async([]{ return flip(",olleH"); }) ); v.push_back( async([]{ return flip( ".gnaL"); }) ); v.push_back( async([]{ return flip("\n!TXEN"); }) ); for( auto& e : v ) { cout << e.get(); } Hello, Lang.NEXT!

14 string flip( string s ) { reverse( begin(s), end(s) ); return translate_to_french( move(s) ); } int main() { vector > v; v.push_back( async([]{ return flip(",olleH"); }) ); v.push_back( async([]{ return flip( ".gnaL"); }) ); v.push_back( async([]{ return flip("\n!TXEN"); }) ); for( auto& e : v ) { cout << e.get(); } Bonjour, Langue.SUIVANTE! program is guaranteed to allocate only three strings high compute / low latency program is guaranteed to allocate only three strings high compute / low latency

15  Use unique_ptr by default, and shared_ptr/weak_ptr to express shared ownership/observation.  Avoid “new” and “delete.”  Use a non-owning * or & to observe an object that will outlive you. C++98C++11 widget* pw = new widget(); delete pw; auto pw = make_shared (); class node { vector children; node* parent; class node { vector > children; node* parent;

16 Then Now circle* p = new circle( 42 ); vector vw; load_shapes( vw ); for( vector ::iterator i = vw.begin(); i != vw.end(); ++i ) { if( (*i)->weight() > 100 ) cout << **i << “ is a match\n”; } for( vector ::iterator i = vw.begin(); i != vw.end(); ++i ) { delete *i; } delete p; auto p = make_shared ( 42 ); auto vw = load_shapes(); for( auto& s : vw ) { if( s->weight() > 100 ) cout << *s << “ is a match\n”; } T*  shared_ptr new  make_shared no need for “delete” automatic lifetime management exception-safe range-for auto type deduction not exception-safe missing try/catch, __try/__finally return by value + move + auto again to deduce vector >

17  With C++11, modern C++ code is clean, safe, and fast.  As clean and safe as any other modern language.  “When used in a modern style.” – Bjarne Stroustrup  Was always fast. Now even faster.  Move semantics, constexpr.

18  What  What makes “C++11 feel like a new language”  Why  Values and tenets for language design  Contrast between native C++ and managed Java/.NET languages

19  Never compromise on performance and control.  Efficient abstraction (e.g., inlining by default)  Flexibility to express exactly what you need (e.g., specialization)  Exact control over memory layout (e.g., stack/member allocation)  Determinism and ordering WYSIWYG (e.g., stack/member lifetime)  Deeply believe in trust and zero overhead.  “Leave no room for a language lower than C++” other than asm  “Zero overhead” / “Don’t pay for what you don’t use”   “Trust the programmer” (  pitfalls! still give usability, guard rails)  “Always provide a way to open the hood and get down to the metal”

20  Never compromise on performance and control.  Efficient abstraction (e.g., inlining)  Flexibility to express exactly what you need (e.g., specialization)  Exact control over memory layout (e.g., stack/member allocation)  Determinism and ordering WYSIWYG (e.g., stack/member lifetime)  Deeply believe in trust and zero overhead.  “Leave no room for a language lower than C++” other than asm  “Zero overhead” / “Don’t pay for what you don’t use”   “Trust the programmer” (  pitfalls! still give usability, guard rails)  “Always provide a way to open the hood and get down to the metal” GPGPU

21  He says: “Provide default-on guard rails and programmer productivity too, but never at the expense of performance and control.”  She says: “Provide performance and control too, but never at the expense of always-on guard rails and programmer productivity.”

22 Performance & Control Productivity

23 Performance & Control Productivity

24 Performance & Control Productivity

25 Performance & Control Productivity

26 C++11 C++98 languagelibrary K&R C proxies for size comparisons: spec #pages, book #pages C# 3.0 (2008) Java 7 (2011) C# 4.0 (2010)

27 1979-1989 Research: C with Classes, ARM C++ 1989-1999 Mainstream C++ goes to town (& ISO, & space, &c) 1999-2009 Coffee-based languages for productivity Q: Can they do everything important? 2009- Native code invited back from exile with the Return of the King: Performance Per Watt

28

29 Phase/TrendMajor Constraints2x Efficient App Runs… (1950-90s) Compute-constrainedProcessor2x compute speed 2x users * http://perspectives.mvdirona.com/2010/09/18/OverallDataCenterCosts.aspx (1995ish-2007ish) Surplus local compute + low UI innovation (e.g., 2 nd party LOB client WIMP apps) Programmer timen/a (200x-) Mobile + bigger experiences (e.g., tablet, ‘smartphone’) Power (battery life) Processor 2x battery life 2x compute speed (2009-) Cloud / datacenter (e.g., Office 365, Shazam, Siri) Server HW (57%) Power (31%) * 0.56x nodes 0.56x power (2009-) Heterogeneous cores (e.g., Cell, GPGPU) Power (dark silicon) Processor 0.5x power envelope 2x compute speed (2020ish-) Moore’s EndProcessor2x compute speed forever

30 * http://perspectives.mvdirona.com/2010/09/18/OverallDataCenterCosts.aspx (200x-) Mobile + bigger experiences (e.g., tablet, ‘smartphone’) Power (battery life) Processor 2x battery life 2x compute speed (2009-) Cloud / datacenter (e.g., Office 365, Shazam, Siri) Server HW (57%) Power (31%) * 0.56x nodes 0.56x power (2009-) Heterogeneous cores (e.g., Cell, GPGPU) Power (dark silicon) Processor 0.5x power envelope 2x compute speed (2020ish-) Moore’s EndProcessor2x compute speed forever Note: The final four are going to dominate for the rest of our careers. Phase/TrendMajor Constraints2x Efficient App Runs…

31 When the business model is to charge for CPU hours, you had better make sure that you are giving customers something that is as resource efficient as possible.“ Niklas Gustafsson

32 Observations: [on Enterprise vs. Cloud ] People costs shift from top to nearly irrelevant. Work done/$ and work done/W are what really matters (S/W issues dominate). Expect high-scale service techniques to spread to enterprise. James Hamilton VP & Distinguished Engineer, Amazon Web Services http://www.mvdirona.com/jrh/TalksAndPapers/JamesHamilton_USENIX2009.pdf http://perspectives.mvdirona.com/2010/09/18/OverallDataCenterCosts.aspx http://mvdirona.com/jrh/TalksAndPapers/JamesHamilton_WhereDoesThePowerGo.pdf

33 Q: How long has your current application been in development? (N=387)

34  “Find your biggest cost, and optimize that.”  Where programmer time is your biggest cost/constraint, and you can afford some overheads, optimize for programmer productivity – consider suitable languages (there).  Where performance / {$|T|W} and/or layout/resources/determinism control is your biggest cost/constraint, optimize for performance and control – consider modern C++ (there).  Every well designed language is a finely crafted tool optimized for specific priorities and purpose. Use the right tool for the job.  It’s a mistake to try to make C#/Java do what C++ does, and vice versa.  (Hammer vs. screwdriver; prevention vs. cure; working with vs. against.)

35 MModern C++ is clean, safe, and fast. SStrong abstraction: Type-safe OO and generic code for modeling power, without sacrificing control and efficiency. FFull control over code and memory: You can always express what you want to do. You can always control memory and data layout exactly. PPay-as-you-go efficiency: No mandatory overheads. “The going word at Facebook is that ‘ reasonably written C++ code just runs fast,’ which underscores the enormous effort spent at optimizing PHP and Java code. Paradoxically, C++ code is more difficult to write than in other languages, but efficient code is a lot easier.” – Andrei Alexandrescu


Download ppt "Herb Sutter.  What  What makes “C++11 feel like a new language”  Why  Values and tenets for language design  Contrast between native C++ and managed."

Similar presentations


Ads by Google