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 performance freaks.
Kenta SATO (@karupanerura)
YAPC::EU 2015
Hello, YAPC::EU!
About me
• Kenta SATO (@karupanerura)
• Senior Engineer at Mobile Factory, Inc.
• WebApp/iOS/Android/Operation/etc..
• Per...
CPAN (PAUSE: KARUPA)
• Time::Strptime
• Geo::Hex::V3::XS
• Test::SharedObject
• Plack::App::Vhost
• etc..
Gotanda.pm
Gotanda.pm
Granada.pm
Gotanda.pm
Granada.pm
a little different,
but mostly same!
Nice to meet you :)
Optimize Perl5 code
for performance freaks.
What about of
performance?
Perl5 code performance
for performance freaks :)
I'll talk about …
• Perl5 code
• Regular expressions
• Variable access
• Conditional branch
I’ll *NOT* talk about …
• Architecture
• Database
• I/O Multiplexing
• Concurrency
• etc..
$example = “strptime”
strptime(3)
• Parse date time string using arbitrary format
• e.g.) “%Y-%m-%d %H:%M:%S”
=> “2015-09-04 12:00:00”
Choices in Perl5
• DateTime::Format::Strptime (PP)
• Time::Piece->strptime (XS)
• POSIX::strptime (XS)
• Time::Strptime (P...
Time::Strptime
• Pure Perl implementation
• If Time::TZOffset is installed, make it faster
• Using Time::Local for calucla...
Benchmark
DEMO
tp=Time::Piece(XS)
ts=Time::Strptime(PP)
(In this case)
Pure Perl is
faster than XS!
NOTE:
Benchmark environment
• https://github.com/karupanerura/Time-
Strptime/wiki/Benckmark-result
Why is Time::Strptime faster?
tuning tips
• Suppress memory copy
• Optimize regular expressions
• Instance cache
• String eval
Suppress memory copy
• Use constant.pm if possible
• for constant folding
• Use perl 5.20 or later
• String COW is availab...
experimental::refaliasing
• Enables aliasing via assignment to references
• Example: my $bar = $foo
• Makes alias $bar to ...
experimental::refaliasing
• But, Not fast… (why…?)
• Copying is 63% faster than refaliasing
• When copying|refaliasing IV ...
Optimize regular expressions
• Less slow expressions
• Character Class < Quantifiers < Grouping
• e.g.) Less matching patte...
Optimize regular expressions
• Approach: Loose matching
• Example: /A[0-9]{4}-[0-9]{2}-[0-9]{2}z/m
• ↑ is 11%~16% faster t...
Instance cache
• Time::Strptime reuses instances
• Often times the same format is used
• So cached instance can be used
• ...
String eval
• Used to avoid conditional statements.
• Pay the cost of generating code instead of at
runtime
• (DEMO)
String eval
• Warning: very DANGEROUS!!
• If you use it in your code, you should write
many tests
CONCLUSION
CONCLUSION
• XS module is generally fast
• Optimized Pure Perl code can be faster
• String eval is helpful, but beware!
Any questions?
(Easy english please…)
or mention to
@karupanerura
on Twitter
You’ve finished this document.
Download and read it offline.
Upcoming SlideShare
Aniki has come
Next
Upcoming SlideShare
Aniki has come
Next
Download to read offline and view in fullscreen.

Share

Optimize perl5 code for perfomance freaks

Download to read offline

YAPC::EU 2015

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

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
  • hkoba

    Sep. 4, 2015

YAPC::EU 2015

Views

Total views

4,957

On Slideshare

0

From embeds

0

Number of embeds

3,432

Actions

Downloads

5

Shares

0

Comments

0

Likes

1

×