Slide 1

Slide 1 text

RMagick, migrate to ImageMagick 7 Shizuo Fujita

Slide 2

Slide 2 text

self.inspect • @watson1978 • Ubiregi, Inc • Ruby committer • RMagick maintainer

Slide 3

Slide 3 text

What's RMagick

Slide 4

Slide 4 text

"RMagick is an interface between the Ruby programming language and the ImageMagick image processing library."

Slide 5

Slide 5 text

Active members ˏmockdeep ˏwatson1978 ˏdlemstra

Slide 6

Slide 6 text

My Motivation

Slide 7

Slide 7 text

I want to use Ruby ... •Web programming

Slide 8

Slide 8 text

I want to use Ruby ... •Web programming •iOS programming

Slide 9

Slide 9 text

I want to use Ruby ... •Web programming •iOS programming •Image processing

Slide 10

Slide 10 text

I want to use Ruby ... •Web programming •iOS programming •Image processing •Machine learning, etc

Slide 11

Slide 11 text

I want to use Ruby ... •Web programming •iOS programming •Image processing •Machine learning, etc To meet crazy great people at Ruby World

Slide 12

Slide 12 text

Current Status of RMagick

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Fixed major problems since v2.16.0 •Set up •Memory leaks •SEGVs

Slide 15

Slide 15 text

Set up problems

Slide 16

Slide 16 text

Set up problems •macOS (Homebrew) •Windows

Slide 17

Slide 17 text

macOS (Homebrew)

Slide 18

Slide 18 text

macOS (Homebrew)

Slide 19

Slide 19 text

Windows •Ruby has some environments • MingW • MSWin • Cygwin • Windows Subsystem for Linux

Slide 20

Slide 20 text

Windows •Ruby has some environments • MingW • MSWin • Cygwin • Windows Subsystem for Linux

Slide 21

Slide 21 text

MingW...?

Slide 22

Slide 22 text

Set up : MingW

Slide 23

Slide 23 text

Solution https://github.com/oneclick/rubyinstaller2/wiki/For-gem-developers

Slide 24

Slide 24 text

MingW works

Slide 25

Slide 25 text

MSWin...?

Slide 26

Slide 26 text

Set up : MSWin

Slide 27

Slide 27 text

MSWin supported...? Looks like MSWin supporting code...

Slide 28

Slide 28 text

Not supported... Why !? For MSWin

Slide 29

Slide 29 text

MSWin works well

Slide 30

Slide 30 text

Memory leak

Slide 31

Slide 31 text

Fixed many memory leaks 55 Closed

Slide 32

Slide 32 text

Case 1 VALUE Image_shear(VALUE self, VALUE x_shear, VALUE y_shear) { Image *image, *new_image; ExceptionInfo *exception; image = rm_check_destroyed(self); exception = AcquireExceptionInfo(); new_image = ShearImage(image, NUM2DBL(x_shear), NUM2DBL(y_shear), exception); rm_check_exception(exception, new_image, DestroyOnError); (void) DestroyExceptionInfo(exception); rm_ensure_result(new_image); return rm_image_new(new_image); }

Slide 33

Slide 33 text

VALUE Image_shear(VALUE self, VALUE x_shear, VALUE y_shear) { Image *image, *new_image; ExceptionInfo *exception; image = rm_check_destroyed(self); exception = AcquireExceptionInfo(); new_image = ShearImage(image, NUM2DBL(x_shear), NUM2DBL(y_shear), exception); rm_check_exception(exception, new_image, DestroyOnError); (void) DestroyExceptionInfo(exception); rm_ensure_result(new_image); return rm_image_new(new_image); } Case 1 Allocate memory

Slide 34

Slide 34 text

VALUE Image_shear(VALUE self, VALUE x_shear, VALUE y_shear) { Image *image, *new_image; ExceptionInfo *exception; image = rm_check_destroyed(self); exception = AcquireExceptionInfo(); new_image = ShearImage(image, NUM2DBL(x_shear), NUM2DBL(y_shear), exception); rm_check_exception(exception, new_image, DestroyOnError); (void) DestroyExceptionInfo(exception); rm_ensure_result(new_image); return rm_image_new(new_image); } Case 1 Allocate memory Destroy memory

Slide 35

Slide 35 text

VALUE Image_shear(VALUE self, VALUE x_shear, VALUE y_shear) { Image *image, *new_image; ExceptionInfo *exception; image = rm_check_destroyed(self); exception = AcquireExceptionInfo(); new_image = ShearImage(image, NUM2DBL(x_shear), NUM2DBL(y_shear), exception); rm_check_exception(exception, new_image, DestroyOnError); (void) DestroyExceptionInfo(exception); rm_ensure_result(new_image); return rm_image_new(new_image); } Case 1 Allocate memory Destroy memory Raise exception with unexpected value

