refactoring to java 8
play

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


  1. Refactoring to Java 8 Trisha Gee (@trisha_gee) Developer & Technical Advocate, JetBrains

  2. Why Java 8?

  3. It’s Faster • Performance Improvements in Common Data Structures • Fork/Join Speed Improvements • Changes to Support Concurrency • …and more http://bit.ly/refJ8

  4. Easy to Parallelize

  5. Fewer Lines of Code

  6. New Solutions to Problems

  7. Minimizes Errors

  8. Safety Check

  9. Test Coverage

  10. Performance Tests

  11. Decide on the Goals

  12. Limit the Scope

  13. Morphia https:// github.com/mongodb/morphia

  14. Refactoring!

  15. Lambda Expressions

  16. Automatic Refactoring • Predicate • Comparator • Runnable • etc …

  17. Abstract classes

  18. Advanced Search

  19. Collections & Streams API

  20. Automatic Refactoring • For loop to collect • For loop to forEach • …with and without Streams

  21. Manual Refactoring

  22. Optional

  23. But what about performance?

  24. Lambdas

  25.  Anonymous Inner Classes vs Lambdas 180 160 140 120 100 Ops/ms 80 60 40 20 0  Anonymous Inner Class Lambda

  26. Performance of Capture  20 18 16 14 12 nsec/op 10 8 6 4 2 0  single thread max threads anonymous(static) anonymous(non-static) lambda http://www.oracle.com/technetwork/java/jvmls2013kuksen-2014088.pdf

  27.  Logging Performance 500,000 450,000 400,000 350,000 300,000 Ops/ms 250,000 200,000 150,000 100,000 50,000 0 Constant message Variable message  Direct call Lambda

  28. Streams vs Iteration

  29. Iterator vs Stream (1000 elements)  9 8 7 6 5 Ops/ms 4 3 2 1 0  for loop forEach()

  30. IterHelper  7000 6000 5000 4000 Ops/ms 3000 2000 1000 0  1 10 100 1000 original simplified refactored

  31. IterHelper  9 8 7 6 5 Ops/ms 4 3 2 1 0 1000 10000 100000  original simplified refactored

  32. BasicDAO – map & collect

  33. BasicDAO  25000 20000 15000 Ops/ms 10000 5000 0 1 10 100 1000  original simplified refactored

  34. DuplicatedAttributeNames – filter, map & collect

  35.  DuplicatedAttributeNames 1200 1000 800 Ops/ms 600 400 200 0  original refactored

  36. EntityScanner – forEach

  37.  EntityScanner 0.06 0.05 0.04 Ops/ms 0.03 0.02 0.01 0  original refactored

  38. DatastoreImpl – filter & forEach

  39.  DatastoreImpl 0.7 0.6 0.5 0.4 Ops/ms 0.3 0.2 0.1 0 original refactored 

  40. MappingValidator – single stream operation

  41.  MappingValidator 900 800 700 600 Ops/ms 500 400 300 200 100 0 EntityWithOneError EntityWith10Errors EntityWith20Errors original refactored 

  42. QueryImpl – multiple operations

  43.  QueryImpl 3500 3000 2500 2000 Ops/ms 1500 1000 500 0  original simplified refactored

  44. Going parallel

  45.  map() 0.9 0.8 Time Taken (seconds) 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 Serial Parallel  MacBook Surface

  46. findAny()  160 140 Time Taken (millis) 120 100 80 60 40 20 0 Serial Parallel  MacBook Surface

  47. Optional

  48.  Compare Constant Field Value with Null 350,000 300,000 250,000 200,000 Ops/ms 150,000 100,000 50,000 0 Value not null Value null  Null check Optional

  49.  Compare Variable Field Value with Null 300,000 250,000 200,000 Ops/ms 150,000 100,000 50,000 0 Value not null Value null  Null check Optional

  50. Summary

  51. Sometimes new idioms decrease clutter

  52. …but sometimes they don’t

  53. Sometimes the new features improve performance

  54.  Anonymous Inner Classes vs Lambdas 180 160 140 120 100 Ops/ms 80 60 40 20 0  Anonymous Inner Class Lambda

  55. …and sometimes they don’t

  56.  DuplicatedAttributeNames 1200 1000 800 Ops/ms 600 400 200 0  original refactored

  57. Sometimes a new feature makes life easier

  58. …sometimes not so much

  59. Some refactoring can safely be done automatically

  60. …often you need a human brain

  61. Conclusion

  62. Should you migrate your code to Java 8?

  63. It Depends

  64. Always remember what your goal is And compare results to it

  65. Understand what may impact performance And if in doubt, measure

  66. Code may magically improve Or you may expose areas for improvement

  67. Your tools can help you But you need to apply your brain too

  68. http://bit.ly/refJ8 @trisha_gee

Recommend


More recommend