Refactoring to Java 8 Trisha Gee (@trisha_gee) Developer & - - PowerPoint PPT Presentation

refactoring to java 8
SMART_READER_LITE
LIVE PREVIEW

Refactoring to Java 8 Trisha Gee (@trisha_gee) Developer & - - PowerPoint PPT Presentation

Refactoring to Java 8 Trisha Gee (@trisha_gee) Developer & Technical Advocate, JetBrains Why Java 8? Its Faster Performance Improvements in Common Data Structures Fork/Join Speed Improvements Changes to Support Concurrency


slide-1
SLIDE 1

Trisha Gee (@trisha_gee) Developer & Technical Advocate, JetBrains

Refactoring to Java 8

slide-2
SLIDE 2

Why Java 8?

slide-3
SLIDE 3

It’s Faster

  • Performance Improvements in Common Data

Structures

  • Fork/Join Speed Improvements
  • Changes to Support Concurrency
  • …and more

http://bit.ly/refJ8

slide-4
SLIDE 4

Easy to Parallelize

slide-5
SLIDE 5

Fewer Lines of Code

slide-6
SLIDE 6

New Solutions to Problems

slide-7
SLIDE 7

Minimizes Errors

slide-8
SLIDE 8

Safety Check

slide-9
SLIDE 9

Test Coverage

slide-10
SLIDE 10

Performance Tests

slide-11
SLIDE 11

Decide on the Goals

slide-12
SLIDE 12

Limit the Scope

slide-13
SLIDE 13

Morphia

https://github.com/mongodb/morphia

slide-14
SLIDE 14

Refactoring!

slide-15
SLIDE 15

Lambda Expressions

slide-16
SLIDE 16

Automatic Refactoring

  • Predicate
  • Comparator
  • Runnable
  • etc…
slide-17
SLIDE 17

Abstract classes

slide-18
SLIDE 18

Advanced Search

slide-19
SLIDE 19

Collections & Streams API

slide-20
SLIDE 20

Automatic Refactoring

  • For loop to collect
  • For loop to forEach
  • …with and without Streams
slide-21
SLIDE 21

Manual Refactoring

slide-22
SLIDE 22

Optional

slide-23
SLIDE 23
slide-24
SLIDE 24

But what about performance?

slide-25
SLIDE 25

Lambdas

slide-26
SLIDE 26

20 40 60 80 100 120 140 160 180

Ops/ms

Anonymous Inner Classes vs Lambdas

Anonymous Inner Class Lambda

 

slide-27
SLIDE 27

http://www.oracle.com/technetwork/java/jvmls2013kuksen-2014088.pdf

2 4 6 8 10 12 14 16 18 20

single thread max threads nsec/op

Performance of Capture

anonymous(static) anonymous(non-static) lambda

 

slide-28
SLIDE 28

 

50,000 100,000 150,000 200,000 250,000 300,000 350,000 400,000 450,000 500,000

Constant message Variable message Ops/ms

Logging Performance

Direct call Lambda

slide-29
SLIDE 29

Streams vs Iteration

slide-30
SLIDE 30

1 2 3 4 5 6 7 8 9

Ops/ms

Iterator vs Stream (1000 elements)

for loop forEach()

 

slide-31
SLIDE 31

1000 2000 3000 4000 5000 6000 7000

1 10 100 1000 Ops/ms

IterHelper

  • riginal

simplified refactored

 

slide-32
SLIDE 32

1 2 3 4 5 6 7 8 9

1000 10000 100000 Ops/ms

IterHelper

  • riginal

simplified refactored

 

slide-33
SLIDE 33

BasicDAO – map & collect

slide-34
SLIDE 34

5000 10000 15000 20000 25000

1 10 100 1000

Ops/ms

BasicDAO

  • riginal

simplified refactored

 

slide-35
SLIDE 35

DuplicatedAttributeNames – filter, map & collect

slide-36
SLIDE 36

200 400 600 800 1000 1200

Ops/ms

DuplicatedAttributeNames

  • riginal

refactored

 

slide-37
SLIDE 37

EntityScanner– forEach

slide-38
SLIDE 38

 

0.01 0.02 0.03 0.04 0.05 0.06

Ops/ms

EntityScanner

  • riginal

refactored

slide-39
SLIDE 39

DatastoreImpl – filter & forEach

slide-40
SLIDE 40

0.1 0.2 0.3 0.4 0.5 0.6 0.7

Ops/ms

DatastoreImpl

  • riginal

refactored

 

slide-41
SLIDE 41

MappingValidator – single stream operation

slide-42
SLIDE 42

 

100 200 300 400 500 600 700 800 900

EntityWithOneError EntityWith10Errors EntityWith20Errors Ops/ms

MappingValidator

  • riginal

refactored

slide-43
SLIDE 43

QueryImpl – multiple operations

slide-44
SLIDE 44

500 1000 1500 2000 2500 3000 3500

Ops/ms

QueryImpl

  • riginal

simplified refactored

 

slide-45
SLIDE 45

Going parallel

slide-46
SLIDE 46

0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 Serial Parallel Time Taken (seconds)

map()

MacBook Surface

 

slide-47
SLIDE 47

20 40 60 80 100 120 140 160 Serial Parallel Time Taken (millis)

findAny()

MacBook Surface

 

slide-48
SLIDE 48

Optional

slide-49
SLIDE 49

50,000 100,000 150,000 200,000 250,000 300,000 350,000

Value not null Value null Ops/ms

Compare Constant Field Value with Null

Null check Optional

 

slide-50
SLIDE 50

50,000 100,000 150,000 200,000 250,000 300,000

Value not null Value null Ops/ms

Compare Variable Field Value with Null

Null check Optional

 

slide-51
SLIDE 51

Summary

slide-52
SLIDE 52

Sometimes new idioms decrease clutter

slide-53
SLIDE 53
slide-54
SLIDE 54

…but sometimes they don’t

slide-55
SLIDE 55
slide-56
SLIDE 56

Sometimes the new features improve performance

slide-57
SLIDE 57

20 40 60 80 100 120 140 160 180

Ops/ms

Anonymous Inner Classes vs Lambdas

Anonymous Inner Class Lambda

 

slide-58
SLIDE 58

…and sometimes they don’t

slide-59
SLIDE 59

200 400 600 800 1000 1200

Ops/ms

DuplicatedAttributeNames

  • riginal

refactored

 

slide-60
SLIDE 60

Sometimes a new feature makes life easier

slide-61
SLIDE 61
slide-62
SLIDE 62

…sometimes not so much

slide-63
SLIDE 63
slide-64
SLIDE 64

Some refactoring can safely be done automatically

slide-65
SLIDE 65
slide-66
SLIDE 66

…often you need a human brain

slide-67
SLIDE 67
slide-68
SLIDE 68

Conclusion

slide-69
SLIDE 69

Should you migrate your code to Java 8?

slide-70
SLIDE 70

It Depends

slide-71
SLIDE 71

Always remember what your goal is

And compare results to it

slide-72
SLIDE 72

Understand what may impact performance

And if in doubt, measure

slide-73
SLIDE 73

Code may magically improve

Or you may expose areas for improvement

slide-74
SLIDE 74

Your tools can help you

But you need to apply your brain too

slide-75
SLIDE 75

http://bit.ly/refJ8

@trisha_gee