JRubyãããã - èµ·åæãªãã·ã§ã³(ã³ãã³ãã©ã¤ã³ãªãã·ã§ã³)
ã¤ã³ã¹ãã¼ã«ç·¨(http://d.hatena.ne.jp/yokolet/20100529#1275156722)ã«æ¸ããã©ãã(ãããããã¨ããã«ä»ã®ã¤ã³ã¹ãã¼ã«æ¹æ³ããããããããNetBeansã«ä»ãã¦ããã¨ã)ã§ãJRubyãã¤ã³ã¹ãã¼ã«ã§ããããæ¬¡ã¯å½ç¶ã使ã£ã¡ãããã¼ï¼ã§ããããã§ã¯ããã£ããã
ãã®1 åãï¼
å®çªãHello Worldã®åã«ãã£ã¦ããã®ã¯ãã»ãã¨ã¼ã«JRubyãåãã¦ããï¼ã確èªãããã¨ãã§ãããHello Worldãåããªãã£ãã¨ãã«ããªã«ãã¾ããããç¹å®ããããã«ãå¿
è¦ãã®ã§ãæåã¯ã³ã¬ã
jrubyã³ãã³ãã¸ã®ãã¹ã¯éãã¦ããããï¼ãããã
jruby -v jruby 1.5.0 (ruby 1.8.7 patchlevel 249) (2010-05-12 6769999) (Java HotSpot(TM) Client VM 1.5.0_24) [i386-java]
ãã¼ã¸ã§ã³çªå·ãã§ãããªï¼ã¾ãã«ãJDKã®ãã¼ã¸ã§ã³ã¨ç¸æ§ãæªãã¦åããªããã¨ãããã¿ããã§ãããã§ã«ä¸å¹´åã®è©±ããªã®ã§ãç¶æ³ã¯å¤ãã£ã¦ããã¨æãã¾ããã
http://kenai.com/projects/jruby/pages/JRubyOnUbuntu
http://kenai.com/projects/jruby/pages/JRubyOnHPUX11_23
ãªãããããã®ã§ãJRubyãåãã¦ãããã©ããã®ç¢ºèªããå¿ããªãã
ãã®2 å®çªãHello Worldã¯ï¼
å®çªãå®çªããããã³ã¬ããããªããããã°ã©ãã³ã°ã¯å§ã¾ããªãããã¼ãJRubyã®å ´åãåºæ¬çã«Rubyã¨åããªã®ã§ã
jruby -e"puts 'Hello World'" Hello World
ã¨ãã
jruby -e"puts \"Hello World\"" Hello World
ã¨ããã¦ã-eãªãã·ã§ã³ãã¤ããã°"(double quote)ã§å²ã¾ããä¸ãè©ä¾¡ãã¦ããããã¨ãæ¥æ¬èªã ã£ã¦ã
jruby -e"puts 'JRubyããããã使ãããï¼'" JRubyããããã使ãããï¼
ã¨ããããã«ãã¡ããã¨è¡¨ç¤ºãã¦ããã¾ãã
ããä¸ã¤ãRubyãªãã§ã¯ã®irbã使ã£ã¦Hello Worldãã¦ã¿ãã¨ããã®ãããã¡ããããã¾ããJRubyã«ã¯irbã®ä»ã«jirbã¨ããæåã«"j"ãã¤ããã³ãã³ããããã®ã§ãããä¸çªãã使ããã¦ããã®ã¯ "jruby -S irb" ã§ã¯ãªããã¨ããªããã¨ããã¨ãjrubyã使ã£ã¦ããã¨ãããã¨ããããããããããããããªãªãã·ã§ã³ãæå®ãããã¨ãã¯ã³ã¬ãä¸çªã ããã§ãã
jruby -S irb irb(main):001:0> puts 'JRubyããããã使ãããï¼' JRubyããããã使ãããï¼ => nil irb(main):002:0>
ãã®3ããªãã·ã§ã³ãããã
ã¨ãããã¾ã§ã§ããã§ã«3ã¤ã®ã³ãã³ãã©ã¤ã³ãªãã·ã§ã³-v, -e -Sãåºã¦ãã¾ãããããä»ã«ã¯ï¼ãããããã©ãã¿ã©ãæãã¦ãããã¾ãããããªã®ãJRubyãRubyã¨ã¯ã¡ãã£ã¨éãã¨ããã§ããã-hãªãã·ã§ã³ãå©ãã¦ã¿ãã¨ããã
jruby -h Usage: jruby [switches] [--] [programfile] [arguments] -0[octal] specify record separator (\0, if no argument) -a autosplit mode with -n or -p (splits $_ into $F) -b benchmark mode, times the script execution -c check syntax only -Cdirectory cd to directory, before executing your script -d set debugging flags (set $DEBUG to true) -e 'command' one line of script. Several -e's allowed. Omit [programfile] -Fpattern split() pattern for autosplit (-a) -i[extension] edit ARGV files in place (make backup if extension supplied) -Idirectory specify $LOAD_PATH directory (may be used more than once) -J[java option] pass an option on to the JVM (e.g. -J-Xmx512m) use --properties to list JRuby properties run 'java -help' for a list of other Java options -Kkcode specifies code-set (e.g. -Ku for Unicode, -Ke for EUC and -Ks for SJIS) -l enable line ending processing -n assume 'while gets(); ... end' loop around your script -p assume loop like -n but print line also like sed -rlibrary require the library, before executing your script -s enable some switch parsing for switches after script name -S look for the script in bin or using PATH environment variable -T[level] turn on tainting checks -v print version number, then turn on verbose mode -w turn warnings on for your script -W[level] set warning level; 0=silence, 1=medium, 2=verbose (default) -x[directory] strip off text before #!ruby line and perhaps cd to directory -X[option] enable extended option (omit option to list) -y enable parsing debug output --copyright print the copyright --debug sets the execution mode most suitable for debugger functionality --jdb runs JRuby process under JDB --properties List all configuration Java properties (pass -J-Dproperty=value) --sample run with profiling using the JVM's sampling profiler --client use the non-optimizing "client" JVM (improves startup; default) --server use the optimizing "server" JVM (improves perf) --manage enable remote JMX management and monitoring of the VM and JRuby --headless do not launch a GUI window, no matter what --1.8 specify Ruby 1.8.x compatibility (default) --1.9 specify Ruby 1.9.x compatibility --bytecode show the JVM bytecode produced by compiling specified code --version print the version
ãããããã£ããããããRubyãªäººã«ã¯ã馴æã¿ã®ãªãã·ã§ã³ãååãããããããã©ãè¦ããã¨ããªããªãã·ã§ã³ãçµæ§ãããããã¨ããããå®ã¯ããã ãããããªãããã£ã¨ãããã§ããããã²ã¨ã¤ã®ãã«ããªãã·ã§ã³ --propertiesãä»ãã¦JRubyãèµ·åãã¦ã¿ãã¨ãã
jruby --properties These properties can be used to alter runtime behavior for perf or compatibility. Specify them by passing -J-D= COMPILER SETTINGS: jruby.compile.mode=JIT|FORCE|OFF Set compilation mode. JIT is default; FORCE compiles all, OFF disables jruby.compile.fastest=true|false (EXPERIMENTAL) Turn on all experimental compiler optimizations jruby.compile.frameless=true|false (EXPERIMENTAL) Turn on frameless compilation where possible jruby.compile.positionless=true|false (EXPERIMENTAL) Turn on compilation that avoids updating Ruby position info. Default is false jruby.compile.threadless=true|false (EXPERIMENTAL) Turn on compilation without polling for "unsafe" thread events. Default is false jruby.compile.fastops=true|false (EXPERIMENTAL) Turn on fast operators for Fixnum. Default is false jruby.compile.fastcase=true|false (EXPERIMENTAL) Turn on fast case/when for all-Fixnum whens. Default is false jruby.compile.chainsize= Set the number of lines at which compiled bodies are "chained". Default is 500 jruby.compile.lazyHandles=true|false Generate method bindings (handles) for compiled methods lazily. Default is false. jruby.compile.peephole=true|false Enable or disable peephole optimizations. Default is true (on). JIT SETTINGS: jruby.jit.threshold= Set the JIT threshold to the specified method invocation count. Default is 50. jruby.jit.max= Set the max count of active methods eligible for JIT-compilation. Default is 4096 per runtime. A value of 0 disables JIT, -1 disables max. jruby.jit.maxsize= Set the maximum full-class byte size allowed for jitted methods. Default is 10000. jruby.jit.logging=true|false Enable JIT logging (reports successful compilation). Default is false jruby.jit.logging.verbose=true|false Enable verbose JIT logging (reports failed compilation). Default is false jruby.jit.logEvery= Log a message every n methods JIT compiled. Default is 0 (off). jruby.jit.exclude= Exclude methods from JIT by class/module short name, c/m::method_name, or -::method_name for anon/singleton classes/modules. Comma-delimited. jruby.jit.cache=true|false Cache jitted method in-memory bodies across runtimes and loads. Default is true. jruby.jit.codeCache= Save jitted methods to as they're compiled, for future runs. NATIVE SUPPORT: jruby.native.enabled=true|false Enable/disable native extensions (like JNA for non-Java APIs; Default is true (This affects all JRuby instances in a given JVM) jruby.native.verbose=true|false Enable verbose logging of native extension loading. Default is false. jruby.fork.enabled=true|false (EXPERIMENTAL, maybe dangerous) Enable fork(2) on platforms that support it. THREAD POOLING: jruby.thread.pool.enabled=true|false Enable reuse of native backing threads via a thread pool. Default is false. jruby.thread.pool.min= The minimum number of threads to keep alive in the pool. Default is 0. jruby.thread.pool.max= The maximum number of threads to allow in the pool. Default is unlimited. jruby.thread.pool.ttl=
ãããã®ãªãã·ã§ã³ã¯
http://kenai.com/projects/jruby/pages/JRubyOptions
ã«ä¸è¦§ãä½ã£ã¦ããã¦ããã®ã§ãããã£ã¨çºãã¦ãã ããã¾ãããã®ä¸è¦§ã¯RedBridge(JRuby Embed)ã«ã³ã³ãã£ã°ç¨ã®ã¡ã½ããããããã追å ããã¨ãã«ãã½ã¼ã¹ã³ã¼ããçºãã¤ã¤ã¾ã¨ãããã®ã§ããã¨ã«ããããããããããã£ã¦ããªã«ãã©ãã ã£ããå¿ãã¦ãã¾ãã®ã§ããã ããã¹ã¦ã®ãªãã·ã§ã³ãã¾ã¨ãã«åãã¯ã±ã§ã¯ãªãã¦ãããªã¼ãexperimentalãªãªãã·ã§ã³ãæ··ãã£ã¦ãããããã®ã§ãä¸ã«ã¯"å¹ããï¼ï¼"ã¨ããã®ãããããã
ãªãã·ã§ã³ãæ¹ãã¦çºããã¨ãã¼ããããã®ããJRubyã¯ãã£ã±ãJavaã®ã¢ããªã±ã¼ã·ã§ã³ãªãã ã¨ãããã¨ãJavaã®ã·ã¹ãã ããããã£ãã³ãã³ãã©ã¤ã³ãªãã·ã§ã³ã使ã£ã¦JVMã®ã³ã³ãã£ã°ããããã¨ããJavaãªäººã«ã¯å½ããåãããæ¹æ³ãã§ãRubyãªäººã«ã¯æªç¥ã®ä¸ç(?)ãªæ¹æ³ãããã¤ãããã¾ãããããJavaãªãªãã·ã§ã³ä½¿ãããªãã¯JRuby使ãããªããã¹ãã¼ãã¢ããã®ããã«ã¯å¿ é ãªã®ã§ããã¹ã¿ã¼ãã¹ããã ã£ã¦ãæã£ãã§ãããHello Worldãirbèµ·åãã¦ã"JRubyé ã"ã£ã¦ãMRIã«æ¯ã¹ããã:)
ãã®4 JRubyã¹ãã¼ãã¢ããã®ãªãã·ã§ã³
JRubyã®åèªã®ããã念ã®ãããè¨ã£ã¦ãããã¨æãã¾ãããJRubyã¯ãããªãã®ã¢ããªã±ã¼ã·ã§ã³ãåããä¸ã§ã¯å®è¡é度ã¨ããç¹ã§MRIã«æ±ºãã¦å¼ãã¯ã¨ãã¾ãã(ãã£ã±ã)ãèµ·åã¯ç¢ºãã«MRIã«æ¯ã¹ã¦é
ãããã©ãä¸åº¦ç«ã¡ä¸ãã£ã¦ãã¾ãã°æ©ããã§ãããã®ãããã¯Hello Worldãããããã¾ããããã
ããã§ããã£ã±ãå®è¡é度ã¯éãã«ããããã¨ã¯ãªããã"ãã£ã¨éãï¼"ã¨ããé¡ãã¯ä¸å¤ãªã®ã§ããã¥ã¼ã³ã¢ããæ¹æ³ãããã¤ãã
Javaãªäººã«ã¯ã馴æã¿ãJVMã«ã¯clientã¢ã¼ãã¨serverã¢ã¼ããããã¾ãããããã²ãclientã¢ã¼ãã§ã試ãããããã¯ç§èªèº«ãçµé¨ããã®ã§ãããããããªãã ãJDK 6ã ã¨ãã§ã¦ã«é ããJDK 5ãªããã£ã¨éãã®ã«ãããä½ã ãã®éãã¯ï¼ï¼ã¨æã£ãããJDK 5ã¯clientãããã©ã«ãã§JDK 6ã¯serverãããã©ã«ãã«ãªã£ã¦ããã®ã§ããããã®å¾ãclient/serverã§ã®ããã©ã¼ãã³ã¹ã®éãã¯IRCã§ã話é¡ã«ãªã£ã¦ãã¦ã"ãã£ã±ãã"ã¨æã£ãããã®è©±ãã¯Charles Nutteræ°ã®ããã°
Headius: JRuby Startup Time Tips
ã«ãç»å ´ãã¡ãªã¿ã«ç§ã®OS Xã§è©¦ãããã
time jruby -J-server -ve "require 'rubygems'; require 'columnize'" jruby 1.5.0 (ruby 1.8.7 patchlevel 249) (2010-05-12 6769999) (Java HotSpot(TM) Server VM 1.5.0_24) [i386-java] real 0m1.741s user 0m2.214s sys 0m0.208s time jruby -J-client -ve "require 'rubygems'; require 'columnize'" jruby 1.5.0 (ruby 1.8.7 patchlevel 249) (2010-05-12 6769999) (Java HotSpot(TM) Client VM 1.5.0_24) [i386-java] real 0m1.315s user 0m1.090s sys 0m0.149s
ãªæãã§ããã£ã±ãclientãé¸ã¶ã¨éããã¨ããã§ãOS Xã®JDK 6ã 㨠-clientãæå®ãã¦ã常ã«server JVMã§ããèµ·åããªãã®ã¯ãã°ãªã®ãããï¼Ubuntuã®OpenJDK 6ã¯ã¡ããã¨åãæ¿ãã£ãããã©ããã
- JITã¯OFFã®æ¹ãããã¨ãããã
Nutteræ°ã®ããã°ã«ããããã¥ã¼ã³ããããããããä¸ã¤ãJRubyã®JIT compilerã¯OFFã«ããæ¹ãéãå ´åãããããªããã¨ããã¨ãcompileã®ããã»ã¹ãé ããããJITãå¹ããããªããã°ã©ã ã®å®è¡ãªãJITãONã«ãã¦ãããæ¹ãã¿ããã ããã©ããããããªãã¨ãã¯OFFã®ã»ããããããã-X-Cã§OFFã«ã§ãã¾ãããã ã"require 'rubygems'; require 'columnize'"ã®ç¨åº¦ã ã¨éãã¯ã³ã¿ãã¼ã
- ObjectSpaceãOFFã«ããã¨éããªããã
次ã¯ãã¡ãã«ãããã¥ã¼ã³ã¢ããããã
http://kenai.com/projects/jruby/pages/PerformanceTuning
ObjectSpaceã¨ãããã®ãç§ã¯ãã¾ã使ã£ããã¨ããªãã®ã§ãããRubyãªäººã¯ãã使ãã®ããªï¼ãRubyéå¼ãã¬ã·ããã ã¨ãã¬ã·ã198ã®âãªãã¸ã§ã¯ããåå¨ãã¦ãããã調ã¹ããâã§ä½¿ã£ã¦ã¾ãããã¾ãå¿ è¦ç¡ãå ´åãJRubyã¯-X-Oã§ç¡å¹ã«ã§ãã¾ãããã£ãã®JIT OFFã¨ä¸ç·ã«æå®ããã¨ããã
time jruby -J-client -X-C -X-O -ve "require 'rubygems'; require 'columnize'" jruby 1.5.0 (ruby 1.8.7 patchlevel 249) (2010-05-12 6769999) (Java HotSpot(TM) Client VM 1.5.0_24) [i386-java] real 0m1.217s user 0m1.041s sys 0m0.141s
æ°æã¡ãéããªã£ã¦ãããããã¡ãªã¿ã«ãPerformance Tuningã®ããã¥ã¡ã³ãã«ããThread Poolingã®æ©è½ã¯ã¢ããªã±ã¼ã·ã§ã³ã«ãããããconcurrentã§ã³ãã°ããã°ãã¦ãããªãã¨ãããªãã¢ããªãããªãã¨ããã®è¾ºã¯Javaã®ãã«ãã¹ã¬ããã®ãã¦ãã¦ã調ã¹ã¦ãã使ã£ãæ¹ãããããã--fastãªãã·ã§ã³ãå ´åã«ãããããé ããªããã¨ããã£ã¦ãããã§ã¼ãªãªãã·ã§ã³ã
- ã¨ãããã -Xmx, -Xmsãå¹ãããã
Java使ãã«ã¨ã£ã¦ã¯ãã«ããããã®ãåéã§ããheapãµã¤ãºã®æå¤§ãæå°å¤ãè¨å®ããJVMãªãã·ã§ã³ã§ãããç¹ã«-Xmsãæå°å¤ã®æ¹ã¯startup timeã«å¹ãã¯ããªã®ã§ãç«ã¡ä¸ãããéããªãã¯ãããã32MBãæå®ãã¦è©¦ãã¦ã¿ãã¨ããã
time jruby -J-client -J-Xms32m -X-C -X-O -ve "require 'rubygems'; require 'columnize'" jruby 1.5.0 (ruby 1.8.7 patchlevel 249) (2010-05-12 6769999) (Java HotSpot(TM) Client VM 1.5.0_24) [i386-java] real 0m1.167s user 0m0.978s sys 0m0.138s
ã¾ã£ãããªãã·ã§ã³ç¡ã(-J-clientã¯ãã£ãã©ã«ãã ããä»ãã¦ããªã)ã®
time jruby -ve "require 'rubygems'; require 'columnize'" jruby 1.5.0 (ruby 1.8.7 patchlevel 249) (2010-05-12 6769999) (Java HotSpot(TM) Client VM 1.5.0_24) [i386-java] real 0m1.320s user 0m1.090s sys 0m0.148s
ã¨æ¯ã¹ãã¨ãéããªã£ã¦ããããããã£ãããã ãã®ããã°ã©ã ã ãããéãã¯ãããã ããã©ã
JVMã®ãªãã·ã§ã³ã¯
http://performance.netbeans.org/howto/jvmswitches/index.html
http://java.sun.com/javase/technologies/hotspot/vmoptions.jsp
ãªãã¦ããã®ãããããNutteræ°ã¯
Headius: My Favorite Hotspot JVM Flags
ãªãã¦ãããªã¿ã¯ãªããã°ãæ¸ãã¦ããããã¾ã ãããããã¥ã¼ãã³ã°ã§ãããã試ãã¦ãã ãããããããã
ããã§ã¨ãããã以å¤ã«ããNailgunãä½¿ãæ¹æ³ã¨ããRubyã®ã¯ã©ã¹ãã³ã³ãã¤ã«ãã¦ãã¾ã(http://kenai.com/projects/jruby/pages/JRubyCompiler)ã¨ããJRubyã«ã¯å®è¡é度ã«ã¤ã¼ã³æ¹æ³ãã¾ã ã¾ã ããã¾ããããã©ãã¿ã©ãããããããã®ããããæ¸ããããªããã¨æãã
ã¨ããã§ãNutteræ°ã®"Startup Time Tips"ããè¿ããã¡ã«æ¥æ¬èªè¨³ãã¾ããã£ã¦@hiro_asariã3æã¯ããã«tweatãã¦ãããã ããã©ãã©ããã§å ¬éããã¦ããã®ãããï¼â訳ãã¾ããâã¨ãâå ¬éãã¾ããâã¨ãããã®ã¯è¦ããã¦ããªããããªâ¥
[追è¨]
æ¥æ¬èªè¨³ãããããåºãããã§ãã