Modern C++ Old Dog, New Tricks Todd L. Montgomery @toddlmontgomery - - PowerPoint PPT Presentation

modern c
SMART_READER_LITE
LIVE PREVIEW

Modern C++ Old Dog, New Tricks Todd L. Montgomery @toddlmontgomery - - PowerPoint PPT Presentation

StoneTor Modern C++ Old Dog, New Tricks Todd L. Montgomery @toddlmontgomery C++ is so old Languages are Tools Learning Tools is Good There are only two kinds of languages: the ones people complain about and the ones nobody uses.


slide-1
SLIDE 1

Modern C++

Old Dog, New Tricks

Todd L. Montgomery @toddlmontgomery

StoneTor

slide-2
SLIDE 2

C++ is so… old

slide-3
SLIDE 3

Languages are Tools Learning Tools is Good

slide-4
SLIDE 4

“There are only two kinds of languages: the ones people complain about and the ones nobody uses.”

— Bjarne Stroustrup

slide-5
SLIDE 5
  • Aeron and C++?
  • ReactiveSocket and C++?
  • What constitutes Modern C++?
  • What Lessons were learned?
slide-6
SLIDE 6

Aeron and C++?

slide-7
SLIDE 7

Truly modern messaging transport

slide-8
SLIDE 8

Feature Bloat & Complexity

slide-9
SLIDE 9

Not Fast Enough

slide-10
SLIDE 10

Low & Predictable Latency is key

slide-11
SLIDE 11

We are in a new world

Multi-core, Multi-socket, Cloud...

slide-12
SLIDE 12

We are in a new world

UDP, IPC, InfiniBand,
 RDMA, PCI-e Multi-core, Multi-socket, Cloud...

slide-13
SLIDE 13

Aeron is trying a new approach

slide-14
SLIDE 14

The Team

Todd Montgomery Richard Warburton Martin Thompson

slide-15
SLIDE 15

Publishers Subscribers Channel Stream

Messaging

Channel

slide-16
SLIDE 16

A library, not a framework, on which other abstractions and applications can be built

slide-17
SLIDE 17

Composable Design

slide-18
SLIDE 18

OSI layer 4 Transport for message oriented streams

slide-19
SLIDE 19

OSI Layer 4 (Transport) Services

  • 1. Connection Oriented Communication
  • 2. Reliability
  • 3. Flow Control
  • 4. Congestion Avoidance/Control
  • 5. Multiplexing
slide-20
SLIDE 20

Publisher Subscriber Subscriber Publisher

Architecture

IPC Log Buffer

slide-21
SLIDE 21

Sender Receiver Receiver Sender Publisher Subscriber Subscriber Publisher

Architecture

Media IPC Log Buffer Media (UDP, InfiniBand, PCI-e 3.0)

slide-22
SLIDE 22

Conductor Sender Receiver Conductor Receiver Sender Publisher Subscriber Subscriber Publisher Admin Events

Architecture

Admin Events Media IPC Log Buffer Media (UDP, InfiniBand, PCI-e 3.0) Function/Method Call Volatile Fields & Queues

slide-23
SLIDE 23

Client Media Driver Media Driver Conductor Sender Receiver Conductor Receiver Sender Client Publisher Conductor Conductor Subscriber Subscriber Publisher Admin Events

Architecture

Admin Events Media IPC Log Buffer IPC Ring/Broadcast Buffer Media (UDP, InfiniBand, PCI-e 3.0) Function/Method Call Volatile Fields & Queues

slide-24
SLIDE 24

C++?

slide-25
SLIDE 25

Client Media Driver Media Driver Conductor Sender Receiver Conductor Receiver Sender Client Publisher Conductor Conductor Subscriber Subscriber Publisher Admin Events

Architecture

Admin Events Media IPC Log Buffer IPC Ring/Broadcast Buffer Media (UDP, InfiniBand, PCI-e 3.0) Function/Method Call Volatile Fields & Queues

slide-26
SLIDE 26

