Slide 1

Slide 1 text

Boost Performance with Memory View Zero-Copy Zen:

Slide 2

Slide 2 text

Kesia Mary Joies Product Engineer Strollby, UST Speaker

Slide 3

Slide 3 text

Let’s begin…

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Zero-Copy ゼロコピー

Slide 6

Slide 6 text

A method to copy data from the disk/network to the memory without passing through the CPU

Slide 7

Slide 7 text

Traditional Mechanism

Slide 8

Slide 8 text

Zero-copy Mechanism

Slide 9

Slide 9 text

Let’s look into an example

Slide 10

Slide 10 text

Receiving server

Slide 11

Slide 11 text

Traditional Copy Client

Slide 12

Slide 12 text

A test run using a test file with 2GB size

Slide 13

Slide 13 text

Zero-Copy Client

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

And, what is buffer protocol ?

Slide 16

Slide 16 text

Buffer Protocol It is a protocol that provides a way to access the internal data of an object.

Slide 17

Slide 17 text

Buffer Protocol Improves execution speed

Slide 18

Slide 18 text

Buffer Protocol Improves execution speed Use less memory

Slide 19

Slide 19 text

Improves execution speed Use less memory Works on large data Buffer Protocol

Slide 20

Slide 20 text

Whenever we perform some action on an object, Python needs to create a copy of the object.

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

Buffer Protocol But the problem is, even if we cannot access this protocol with the standard code base, this is accessible to us at the C-API level.

Slide 23

Slide 23 text

Buffer Protocol But the problem is, even if we cannot access this protocol with the standard code base, this is accessible to us at the C-API level. So, in Python, if we want to expose the same protocol in Python, we need to use memory view.

Slide 24

Slide 24 text

Before introducing memory view…

Slide 25

Slide 25 text

Python’s built-in binary data types

Slide 26

Slide 26 text

Python’s built-in binary data types Bytes

Slide 27

Slide 27 text

Python’s built-in binary data types Bytes Byte Array

Slide 28

Slide 28 text

Bytes Byte Array Memory View Python’s built-in binary data types

Slide 29

Slide 29 text

Bytes Immutable data type

Slide 30

Slide 30 text

Bytes Immutable data type Return a byte object

Slide 31

Slide 31 text

Immutable data type Return a byte object Must be iterable of integers between 0 <= x < 256 Bytes

Slide 32

Slide 32 text

Bytes Syntax: bytes([source[, encoding[, errors]]])

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

Bytearray

Slide 40

Slide 40 text

Bytearray Similar to bytes

Slide 41

Slide 41 text

Bytearray Similar to bytes Supports mutability

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

Memory View

Slide 45

Slide 45 text

Memory View Zero-Copy View

Slide 46

Slide 46 text

Memory View Zero-Copy View Slicing and Indexing

Slide 47

Slide 47 text

Memory View Zero-Copy View Slicing and Indexing Efficient Data Manipulation

Slide 48

Slide 48 text

Memory View Syntax: memoryview(object)

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

Converts to byte array using str.encode()

Slide 53

Slide 53 text

Converts to byte array using str.encode()

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

PEP 688 – Making the buffer protocol accessible in Python ● A better way to interact with the buffer protocol directly. ● Now supports additional types with the buffer protocol. ● Simplified Workflow ● Performance Boost

Slide 59

Slide 59 text

Comparison

Slide 60

Slide 60 text

Without Memory View

Slide 61

Slide 61 text

With Memory View Without Memory View

Slide 62

Slide 62 text

Without Memory View With Memory View Without Memory View

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

Zero-copy zen_boost performance with memory view.talk PyCon to Memory 3 minutes

Slide 65

Slide 65 text

Applications

Slide 66

Slide 66 text

Example - Numeric and Scientific Computing:

Slide 67

Slide 67 text

Example - Video Streaming

Slide 68

Slide 68 text

Example - Video Streaming

Slide 69

Slide 69 text

Example - Video Streaming

Slide 70

Slide 70 text

Reduced Memory Footprint Benefits

Slide 71

Slide 71 text

Reduced Memory Footprint Better Performance Benefits

Slide 72

Slide 72 text

Reduced Memory Footprint Better Performance Improved Scalability Benefits

Slide 73

Slide 73 text

References 1. Idea of zero copy [with example] 2. Effective Python › Item 74: Consider memoryview and bytearray for Zero-Copy Interactions with bytes 3. Python memoryview() 4. Python Memoryview Example for Beginners 5. memoryview in Python 6. Python memoryview() Function (Buffer Protocol And Memory View) 7. PEP 688 – Making the buffer protocol accessible in Python

Slide 74

Slide 74 text

@kesiajoies Thank you