optimize perl5 code for perfomance freaks
TRANSCRIPT
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..
• Perl/Swift/Kotlin/Java/Crystal/XS/C99/etc..
• Gotanda.pm Author
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 (PP)
• Time::Moment (fixed format but very fast)
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
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 available
• experimental::refaliasing
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
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 :)
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
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..
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
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