Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Optimize perl5 code for perfomance freaks

4,706 views

Published on

YAPC::EU 2015

Published in: Software
  • Be the first to comment

Optimize perl5 code for perfomance freaks

  1. 1. Optimize Perl5 code for performance freaks. Kenta SATO (@karupanerura) YAPC::EU 2015
  2. 2. Hello, YAPC::EU!
  3. 3. About me • Kenta SATO (@karupanerura) • Senior Engineer at Mobile Factory, Inc. • WebApp/iOS/Android/Operation/etc.. • Perl/Swift/Kotlin/Java/Crystal/XS/C99/etc.. • Gotanda.pm Author
  4. 4. CPAN (PAUSE: KARUPA) • Time::Strptime • Geo::Hex::V3::XS • Test::SharedObject • Plack::App::Vhost • etc..
  5. 5. Gotanda.pm
  6. 6. Gotanda.pm Granada.pm
  7. 7. Gotanda.pm Granada.pm a little different, but mostly same!
  8. 8. Nice to meet you :)
  9. 9. Optimize Perl5 code for performance freaks.
  10. 10. What about of performance?
  11. 11. Perl5 code performance
  12. 12. for performance freaks :)
  13. 13. I'll talk about … • Perl5 code • Regular expressions • Variable access • Conditional branch
  14. 14. I’ll *NOT* talk about … • Architecture • Database • I/O Multiplexing • Concurrency • etc..
  15. 15. $example = “strptime”
  16. 16. strptime(3) • Parse date time string using arbitrary format • e.g.) “%Y-%m-%d %H:%M:%S” => “2015-09-04 12:00:00”
  17. 17. Choices in Perl5 • DateTime::Format::Strptime (PP) • Time::Piece->strptime (XS) • POSIX::strptime (XS) • Time::Strptime (PP) • Time::Moment (fixed format but very fast)
  18. 18. Time::Strptime • Pure Perl implementation • If Time::TZOffset is installed, make it faster • Using Time::Local for caluclating epoch • Using system timezone • Using system locale
  19. 19. Benchmark DEMO
  20. 20. tp=Time::Piece(XS) ts=Time::Strptime(PP)
  21. 21. (In this case) Pure Perl is faster than XS!
  22. 22. NOTE: Benchmark environment • https://github.com/karupanerura/Time- Strptime/wiki/Benckmark-result
  23. 23. Why is Time::Strptime faster?
  24. 24. tuning tips • Suppress memory copy • Optimize regular expressions • Instance cache • String eval
  25. 25. Suppress memory copy • Use constant.pm if possible • for constant folding • Use perl 5.20 or later • String COW is available • experimental::refaliasing
  26. 26. experimental::refaliasing • Enables aliasing via assignment to references • Example: my $bar = $foo • Makes alias $bar to $foo • (pointer to same memory) • Available on perl 5.22 or later
  27. 27. experimental::refaliasing • But, Not fast… (why…?) • Copying is 63% faster than refaliasing • When copying|refaliasing IV value • I believe that refaliasing will be faster than copying in near future :)
  28. 28. Optimize regular expressions • Less slow expressions • Character Class < Quantifiers < Grouping • e.g.) Less matching patterns of regexp • e.g.) /A[0-5][0-9]z/m • This is 6% faster than /A[0-9]{2}z/m
  29. 29. Optimize regular expressions • Approach: Loose matching • Example: /A[0-9]{4}-[0-9]{2}-[0-9]{2}z/m • ↑ is 11%~16% faster than ↓ • /A[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01])z/m • Cons: Unsafe..
  30. 30. Instance cache • Time::Strptime reuses instances • Often times the same format is used • So cached instance can be used • Pay format parsing cost only once • Caching is enabled by default
  31. 31. String eval • Used to avoid conditional statements. • Pay the cost of generating code instead of at runtime • (DEMO)
  32. 32. String eval • Warning: very DANGEROUS!! • If you use it in your code, you should write many tests
  33. 33. CONCLUSION
  34. 34. CONCLUSION • XS module is generally fast • Optimized Pure Perl code can be faster • String eval is helpful, but beware!
  35. 35. Any questions? (Easy english please…) or mention to @karupanerura on Twitter

×