Client Media Driver Media Driver Conductor Sender Receiver Conductor Receiver Sender Client Publisher Conductor Conductor Subscriber Subscriber Publisher Admin Events

Architecture

Admin Events Media IPC Log Buffer IPC Ring/Broadcast Buffer Media (UDP, InfiniBand, PCI-e 3.0) Function/Method Call Volatile Fields & Queues Client

  • Application API
  • Java, C, C++, C#, etc.
slide-27
SLIDE 27

The Media Driver is (currently) Java…

slide-28
SLIDE 28

… currently…

slide-29
SLIDE 29

ReactiveSocket and C++?

slide-30
SLIDE 30

Today, Asynchronous is the norm

slide-31
SLIDE 31

The Lure of Complexity The Need for Simplicity

Asynchrony

slide-32
SLIDE 32

Composition is hard

Asynchrony

slide-33
SLIDE 33

ReactiveX

http://reactivex.io/

slide-34
SLIDE 34

Observables

slide-35
SLIDE 35

Challenges

  • 1. Non-Blocking Back Pressure
  • 2. Heterogeneous Connectivity
slide-36
SLIDE 36

Dealing with Back Pressure

  • 1. Reative Streams
  • 2. RxJava 2.0
  • 3. Akka Streams
  • 4. java.util.concurrent.Flow (JEP 266)

http://www.reactive-streams.org/

slide-37
SLIDE 37

Rx Heterogeneous Connectivity

  • 1. ReactiveSocket

http://reactivesocket.io/

slide-38
SLIDE 38

C++?

slide-39
SLIDE 39

Rx Heterogeneous Connectivity

  • 1. ReactiveSocket

http://reactivesocket.io/

Protocol

  • Reactive Streams - Application API
  • Java, JS, C++, Go, etc.
  • What do you call a 1 language protocol?
slide-40
SLIDE 40

What constitutes Modern C++?

slide-41
SLIDE 41
slide-42
SLIDE 42

“Within C++, there is a much smaller and cleaner language struggling to get out.” ”And no, that smaller and cleaner language is not Java or C#.”

— Bjarne Stroustrup

slide-43
SLIDE 43

Modern C++

Main Concept(s)

slide-44
SLIDE 44

Resource Ownership & Lifetime

slide-45
SLIDE 45

Scope Coupling Dependency

Resource Ownership & Lifetime

slide-46
SLIDE 46

Modern C++

  • Idioms (RAII, etc.)
slide-47
SLIDE 47

std::lock_guard std::unique_ptr std::shared_ptr

Resource Acquisition is Initialization (RAII)

http://en.cppreference.com/w/cpp/language/raii

slide-48
SLIDE 48

Other C++ Idioms

  • PImpl
  • CRTP
  • Erase-Remove
  • SFINAE*

* Substitution Failure is Not An Error

slide-49
SLIDE 49

Modern C++

  • Idioms (RAII, etc.)
  • Smart Pointers
slide-50
SLIDE 50

std::shared_ptr std::unique_ptr std::weak_ptr

Smart Pointers

http://en.cppreference.com/w/cpp/memory

slide-51
SLIDE 51

Smart Pointers

slide-52
SLIDE 52

Smart Pointers & Ownership

slide-53
SLIDE 53

Modern C++

  • Idioms (RAII, etc.)
  • Smart Pointers
  • Lambda's & Function Objects
slide-54
SLIDE 54

Lambda’s & Function Objects

slide-55
SLIDE 55

Modern C++

  • Idioms (RAII, etc.)
  • Smart Pointers
  • Lambda's & Function Objects
  • Atomics (std::atomic)
slide-56
SLIDE 56

std::atomic<bool> std::atomic_flag std::atomic<int>

Atomic Operations

http://en.cppreference.com/w/cpp/atomic

slide-57
SLIDE 57

Modern C++

  • Idioms (RAII, etc.)
  • Smart Pointers
  • Lambda's & Function Objects
  • Atomics (std::atomic)
  • Thread Support
