ããããè¨äºãèªãã ã
transparent-to-radiation.blogspot.com
ãªããScalaã®ã¡ã¢ãªä½¿ç¨éãç°å¸¸ã«å¤ããªã¨æã£ã¦ã調ã¹ããæ¤è¨¼ã³ã¼ããã¢ããããã
æ¤è¨¼ã¨ãã¦ãæ§ã
ãªJVM(OpenJDKã¨ãCorettoã¨ã)ã¨ãã®ãã¼ã¸ã§ã³(8, 11, 17)ã§JARãå®è¡ãã¦èå¯ãããJVMãç¨æããããã«ASDFã使ã£ããã¾ããASDFããå¼ã£ã±ã£ã¦ãããªãJVMã®ãã¼ã¸ã§ã³(OpenJDKã®8ãªã©)ã¯çç¥ãã¦ããã JAVA_OPTS
ã¯-Xms100M -Xmx2G
ã§ããã
æå
ã®ãã·ã³(Linux x86_64 Xeon W-10855M 2.80GHz 64GiB RAM)ã§ã®run-matrix.sh
ã®å®è¡çµæã¯ã以ä¸ã®éã(ååã¯ãJVMãè¨ç®ä»¶æ°ãå®è¡æéSecãã¡ã¢ãªç·ä½¿ç¨éKiB)ã
openjdk-11 openjdk 11 1000 0.35 62612 openjdk-11 openjdk 11 1000000 0.62 115120 openjdk-11 openjdk 11 10000000 3.78 688544 openjdk-11 openjdk 11 100000000 46.77 1535268 openjdk-17 openjdk 17 1000 0.51 63140 openjdk-17 openjdk 17 1000000 0.70 110328 openjdk-17 openjdk 17 10000000 2.57 695104 openjdk-17 openjdk 17 100000000 32.91 1566164 temurin-8.0.362+9 temurin 8 1000 0.49 60696 temurin-8.0.362+9 temurin 8 1000000 0.77 145188 temurin-8.0.362+9 temurin 8 10000000 3.76 372256 temurin-8.0.362+9 temurin 8 100000000 49.37 2240764 temurin-11.0.18+10 temurin 11 1000 0.42 61728 temurin-11.0.18+10 temurin 11 1000000 0.62 124076 temurin-11.0.18+10 temurin 11 10000000 2.20 686860 temurin-11.0.18+10 temurin 11 100000000 33.52 1891060 temurin-17.0.6+10 temurin 17 1000 0.35 65692 temurin-17.0.6+10 temurin 17 1000000 0.48 113516 temurin-17.0.6+10 temurin 17 10000000 2.09 693068 temurin-17.0.6+10 temurin 17 100000000 32.92 1967876 corretto-8.372.07.1 corretto 8 1000 0.58 61340 corretto-8.372.07.1 corretto 8 1000000 0.94 160644 corretto-8.372.07.1 corretto 8 10000000 3.79 372752 corretto-8.372.07.1 corretto 8 100000000 49.09 2241108 corretto-11.0.19.7.1 corretto 11 1000 0.36 62172 corretto-11.0.19.7.1 corretto 11 1000000 0.56 127616 corretto-11.0.19.7.1 corretto 11 10000000 2.26 688568 corretto-11.0.19.7.1 corretto 11 100000000 35.94 1992936 corretto-17.0.7.7.1 corretto 17 1000 0.29 66072 corretto-17.0.7.7.1 corretto 17 1000000 0.39 114328 corretto-17.0.7.7.1 corretto 17 10000000 2.04 693248 corretto-17.0.7.7.1 corretto 17 100000000 30.20 1285704 graalvm-22.3.1+java11 graalvm 11 1000 0.36 118316 graalvm-22.3.1+java11 graalvm 11 1000000 0.49 151004 graalvm-22.3.1+java11 graalvm 11 10000000 1.52 177736 graalvm-22.3.1+java11 graalvm 11 100000000 23.67 301800 graalvm-22.3.1+java17 graalvm 17 1000 0.34 113060 graalvm-22.3.1+java17 graalvm 17 1000000 0.41 138260 graalvm-22.3.1+java17 graalvm 17 10000000 1.52 159484 graalvm-22.3.1+java17 graalvm 17 100000000 23.92 264100
å®è¡æéãJVMã®ãã©ã³ãã¨è¨ç®åæ°ã§åé¡ãã¦æ£ããããã«ããã¨ä»¥ä¸ã®ããã«ãªããä¸çªå ¨ä½çãªæ§è½ãåºã¦ãããªã®ã¯ææ°ã®GraalVMã ã£ãã
ã¡ã¢ãªä½¿ç¨éã ã¨ä»¥ä¸ã®ããã«ãªãã
JVMã®ãã¼ã¸ã§ã³ã ãååºãã¨ä»¥ä¸ã®éãã
ãã®æ¤è¨¼ã®éç¨ã§æ³¨ç®ãã¹ããã¤ã³ããããã¤ãè¦ä»ããã
JVM
ããããã§ãåããã¨ãããGraalVMã¯é¡èã«ã¡ã¢ãªæ¶è²»éãå°ãªãããè¨ç®æ§è½ãè¯ããã¡ã¢ãªæ¶è²»ãå°ãªãçç±ã¯ãã¨ã§èª¬æããããé常ã«é度ãåºã¦ããã®ãé¢ç½ããææ°çã®GraalVMã§ã®åä½æéã¯ã100000000åã®è¨ç®ã23ç§ã§æ¸ã¾ãã¦ãããããªãè³¢ãæé©åããã¦ããã®ããªã¨æãã
ã¾ããJavaã®ãã¼ã¸ã§ã³ã8ãã11ã«ã11ãã17ã«ãªãã«ã¤ãã¦è¨ç®ã¨ã¡ã¢ãªæ¶è²»ã®æ§è½ãããããè¯ããªã£ã¦ããã®ãé¢ç½ããçããJavaã®ãã¼ã¸ã§ã³ã¯ä¸ãã¾ãããããããªã«èª¿åè¯ããªãã¨ã¯æã£ã¦ãªãã£ãã®ã§é©ãããã©ãæãã°Java8ãåºãã®ã¯2014å¹´ã®ãã¨ãªã®ã§ããããã10å¹´çµã¤ããã§ã10å¹´åã®é²æ©ãããã«åºã¦ããã®ããªã¨æãããããã«10å¹´ã¯ãã«ãã
GC
ãã®ãã¹ãã§ã¯GCã¯ç¹ã«æå®ãã¦ãªãã£ãããå
è¨äºã§ãGCãããã©ã«ãã®ã¾ã¾ã«ãã¦ããããã ãJava/Scalaã¨ãã£ãJVMãã¡ããªã¼ã§ã¯ãã¡ã¢ãªä½¿ç¨éãã¨ãããã®ã¯ã¡ãã£ã¨ããªããã¼ã§ãå
¨ä½ã¨ãã¦ã¯ã-Xmx
ãªãã·ã§ã³ã§JVMã«ä¸ããããæ大ã¡ã¢ãªå®¹éã絶対ã«è¶
ããªããã¨ããå
±éç¹ããããã®ã®ããããã©ã使ããã¯GCã¢ã«ã´ãªãºã ã«ä»»ãããã¦ãã¦ãæè¿ã®ã¢ã«ã´ãªãºã ã¯ãä¸ããããæ大å¤ãã®ã³ã®ã³ä½¿ã£ã¦æé«ã®ããã©ã¼ãã³ã¹ãåºããæ¹åæ§ã§èª¿æ´ããå¾åã«ããããã ã常æèµ·åãåæã®ãµã¼ãç¨éã ã¨ãã®æ¯ãèããé©åã§ããããã¨ã¯æ³åãã¤ãã¨æãã
JVMè¨èªã«ããã¦ãæ大ã¡ã¢ãªä½¿ç¨éã大ããå°ããã¨ããè°è«ã¯JVMã®è©±ã§ããåãªããã¥ã¼ãã³ã°è¦ç´ ã«ãªã£ã¦ãã¾ããã³ã¼ããã³ã³ãã¤ã©ã®åãæªããé¢ãã話ã¨ãªãããããã£ã¦Java/Scalaãä»ã®è¨èªã¨æ¯ã¹ã¦ã¡ã¢ãªãé£ãã¨ããã®ã¯ééãã§ã¯ãªãã®ã ããã©ããã¾ãåç´åãã§ããªãã¨æããä»ã®è¨èªã§ã¯æ±ºå®çãªãã¡ã¢ãªä½¿ç¨éãã¨ããæ¦å¿µã¯ãJVMç³»è¨èªã§ã¯ããã¥ã¼ãã³ã°ã®å¯¾è±¡ãã§ãã£ã¦ãå¯å¤çãªã®ã§ããã
å®éãGCã¢ã«ã´ãªãºã ã§æ¶è²»ã¡ã¢ãªã¯å¤§ããå¤åãããä¸æ²ã®æ¯è¼ã§ã¯GraalVMãã¡ã¢ãªä½¿ç¨éãå°ãªã(=è¯ãï¼)ã¨ããé°å²æ°ã ã£ãããGraalVMã§ã¯ããã©ã«ãã§Serial GCã使ãã®ã§ãåã«Serial GCãä¿å®çã«ã¡ã¢ãªãååãã¦ããã(ãã®ãããã¹ã«ã¼ããããã¬ã¤ãã³ã·ãå£ã)ã¨ããã ãã®è©±ã®å¯è½æ§ããããã¡ãªã¿ã«Java 9ããG1 GCã¨ããGCãæ¨æºã©ã¤ã³ãããã«å ¥ã£ã¦ãã¦ãããå¤ãã®JVMã§æ¡ç¨ããã¦ãããSerial GCã¨éã£ã¦ãã«ãã¹ã¬ããã§åä½ãã(é¾å¤ã¯ãã¥ã¼ãã³ã°å¯è½ã ã)ãã¾ãç©æ¥µçã«ã¯ã¡ã¢ãªã解æ¾ããªãå¾åã«ããã
試ãã«å
¨ã¦ã®JVMã§-XX:+UseG1GC
ãè¨å®ãã¦G1 GCãå¼·å¶ããã¨ã©ããªããã¨ããã¨ã以ä¸ã®ãããªçµæã«ãªãã
ããã§ããã£ã±ãGraalVMã¯éããã¡ã¢ãªãé£ããªãããã ã£ãããããã大éè¨ç®ãå¾æãªã®ã ãããï¼è³¢ãæ¨è«ããã¦ãã¡ã¢ãªå²å½ã¦ãæé©åãã¦ããã®ãããããªãã
ArrayBuffer
æ§è½ãå£åãã¦ããåå ã®ä»®èª¬ã¨ãã¦èªåãé¸ãã ãã1ã¤ã®ç¹ããå
è¨äºã®æ¤è¨¼ã³ã¼ãã§ä½¿ããã¦ããscala.collection.mutable.ArrayBuffer
ã ãããã¯å
é¨ã«Arrayãæã£ã¦ããããé常ã«é«éãªã¢ã¯ã»ã¹ãå¯è½ã§ããããåçãªæ¡å¼µãå¯è½ãªåªãããã¼ã¿æ§é ã ãã¡ãªã¿ã«å
è¨äºã®Javaçã³ã¼ãã§ã¯java.util.ArrayList
ã使ããã¦ãããããããã©ã¼ãã³ã¹å·®ã®åå ã§ã¯ãªããã¨ç®è«ãã ã®ã ã
å®è£ ãè¦ãã¨ãããããScalaã®ArrayBufferã¯ãããããã確ä¿ãã¦ãããArrayã®å®¹éãè¦ç´ 追å ã§ä¸è¶³ããã¨ããå é¨ã®Arrayã®ãµã¤ãºã2åã«ãã¦ç¢ºä¿ããªãããå 容ãã³ãã¼ããã¨ããæåã«ãªã£ã¦ãããããã巨大ãªã¡ã¢ãªæ¶è²»ã®åå ã§ã¯ãªããã¨æã£ãã®ã ãã¡ãªã¿ã«Javaã®ArrayListã¯ã©ã®ç¨åº¦ã¡ã¢ãªã確ä¿ãããã®ä»æ§ã¯JVMã®å®è£ ã«å§ãã¦ããã®ãé¢ç½ãã
GCãG1GCã«åºå®ããç¶æ ã§ãå®è£ ãæ¸ãæãã¦ArrayListã使ãããã«ããã¨ä»¥ä¸ã®ãããªçµæã«ãªã£ãã
ãã¾ãåçãªå¤åã¯èµ·ãããªãã£ãããã¼ã¿æ§é ã¯æ¯é çã§ã¯ãªãããã ã£ãã
å¶å¾¡æ§é
å
è¨äºã®ã³ã¼ãã§ã¯ãbreakãããããªå¦çã«scala.util.control.Breaks
ã使ã£ã¦ããã
val primeNumbers = new java.util.ArrayList[Int]() val b = new Breaks def isPrimeNumber(n: Int): Boolean = { var isPrime = true b.breakable { for (pn <- primeNumbers.iterator().asScala) { if (pn * pn > n) { isPrime = true b.break } if (n % pn == 0) { isPrime = false b.break } } } return isPrime }
ããã大éã«ä½ããã¦ã¯æ¨ã¦ããã¦ããã®ã§ã¯ãªããï¼ã¨ç®æãã¤ããç´ æ´ãªScalaã®ã³ã¼ãã«æ¸ãæãã(GCã¯G1GCã§åºå®ãã)ã
def isPrimeNumber(n: Int): Boolean = { var isPrime = true val iter = primeNumbers.iterator while (iter.hasNext) { val pn = iter.next() if (pn * pn > n) { return true } else if (n % pn == 0) { return false } } return isPrime }
ãããã¦ãããã«éããªã£ã¦ãã ããã25ç§ãããã§è¨ç®ã§ããããã«ãªã£ããã¡ã¢ãªæ¶è²»ã¯ã¡ãã£ã¨æ¹åãããçµå±GraalVMãæ¶è²»éãå°ãªãã¨ãããã¨ã¯å¤åãªãã ã£ãã
çªå¤: Stack
ã¡ãªã¿ã«Stackã¨ãããã¼ã¿æ§é ãããã
ææ°ã®Scalaã§ã¯Stackã®å é¨ã¯Arrayã«ãªã£ã¦ãã¦ã追å ãèµ°æ»ãé«éã«è¨è¨ããã¦ãããä»åã¯è¿½å ã¨èµ°æ»ããããªãã®ã§Stackã§ããã®ã ãå¶å¾¡æ§é ã¯å ç¨ã®ç°¡ç´ ãªãã¼ã¸ã§ã³ã®ã¾ã¾ãããã使ãã¨ã©ããªãã®ã試ãã¦ã¿ãã
ããªãã¡ã¢ãªæ¶è²»ã®å ·åãè¯ããªã£ããã¨ãããããç¹ã«ãJava 17ã§ã®æ¯ãèãã®è¯ããé¡èã«ãªã£ã¦ãã¦ããã¾ãJVMã®éããæ°ã«ãªããªããªã£ã¦ãã(Graalã¯å®å®ãã¦è»½ãã)ã
çªå¤: Native
çªå¤ç·¨ã¨ãã¦Scala Nativeã§ã³ã³ãã¤ã«ãã¦ã¿ããã£ãããGCãããããè¨ããã¨ãèããã«ç¡éã«ã¡ã¢ãªãé£ã£ãããã¦ãã¾ã£ããboehm GCãã³ã³ãã¤ã«æã«æå®ãã¦ããã¨å®å®ãã¦250MiBãããã®åä½ã§åãã¦ããããåä½é度ãã®ãã®ã¯ãããªã«éããªãã¦ãJITã«ããæé©åãé度ã«å¼·ãå½±é¿ãåã¼ãã¦ããã®ãããããªãã¨æãã(ãã¼ãããã¯ã©ã¹ãå°ãªãã®ã§ãNativeã®ã¡ãªãããåºã«ãã課é¡ã ã£ãããã«æã)ã
çµèª
çµå±Javaãããã¡ã¢ãªæ¶è²»ãå¤ãã®ã¯è¬ã ã£ãããããããGCã¨Scalaã®ç¸æ§ããªã«ãã§ããã¾ãã¡ã¢ãªã解æ¾ãã«ããåä½ã«ãªã£ã¦ããã®ãããããªãã¨æã£ããGraalVMã®åä½ãç½çã§ãéããã¡ã¢ãªããã解æ¾ããã¨ãã夢ã®ãããªæãã ã£ããJavaã«æ¯ã¹ãã¨Scalaã¯ã©ã³ã¿ã¤ã ã¯ã©ã¹ã®ã¶ããä¹ã£ã¦ããããããããã¡ã¢ãªãé£ã£ã¦ããã®ãããããªããªã¨æã£ãã
- ç¹å®ã®ã¦ã¼ã¹ã±ã¼ã¹ã§ã¯GraalVMã使ãã¨ã¡ã¢ãªæ¶è²»ãæããããã¡ã«ãªãããã
- ã¡ã¢ãªã®ç®æ¨ã決ã¾ã£ã¦ãããªãã¡ããã¨ãã¼ããæå®ããªãã¨ãæè¿ã®GCã¯ããããã¡ã¢ãªãé£ããã¨ãã
- æé©ãªãã¼ã¿æ§é ãé¸ã¼ã
- ã¾ã Java8使ã£ã¦ããã§ããï¼