Slide 36

Slide 36 text

VALUE Image_shear(VALUE self, VALUE x_shear, VALUE y_shear) { Image *image, *new_image; ExceptionInfo *exception; image = rm_check_destroyed(self); exception = AcquireExceptionInfo(); new_image = ShearImage(image, NUM2DBL(x_shear), NUM2DBL(y_shear), exception); rm_check_exception(exception, new_image, DestroyOnError); (void) DestroyExceptionInfo(exception); rm_ensure_result(new_image); return rm_image_new(new_image); } Case 1 Allocate memory Destroy memory Raise exception with unexpected value Unreachable by exception

Slide 37

Slide 37 text

Case 1: Exception causes Memory Leak image = Magick::Image.read('sample.png').first begin image.shear('x', 'y') rescue end

Slide 38

Slide 38 text

Case 2 static VALUE has_attribute(VALUE self, MagickBooleanType....[snip]) { Image *image; ExceptionInfo *exception; MagickBooleanType r; image = rm_check_destroyed(self); exception = AcquireExceptionInfo(); r = (attr_test)(image, exception); CHECK_EXCEPTION() return r ? Qtrue : Qfalse; }

Slide 39

Slide 39 text

static VALUE has_attribute(VALUE self, MagickBooleanType....[snip]) { Image *image; ExceptionInfo *exception; MagickBooleanType r; image = rm_check_destroyed(self); exception = AcquireExceptionInfo(); r = (attr_test)(image, exception); CHECK_EXCEPTION() return r ? Qtrue : Qfalse; } Case 2 Allocate memory

Slide 40

Slide 40 text

static VALUE has_attribute(VALUE self, MagickBooleanType....[snip]) { Image *image; ExceptionInfo *exception; MagickBooleanType r; image = rm_check_destroyed(self); exception = AcquireExceptionInfo(); r = (attr_test)(image, exception); CHECK_EXCEPTION() return r ? Qtrue : Qfalse; } Case 2 Allocate memory Missing destroy

Slide 41

Slide 41 text

Case 2: Missing destroy lead to Memory Leak image = Magick::Image.read('sample.png').first image.gray?

Slide 42

Slide 42 text

Case 3 Unnecessary clone API returns NEW image in allocated memory

Slide 43

Slide 43 text

Case 3: Memory Leak by incorrect API usage image = Magick::Image.read('sample.png').first image.sharpen_channel

Slide 44

Slide 44 text

Case 4

Slide 45

Slide 45 text

Case 4 Found memory leak in ImageMagick

Slide 46

Slide 46 text

Case 4

Slide 47

Slide 47 text

Case 4: Huge software has Memory Leak

Slide 48

Slide 48 text

How to detect memory leak

Slide 49

Slide 49 text

Profiler tool

Slide 50

Slide 50 text

Profiler tool Memory leak detected

Slide 51

Slide 51 text

Profiler tool

Slide 52

Slide 52 text

Profiler tool... •Detects at executed code only •unit-test •RSpec •Needs better coverage...

Slide 53

Slide 53 text

Code reading... •To find same pattern that profiler detects

Slide 54

Slide 54 text

SEGV

Slide 55

Slide 55 text

Closed 5 SEGVs 4 Fixed 1 Workaround

Slide 56

Slide 56 text

Workaround Case Help wanted

Slide 57

Slide 57 text

Workaround Case •Call back image processing progress to Ruby User's Ruby code RMagick ImageMagick Callback

Slide 58

Slide 58 text

ruby_stack_length() shows 51119378542 = 47 GB • Invalid stack length • GC running →SEGV

Slide 59

Slide 59 text

RMagick is better than ever.

Slide 60

Slide 60 text

Future...

Slide 61

Slide 61 text

ImageMagick 7 support

Slide 62

Slide 62 text

Preparing draft code... now

Slide 63

Slide 63 text

Plan 1: Split RMagick ver 4.x RMagick ver 3.x ver 3.0 ver 3.1 ver 2.16.1 ImageMagick 7 ImageMagick 6

Slide 64

Slide 64 text

Plan 2 : Mix both ImageMagick 7 ImageMagick 7 ImageMagick 6 Using many #if defined ...

Slide 65

Slide 65 text

Plan 3: Bundle both ImageMagick 7 ImageMagick 6

Slide 66

Slide 66 text

We welcome good ideas for the future

Slide 67

Slide 67 text

Announcement •RubyData Workshop (Tomorrow, 14:20 ~) •RubyKaigi 2019 Code Party (Tomorrow, 19:00 ~)

Slide 68

Slide 68 text

Thank you