slide-58
SLIDE 58

std::thread std::mutex std::async std::promise std::future

Thread Support

http://en.cppreference.com/w/cpp/thread

slide-59
SLIDE 59

Thread Support

slide-60
SLIDE 60

Modern C++

  • Idioms (RAII, etc.)
  • Smart Pointers
  • Lambda's & Function Objects
  • Atomics (std::atomic)
  • Thread Support
  • Move Construction/Assignment *
slide-61
SLIDE 61

Moving vs. Copying Ownership?

Move Construction/Assignment

http://en.cppreference.com/w/cpp/language/rule_of_three

slide-62
SLIDE 62

Modern C++

  • Idioms (RAII, etc.)
  • Smart Pointers
  • Lambda's & Function Objects
  • Atomics (std::atomic)
  • Thread Support
  • Move Construction/Assignment *
  • Toolchain (Build/Test)
slide-63
SLIDE 63

CMake Google Test Google Mock Google Benchmark etc.

Modern Toolchain

https://cmake.org/ https://github.com/google/googletest

slide-64
SLIDE 64

What Lessons were learned?

slide-65
SLIDE 65

Lessons - What do you think?

  • Idioms (RAII, etc.)
  • Stack Allocation
  • Smart Pointers
  • Lambda's & Function Objects
  • Atomics (std::atomic)
  • Thread Support
  • Move Construction/Assignment
  • Toolchain (Build/Test)
slide-66
SLIDE 66

Lessons

  • Idioms (RAII, etc.)
  • Stack Allocation
  • Smart Pointers
  • Lambda's & Function Objects
  • Atomics (std::atomic)
  • Thread Support
  • Move Construction/Assignment
  • Toolchain (Build/Test)
slide-67
SLIDE 67
slide-68
SLIDE 68

RAII & Smart Pointers

  • Give in to Smart Pointers
slide-69
SLIDE 69

But But But… CYCLES!!!

Smart Pointers

slide-70
SLIDE 70

Dealing with “Cycles”

  • std::weak_ptr
  • std::enable_shared_from_this
  • Design them out
  • Techniques & Indirection
  • RCU / QSBR
  • Deferred & Unordered Destruction

(GCPP)

https://github.com/hsutter/gcpp

slide-71
SLIDE 71

RAII & Smart Pointers

  • Give in to Smart Pointers
  • Explicit Coupling
slide-72
SLIDE 72

RAII & Smart Pointers

  • Give in to Smart Pointers
  • Explicit Coupling
  • Explicit Scoping
slide-73
SLIDE 73

RAII & Smart Pointers

  • Give in to Smart Pointers
  • Explicit Coupling
  • Explicit Scoping
  • Explicit Dependency
slide-74
SLIDE 74

Hiding Complexity is not Dealing with Complexity

Explicit Coupling / Scope / Dependency

slide-75
SLIDE 75

Speaks to Quality Build Quality In

Explicit Coupling / Scope / Dependency

slide-76
SLIDE 76

Enforced Decoupling & Isolation….

Coupling / Scope / Dependency

slide-77
SLIDE 77

What a language provides? What a language takes away?

More Important?

slide-78
SLIDE 78

Deal with it up front Pays off massively later “If it hurts, do it more often”

Explicit Coupling / Scope / Dependency

slide-79
SLIDE 79

Lessons

  • Idioms (RAII, etc.)
  • Stack Allocation
  • Smart Pointers
  • Lambda's & Function Objects
  • Atomics (std::atomic)
  • Thread Support
  • Move Construction/Assignment
  • Toolchain (Build/Test)
slide-80
SLIDE 80

Not Needed Because Escape Analysis Value Types … ${EXCUSE}[i]

Stack Allocation (Lack of)

slide-81
SLIDE 81
slide-82
SLIDE 82
slide-83
SLIDE 83

Mechanism is NOT a set of “features”

Stack Allocation

slide-84
SLIDE 84

Stack Function Objects very very low cost deferred/conditional execution

