Optimize perl5 code for perfomance freaks

of 35 /35
Optimize Perl5 code for performance freaks. Kenta SATO (@karupanerura) YAPC::EU 2015

Transcript of Optimize perl5 code for perfomance freaks

Page 1: Optimize perl5 code for perfomance freaks

Optimize Perl5 code for performance freaks. Kenta SATO (@karupanerura) YAPC::EU 2015

Page 2: Optimize perl5 code for perfomance freaks

Hello, YAPC::EU!

Page 3: Optimize perl5 code for perfomance freaks

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

Page 4: Optimize perl5 code for perfomance freaks

CPAN (PAUSE: KARUPA)

• Time::Strptime

• Geo::Hex::V3::XS

• Test::SharedObject

• Plack::App::Vhost

• etc..

Page 5: Optimize perl5 code for perfomance freaks

Gotanda.pm

Page 6: Optimize perl5 code for perfomance freaks

Gotanda.pm Granada.pm

Page 7: Optimize perl5 code for perfomance freaks

Gotanda.pm Granada.pm

a little different, but mostly same!

Page 8: Optimize perl5 code for perfomance freaks

Nice to meet you :)

Page 9: Optimize perl5 code for perfomance freaks

Optimize Perl5 code for performance freaks.

Page 10: Optimize perl5 code for perfomance freaks

What about of performance?

Page 11: Optimize perl5 code for perfomance freaks

Perl5 code performance

Page 12: Optimize perl5 code for perfomance freaks

for performance freaks :)

Page 13: Optimize perl5 code for perfomance freaks

I'll talk about …

• Perl5 code

• Regular expressions

• Variable access

• Conditional branch

Page 14: Optimize perl5 code for perfomance freaks

I’ll *NOT* talk about …

• Architecture

• Database

• I/O Multiplexing

• Concurrency

• etc..

Page 15: Optimize perl5 code for perfomance freaks

$example = “strptime”

Page 16: Optimize perl5 code for perfomance freaks

strptime(3)

• Parse date time string using arbitrary format

• e.g.) “%Y-%m-%d %H:%M:%S” => “2015-09-04 12:00:00”

Page 17: Optimize perl5 code for perfomance freaks

Choices in Perl5

• DateTime::Format::Strptime (PP)

• Time::Piece->strptime (XS)

• POSIX::strptime (XS)

• Time::Strptime (PP)

• Time::Moment (fixed format but very fast)

Page 18: Optimize perl5 code for perfomance freaks

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

Page 19: Optimize perl5 code for perfomance freaks

Benchmark

DEMO

Page 20: Optimize perl5 code for perfomance freaks

tp=Time::Piece(XS) ts=Time::Strptime(PP)

Page 21: Optimize perl5 code for perfomance freaks

(In this case) Pure Perl is faster than XS!

Page 22: Optimize perl5 code for perfomance freaks

NOTE: Benchmark environment• https://github.com/karupanerura/Time-

Strptime/wiki/Benckmark-result

Page 23: Optimize perl5 code for perfomance freaks

Why is Time::Strptime faster?

Page 24: Optimize perl5 code for perfomance freaks

tuning tips

• Suppress memory copy

• Optimize regular expressions

• Instance cache

• String eval

Page 25: Optimize perl5 code for perfomance freaks

Suppress memory copy

• Use constant.pm if possible

• for constant folding

• Use perl 5.20 or later

• String COW is available

• experimental::refaliasing

Page 26: Optimize perl5 code for perfomance freaks

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

Page 27: Optimize perl5 code for perfomance freaks

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 :)

Page 28: Optimize perl5 code for perfomance freaks

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

Page 29: Optimize perl5 code for perfomance freaks

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..

Page 30: Optimize perl5 code for perfomance freaks

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

Page 31: Optimize perl5 code for perfomance freaks

String eval

• Used to avoid conditional statements.

• Pay the cost of generating code instead of at runtime

• (DEMO)

Page 32: Optimize perl5 code for perfomance freaks

String eval

• Warning: very DANGEROUS!!

• If you use it in your code, you should write many tests

Page 33: Optimize perl5 code for perfomance freaks

CONCLUSION

Page 34: Optimize perl5 code for perfomance freaks

CONCLUSION

• XS module is generally fast

• Optimized Pure Perl code can be faster

• String eval is helpful, but beware!

Page 35: Optimize perl5 code for perfomance freaks

Any questions? (Easy english please…)

or mention to @karupanerura

on Twitter