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.
Perl6 + JVM
tokuhirom
YAPC::Asia 2016
Self Introduction
Amon2,Harriet, Localizer,
Minilla, Test::Requires,
etc.
Perl6?
This is Rakudo Star, a useful,
usable Perl 6 distribution for
"early adopters".
曰く、
useful??
BTW,
Products
• nqp: Perl6 subset, to write Perl6 interpreter
• rakudo: One of Perl6 implementation
VM Support
• Parrot: VM for…
• MoarVM: VM for Perl6
• JVM: VM for Java ← Today’s topic
Why?
Because, I’m using
Java for $DAYJOB
You can use java
libraries, instead of poor
Perl6 libraries.
perl6-j is probably useful
for Java developers
で?
How do I install Perl6?
wget http://rakudo.org/
downloads/star/rakudo-
star-2015.07.tar.gz
tar xzvf rakudo-
star-2015.07.tar.gz
perl Configure.pl
—backends=jvm
—gen-nqp
--prefix=$HOME/perl6
• make
• make install
Easy
brew install rakudo-star
—with-jvm
Then…
Evaluation point as a
Web developer
• Start-up speed
• File access
• JSON processing
• HTTP client
• HTTP Server(Performance)
• Access to mysql
• use java lib...
Start-up speed
time ./install/bin/perl6-j 
-e 'say "Hello, YAPC”’
Hello, YAPC
./install/bin/perl6-j -e 'say "Hello, YAPC”'
17.32s user 0....
Slow!
Is JVM slow?
No!
> javac Hello.java
> time java Hello
Hello
java Hello 0.12s user 0.03s
system 116% cpu 0.130 total
time perl -E 'say "Hello, YAPC"'
Hello, YAPC
perl -E 'say "Hello, YAPC"' 0.04s
user 0.04s system 83% cpu 0.094
total
433 times slower
time /usr/local/bin/perl6-m -e 'say "hello"'
hello
/usr/local/bin/perl6-m -e 'say "hello"' 0.31s
user 0.04s system 97% cpu...
2. File Access
say slurp("/etc/
passwd")
It works.
3. JSON
use JSON::Tiny;
(bundled)
> use JSON::Tiny;
> from-json(‘[1,2,true]').perl
[1, 2, Bool::True]
to-json(
{“a"=>5.16,"b"=>false,"c"=>[1..5]}
)
4. HTTP Client
use LWP::Simple;
(Bundled)
> use LWP::Simple
> LWP::Simple.get("http://
64p.org")
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML
1.0 Transitional//EN" "htt...
useful!
But there’s no legacy
encoding support.
> LWP::Simple.get("http://
google.co.jp")
Unknown encoding 'shift_jis'
5. DB Access
DB Access uses
NativeCall
NativeCall?
FFI - Foreign Function
Interface
Call C API from Perl6
JNA on JVM
ちな Perl5 だと
require DynaLoader;
DynaLoader::dl_install_xsub(
"myfork",
DynaLoader::dl_find_symbol(
DynaLoader::dl_load_file("libc.so"),
...
Joke…
use FFI;
そして Perl6 へ。。
use DBDish;
my $dbh =
DBIish.connect('SQLite', :
database<thefile.sqlite3>);
cp /usr/local/Cellar/sqlite/
3.8.9/lib/libsqlite3.dylib ./
Supported DB:
PostgreSQL
MySQL
SQLite3
use DBIish;
my $dbh =
DBIish.connect('mysql', :host<127.0.0.
1>, :port(3306), :database<mysql>, :us
er<root>, :password(''...
How do I call JNI
methods?
sub mysql_affected_rows( OpaquePointer
$mysql_client )
returns int32
is native('libmysqlclient')
{ ... }
It’s easy!!!
You can call C APIs!
6. HTTP Server
HTTP::Easy::PSGI
(bundled)
use HTTP::Easy::PSGI;
my $http = HTTP::Easy::PSGI.new(:port(8080));
my $app = sub (%env) {
my $name = %env<QUERY_STRING> |...
with perl6-m
• ab -n 50 -c 1 http://127.0.0.1:8080/
• Requests per second: 5.95 [#/sec] (mean)
with perl6-j
• ab -n 50 -c 1 http://127.0.0.1:8080/
• Requests per second: 10.60 [#/sec] (mean)
10 threads + perl6-j
• ab -c 10 -n 1000 http://127.0.0.1:8080/
• Requests per second: 22.91 [#/sec] (mean)
Single thread + perl6-j
• single thread
• ab -c 10 -n 1000 http://127.0.0.1:8080/
• Requests per second: 17.84 [#/sec] (me...
ab -n 1000 -c 10
http://127.0.0.1:8080/
Time per request:
550.895 [ms] (mean)
(It doesn’t support
concurrency)
7. Call Java methods
use
java::util::zip::CRC32:fro
m<
java>;
my $crc = CRC32.new();
for 'Hello, Java'.encode('utf-8') {
$crc.update($_)
}
$crc.getValue.say;
it works.
8. GUI
my $frame =
JFrame.new("Helloworl
d");
$frame.setDefaultClose
Operation(1);
my $label = JLabel.new("Hello,
world");
$frame.getContentPane.add($labe
l);
$frame.pack();
$frame.setVisible(True)
Demo
use
My::Own::Class:from<
Java>:jar<hoge.jar>
Conclusion
There is some issues.
But practical than I
thought
Upcoming SlideShare
Loading in …5
×

Perl6 meets JVM

3,371 views

Published on

YAPC::Asia 2015

Published in: Engineering

Perl6 meets JVM

  1. 1. Perl6 + JVM tokuhirom YAPC::Asia 2016
  2. 2. Self Introduction
  3. 3. Amon2,Harriet, Localizer, Minilla, Test::Requires, etc.
  4. 4. Perl6?
  5. 5. This is Rakudo Star, a useful, usable Perl 6 distribution for "early adopters". 曰く、
  6. 6. useful??
  7. 7. BTW,
  8. 8. Products • nqp: Perl6 subset, to write Perl6 interpreter • rakudo: One of Perl6 implementation
  9. 9. VM Support • Parrot: VM for… • MoarVM: VM for Perl6 • JVM: VM for Java ← Today’s topic
  10. 10. Why?
  11. 11. Because, I’m using Java for $DAYJOB
  12. 12. You can use java libraries, instead of poor Perl6 libraries.
  13. 13. perl6-j is probably useful for Java developers
  14. 14. で?
  15. 15. How do I install Perl6?
  16. 16. wget http://rakudo.org/ downloads/star/rakudo- star-2015.07.tar.gz
  17. 17. tar xzvf rakudo- star-2015.07.tar.gz
  18. 18. perl Configure.pl —backends=jvm —gen-nqp --prefix=$HOME/perl6
  19. 19. • make • make install
  20. 20. Easy
  21. 21. brew install rakudo-star —with-jvm
  22. 22. Then…
  23. 23. Evaluation point as a Web developer
  24. 24. • Start-up speed • File access • JSON processing • HTTP client • HTTP Server(Performance) • Access to mysql • use java libraries
  25. 25. Start-up speed
  26. 26. time ./install/bin/perl6-j -e 'say "Hello, YAPC”’ Hello, YAPC ./install/bin/perl6-j -e 'say "Hello, YAPC”' 17.32s user 0.51s system 254% cpu 6.995 total
  27. 27. Slow!
  28. 28. Is JVM slow?
  29. 29. No!
  30. 30. > javac Hello.java > time java Hello Hello java Hello 0.12s user 0.03s system 116% cpu 0.130 total
  31. 31. time perl -E 'say "Hello, YAPC"' Hello, YAPC perl -E 'say "Hello, YAPC"' 0.04s user 0.04s system 83% cpu 0.094 total
  32. 32. 433 times slower
  33. 33. time /usr/local/bin/perl6-m -e 'say "hello"' hello /usr/local/bin/perl6-m -e 'say "hello"' 0.31s user 0.04s system 97% cpu 0.360 total
  34. 34. 2. File Access
  35. 35. say slurp("/etc/ passwd")
  36. 36. It works.
  37. 37. 3. JSON
  38. 38. use JSON::Tiny; (bundled)
  39. 39. > use JSON::Tiny; > from-json(‘[1,2,true]').perl [1, 2, Bool::True]
  40. 40. to-json( {“a"=>5.16,"b"=>false,"c"=>[1..5]} )
  41. 41. 4. HTTP Client
  42. 42. use LWP::Simple; (Bundled)
  43. 43. > use LWP::Simple > LWP::Simple.get("http:// 64p.org") <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o$ g/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
  44. 44. useful!
  45. 45. But there’s no legacy encoding support.
  46. 46. > LWP::Simple.get("http:// google.co.jp") Unknown encoding 'shift_jis'
  47. 47. 5. DB Access
  48. 48. DB Access uses NativeCall
  49. 49. NativeCall?
  50. 50. FFI - Foreign Function Interface
  51. 51. Call C API from Perl6
  52. 52. JNA on JVM
  53. 53. ちな Perl5 だと
  54. 54. require DynaLoader; DynaLoader::dl_install_xsub( "myfork", DynaLoader::dl_find_symbol( DynaLoader::dl_load_file("libc.so"), 'fork' ), ); myfork();
  55. 55. Joke…
  56. 56. use FFI;
  57. 57. そして Perl6 へ。。
  58. 58. use DBDish;
  59. 59. my $dbh = DBIish.connect('SQLite', : database<thefile.sqlite3>);
  60. 60. cp /usr/local/Cellar/sqlite/ 3.8.9/lib/libsqlite3.dylib ./
  61. 61. Supported DB: PostgreSQL MySQL SQLite3
  62. 62. use DBIish; my $dbh = DBIish.connect('mysql', :host<127.0.0. 1>, :port(3306), :database<mysql>, :us er<root>, :password('')); my $sth = $dbh.prepare('SHOW TABLES'); $sth.execute(); my $arrayref = $sth.fetchall_arrayref(); $arrayref.perl.say;
  63. 63. How do I call JNI methods?
  64. 64. sub mysql_affected_rows( OpaquePointer $mysql_client ) returns int32 is native('libmysqlclient') { ... }
  65. 65. It’s easy!!!
  66. 66. You can call C APIs!
  67. 67. 6. HTTP Server
  68. 68. HTTP::Easy::PSGI (bundled)
  69. 69. use HTTP::Easy::PSGI; my $http = HTTP::Easy::PSGI.new(:port(8080)); my $app = sub (%env) { my $name = %env<QUERY_STRING> || "World"; return [ 200, [ 'Content-Type' => 'text/plain' ], [ "Hello $name" ] ]; } $http.handle($app);
  70. 70. with perl6-m • ab -n 50 -c 1 http://127.0.0.1:8080/ • Requests per second: 5.95 [#/sec] (mean)
  71. 71. with perl6-j • ab -n 50 -c 1 http://127.0.0.1:8080/ • Requests per second: 10.60 [#/sec] (mean)
  72. 72. 10 threads + perl6-j • ab -c 10 -n 1000 http://127.0.0.1:8080/ • Requests per second: 22.91 [#/sec] (mean)
  73. 73. Single thread + perl6-j • single thread • ab -c 10 -n 1000 http://127.0.0.1:8080/ • Requests per second: 17.84 [#/sec] (mean)
  74. 74. ab -n 1000 -c 10 http://127.0.0.1:8080/
  75. 75. Time per request: 550.895 [ms] (mean)
  76. 76. (It doesn’t support concurrency)
  77. 77. 7. Call Java methods
  78. 78. use java::util::zip::CRC32:fro m< java>; my $crc = CRC32.new();
  79. 79. for 'Hello, Java'.encode('utf-8') { $crc.update($_) }
  80. 80. $crc.getValue.say;
  81. 81. it works.
  82. 82. 8. GUI
  83. 83. my $frame = JFrame.new("Helloworl d"); $frame.setDefaultClose Operation(1);
  84. 84. my $label = JLabel.new("Hello, world"); $frame.getContentPane.add($labe l);
  85. 85. $frame.pack(); $frame.setVisible(True)
  86. 86. Demo
  87. 87. use My::Own::Class:from< Java>:jar<hoge.jar>
  88. 88. Conclusion
  89. 89. There is some issues. But practical than I thought

×