Stack Allocation

slide-85
SLIDE 85

Lessons

  • Idioms (RAII, etc.)
  • Stack Allocation
  • Smart Pointers
  • Lambda's & Function Objects
  • Atomics (std::atomic)
  • Thread Support
  • Move Construction/Assignment
  • Toolchain (Build/Test)
slide-86
SLIDE 86
slide-87
SLIDE 87

Move Construction/Assignment

  • Much more than you think
slide-88
SLIDE 88

Move Construction/Assignment

  • Much more than you think
  • Sometimes/Often better to copy
slide-89
SLIDE 89

Move Construction/Assignment

  • Much more than you think
  • Sometimes/Often better to copy
  • Optimization Interactions (e.g. SSO)
slide-90
SLIDE 90

Move Construction/Assignment

  • Much more than you think
  • Sometimes/Often better to copy
  • Optimization Interactions (e.g. SSO)
  • When you have to do it… why?!?
slide-91
SLIDE 91

Lessons

  • Idioms (RAII, etc.)
  • Stack Allocation
  • Smart Pointers
  • Lambda's & Function Objects
  • Atomics (std::atomic)
  • Thread Support
  • Move Construction/Assignment
  • Toolchain (Build/Test)
slide-92
SLIDE 92
slide-93
SLIDE 93

Client Media Driver Media Driver Conductor Sender Receiver Conductor Receiver Sender Client Publisher Conductor Conductor Subscriber Subscriber Publisher Admin Events

Architecture

Admin Events Media IPC Log Buffer IPC Ring/Broadcast Buffer Media (UDP, InfiniBand, PCI-e 3.0) Function/Method Call Volatile Fields & Queues

slide-94
SLIDE 94

Data Structures (Shared Memory)

  • IPC Ring Buffers
  • IPC Broadcast Buffers
  • IPC Log Buffers
slide-95
SLIDE 95

Creates a


replicated persistent log


  • f messages

What Aeron does

slide-96
SLIDE 96

Log Buffer File

Term 2 Log Meta Data

Atomic & Ordered Operations

Term 1 Term 0

slide-97
SLIDE 97

Position

slide-98
SLIDE 98

Unique identification of a byte within each stream

slide-99
SLIDE 99

Publishers, Senders,
 Receivers, and Subscribers
 all keep position counters

slide-100
SLIDE 100

Position counters are the key to
 flow control and monitoring

slide-101
SLIDE 101

Statistics & Position Counters are accessible in shared memory

Atomic & Ordered Operations

slide-102
SLIDE 102

Multiple Challenges

  • Size (and Layout)
  • Memory Models (C++11 to Java)
slide-103
SLIDE 103

Size

Matters

slide-104
SLIDE 104
slide-105
SLIDE 105

Size Matters (Atomics)

  • Not designed for arbitrary memory
  • Size not the same as the types
  • Concerned only with operations
slide-106
SLIDE 106

Memory Models

  • Interoperability with JMM
  • std::memory_order fit for purpose
slide-107
SLIDE 107

So… Aeron uses its own atomic

  • perations C/C++ functions

(JMM compatible)

* gcc x86_64 initially contributed by phaynes (phaynes@threatmetrix.com)

slide-108
SLIDE 108

Wrapping Up

slide-109
SLIDE 109

Languages are Tools Fit for purpose Add to your toolbox!

slide-110
SLIDE 110

“There are more useful systems developed in languages deemed awful than in languages praised for being beautiful--many more.”

— Bjarne Stroustrup (via Jason Pontin)

slide-111
SLIDE 111
slide-112
SLIDE 112

https://github.com/real-logic/Aeron

Where can I find them?

https://github.com/ReactiveSocket/ reactivesocket-cpp

slide-113
SLIDE 113

Aeron: https://github.com/real-logic/Aeron ReactiveSocket: https://github.com/ReactiveSocket/reactivesocket-cpp Twitter: @toddlmontgomery

Thank You!

Questions?