æè¡é¨ã®ãã«ã¿ã¤ã Rubyã³ããã¿ã®é è¤ï¼@mametterï¼ã§ããæ¨æ¥ã® Hackarade #04 ã®éå¬å ±åã«ç¶ãã2æ¥é£ç¶ã§è¨äºãæ稿ãã¾ãã
ä»åã¯ãããæ¡ä»¶ä¸ã§ã®Rubyã®å®è¡é度ãé«éåãã話ãç´¹ä»ãã¾ãããã®æ¹åã¯ãã§ã«MRIã®å 端ã«ã³ãããããã¦ãã¦*1ãå¹´æ«ãªãªã¼ã¹äºå®ã®Ruby 2.6ã«å«ã¾ããäºå®ã§ãã
ã²ã¨ãã¨ã§è¨ãã¨ããç°¡æ½ããããã¯ãã«ãç´¢å¼ã«ä½¿ããã¨ã§ãããã°ã©ã ã«ã¦ã³ã¿ããè¡çªå·ãè¨ç®ããã¢ã«ã´ãªãºã ãO(log N)ããO(1)ã«æ¹åãããããã«ãããTracePointæå¹æãã³ã¼ãã«ãã¬ãã¸æ¸¬å®ä¸ã§ãé·ã N ã®ã¡ã½ããã®å®è¡ã O(N log N) ãã O(N) ã«é«éåããããã¨ãããã¨ã§ããé ã«èª¬æãã¾ãã
èæ¯ï¼Rubyã®ãã¤ãã³ã¼ãã®æ§é
ãã®æé©åãç解ããã«ã¯ã¾ããRubyã®ãã¤ãã³ã¼ãã®ããç¹å¾´ãç¥ãå¿ è¦ãããã¾ãã
ãã¨ãã°
x = :foo y = :bar z = :baz
ã¨ããRubyããã°ã©ã ã¯ãæ¦å¿µçã«ã¯æ¬¡ã®ãããªãã¤ãã³ã¼ãã«ãªãã¾ãã
PC | å½ä»¤ | è¡çªå· |
---|---|---|
0000 | putobject :foo |
1 |
0002 | setlocal x |
|
0004 | putobject :bar |
2 |
0006 | setlocal y |
|
0008 | putobject :baz |
3 |
0010 | setlocal z |
PCã¯ããã°ã©ã ã«ã¦ã³ã¿ãputobjectã¯ãªãã¸ã§ã¯ããã¹ã¿ãã¯ã«pushããå½ä»¤ãsetlocalã¯ã¹ã¿ãã¯ã®ããããæå®å¤æ°ã«ä»£å ¥ããå½ä»¤ã§ããã½ã¼ã¹ã³ã¼ãã®è¡çªå·ãä¿æãã¦ããã®ããã¤ã³ãã§ããè¡çªå·ã®æ å ±ã¯ãä¾å¤çºçæã®ããã¯ãã¬ã¼ã¹ããã³ã¼ãã«ãã¬ãã¸ã®æ¸¬å®ãªã©ã§å¿ è¦ã«ãªãã¾ãã
ããã§æ³¨ç®ãã¦ã»ããã®ã¯ãè¡çªå·ã®æ å ±ã¯ä¸é¨ã®å½ä»¤ã ããæã£ã¦ããã¨ãããã¨ã§ãï¼ä»ã«ãTracePointã®ã¤ãã³ãã¿ã¤ãããä¸é¨ã®å½ä»¤ã ããä¿æãã¾ãï¼ããã®ããããã®ãã¤ãã³ã¼ãããã®ã¾ã¾ã¡ã¢ãªã«ä¿æããã¨ãç¡é§ãçãããã¨ã«ãªãã¾ã*2ãããã§ãRubyã®VMã¯ãã®ãã¼ãã«ããå½ä»¤åãã¨ãå½ä»¤æ å ±ãã¼ãã«ãã¨ãã2ã¤ã®ãã¼ãã«ã«å解ãã¦ä¿æãã¦ãã¾ãã
PC | å½ä»¤ |
---|---|
0000 | putobject :foo |
0002 | setlocal x |
0004 | putobject :bar |
0006 | setlocal y |
0008 | putobject :baz |
0010 | setlocal z |
å³âï¼å½ä»¤å
å³âï¼å½ä»¤æ å ±ãã¼ãã«
PC | è¡çªå· |
---|---|
0000 | 1 |
0004 | 2 |
0008 | 3 |
å½ä»¤æ å ±ãã¼ãã«ã¯ãè¡çªå·ã®ããå½ä»¤ã®åããæ å ±ãä¿æããªãã®ã§ãä½è¨ãªã¡ã¢ãªãæ¶è²»ãã¾ããã
æ§è½ã®åé¡
ã¡ã¢ãªæ¶è²»éãæ¸ãããã¨ã¯ã§ãã¾ããããå®éã«è¡çªå·ã調ã¹ãå¿ è¦ãçããã¨ããå½ä»¤æ å ±ãã¼ãã«ã表å¼ãããå¿ è¦ãããã¾ãããã®è¡¨å¼ãã¯ãRuby 2.4ã¯ç·å½¢æ¢ç´¢ã§O(N)ãRuby 2.5ã¯äºåæ¢ç´¢ã«æ¹åãã¦O(log N)ã§ããã
ãã®è¡¨å¼ããå®æ°æéã§ãªãããã«ãè¡çªå·ãTracePointã¤ãã³ããã©ã°ãé »ç¹ã«åç §ãããããªç¶æ³ä¸ï¼ãã¨ãã°TracePointæå¹ä¸ï¼ã§ãã¡ãã£ã¨ã³ã£ããããæ§è½ç¹æ§ãçã¾ãã¾ãã次ã®å³ã¯ããã¼ã«ã«å¤æ°ã¸ã®ä»£å ¥ãNåããã ãã®Nè¡ã®ã¡ã½ããããTracePointæå¹ä¸ã§å®è¡ããã®ã«ãããæéã測å®ããçµæã§ãã
Nè¡ã®ã¡ã½ããã®å®è¡ã«ãããæéãéç·å½¢ã«ãªã£ã¦ãããã¨ããããã¾ãã10000è¡ããããããªã¡ã½ãããæ¸ããã¨ã¯ï¼èªåçæãé¤ãã°ï¼ç¨ãªã®ã§ããã¾ãåé¡ã«ãªã£ã¦ãã¾ããã§ããããç´è¦³çãªæåã¨ã¯è¨ãã¾ããããã®ããã«ãªãã®ã¯ãNè¡ã®ã¡ã½ããã¯O(N)åã®å½ä»¤ãããªããç·å½¢æ¢ç´¢ï¼Ruby 2.4ï¼ã ã¨å½ä»¤ãã¨ã«O(N)ã®è¡¨å¼ããè¡ãããã®ã§ãã¡ã½ããã®å®è¡å ¨ä½ã§ã¯O(N ^ 2)ã®æéããããããã§ããäºåæ¢ç´¢ï¼Ruby 2.5ï¼ã¯å§åçã«æ¹åãã¦ãã¾ãããã¾ã O(N log N)ã§ãç·å½¢ã«ã¯ãªã£ã¦ãã¾ããã
ä»åã¯ããããªãæé©åã¨ãã¦ãå½ä»¤æ å ±ãã¼ãã«ã«ãç°¡æ½ããããã¯ãã«ããç´¢å¼ã¨ãã¦ãããããã¨ã§ã表å¼ããå®æ°æéã«æ¹åãã¾ãããããã«ãããNè¡ã®ã¡ã½ããã®å®è¡ã«ãããæéãO(N)ã¨ãããå½ããåã®æå¾ ãéæãããã¨ãã§ãã¾ãã
ç°¡æ½ããããã¯ãã«ã¨ã¯
ç°¡æ½ããããã¯ãã«ã¯ããããã®åã表ããã¼ã¿æ§é ã§ãã
ã¤ã³ããã¯ã¹ | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | ... |
---|---|---|---|---|---|---|---|---|---|---|---|
ãããåãã | 1 | 0 | 1 | 0 | 0 | 1 | 0 | 0 | 1 | 0 | ... |
å³ï¼ããããã¯ãã«ã®ä¾
ã¤ã³ããã¯ã¹ã0..nã®éã«ãã1ã®æ°ãæ°ããæä½ãrank(n)ã¨è¨ãã¾ããä¸ã®ãããåã§èããã¨ããã¨ãã°rank(2)=2ãrank(5)=3ãrank(9)=4ã§ãã
ãã ã®ãããåã§ã¯ãrank(n)ãè¨ç®ããããã«O(n)ã®æéããããã¾ãããããç°¡æ½ããããã¯ãã«ã¯ã¨ã¦ãããããã®ã§ãrank(n)ã®åãåããã«å®æ°æéã§å¿ãã¦ãã¾ãã¾ãããã®ããã«ãå°ãè£å©ãã¼ã¿ãæã¤å¿ è¦ãããã¾ããããã®ãµã¤ãºã¯ãããåèªä½ã®20%ãã25%ç¨åº¦ã§ãï¼ãã ã®ãããåã«æ¯ã¹ãã¨ã1.25åã®ã¡ã¢ãªæ¶è²»ï¼ã*3
ãã®ããã«ãè£å©ãã¼ã¿ãã»ã¨ãã©ä½¿ããã«ï¼æ¼¸è¿çã«o(n)ã®è£å©ãã¼ã¿ã§ï¼é«éã«åãåããã«å¿ãããã¨ãã§ãããã¼ã¿æ§é ããç°¡æ½ãã¼ã¿æ§é ã¨ããã¾ããç°¡æ½ããããã¯ãã«ã¯ãç°¡æ½ãã¼ã¿æ§é ã®æãåºæ¬çãªãã®ã§ããç°¡æ½ãã¼ã¿æ§é ããã¾ã使ãã¨ãé«éå ¨ææ¤ç´¢ããå§ç¸®ãã¼ã¿ãå±éããã«æ¤ç´¢ããã¢ã«ã´ãªãºã ãªã©ã®å¿ç¨ãããã¾ããå ·ä½çãªå®è£ æ¹æ³ãå¿ç¨ã詳ããç¥ãããæ¹ã¯ãä»å¹´çºå£²ããããç°¡æ½ãã¼ã¿æ§é ãï¼å®å ¼é¦å½¦ èï¼ãªã©ããåç §ãã ããã
ç°¡æ½ããããã¯ãã«ã«ããå½ä»¤æ å ±ãã¼ãã«ã®è¡¨å¼ã
ç°¡æ½ããããã¯ãã«ãç¨ãããã¨ã§ãå½ä»¤æ å ±ãã¼ãã«ã®è¡¨å¼ããO(1)ã«ããæ¹æ³ã説æãã¾ããå½ä»¤æ å ±ãã¼ãã«ãåæ²ãã¾ãã
PC | è¡çªå· |
---|---|
0000 | 1 |
0004 | 2 |
0008 | 3 |
å³ï¼å½ä»¤æ å ±ãã¼ãã«
PCã®ã¤ã³ããã¯ã¹ã ã1ã«ãªã£ãç°¡æ½ããããã¯ãã«ãä½ãã¾ãããã®ä¾ã ã¨ãPCã¯0000ã0004ã0008ãªã®ã§ã"10001000100.."ã¨ãããããåãç°¡æ½ããããã¯ãã«ã§è¡¨ç¾ãã¾ãããããç´¢å¼ã«ãªãã¾ãã
ã¤ã³ããã¯ã¹ | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | ... |
---|---|---|---|---|---|---|---|---|---|---|---|---|
ãããåãã | 1 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 | 0 | ... |
å³ï¼ç°¡æ½ããããã¯ãã«ã«ããå½ä»¤æ å ±ãã¼ãã«ã®ç´¢å¼
ããPCã«å¯¾å¿ããè¡çªå·ãå¾ããã¨ãã¯ãrank(PC)ãè¨ç®ãã¾ãããã¨ãã°0004ã ã£ãããrank(4) = 2ã«ãªãã¾ãããã®å¤ã¯ãå½ä»¤æ å ±ãã¼ãã«ã®ä¸ãã2çªç®ã®è¡ï¼è¡çªå·ã¯2ï¼ã§ãããã¨ã表ãã¦ãã¾ããPC=0008ã ã£ããrank(8) = 3ãªã®ã§ãä¸ãã3çªç®ã®è¡ï¼è¡çªå·ã¯3ï¼ã«ãªãã¾ããç°¡æ½ããããã¯ãã«ã®rankæä½ã¯O(1)ãªã®ã§ãå½ä»¤æ å ±ãã¼ãã«ã®è¡¨å¼ããå®æ°æéã§ã§ãããã¨ã«ãªãã¾ãã
ç°¡æ½ããããã¯ãã«ã®åã ãã¡ã¢ãªä½¿ç¨ãå¢ãããã¨ãå¿é ã«ãªãããããã¾ããããããããã®æé©åã«ãã£ã¦å½ä»¤æ å ±ãã¼ãã«ããPCã®åãæ¶ããã¨ãã§ããï¼äºåæ¢ç´¢ã§ã¯PCã®æ å ±ãå¿ è¦ã ã£ãï¼ã®ã§ãçµæçã«ã¯ã ãããç¸æ®ºããã¾ãã
å®é¨
Nè¡ã®ã¡ã½ãããTracePointæå¹ä¸ã§å®è¡ããã®ã«ãããæéã測å®ããçµæã次ã®å³ã§ãã
å³ï¼ç·å½¢æ¢ç´¢âäºåæ¢ç´¢âç°¡æ½ããããã¯ãã«
ç·å½¢æ¢ç´¢âäºåæ¢ç´¢ã»ã©ã®å¤§ããªæ¹åã§ã¯ããã¾ããããè¡æ°ã大ãããªã£ãã¨ãã«äºåæ¢ç´¢ãåé§ãã¦ãããã¨ããããã¾ãã
ã¡ã¢ãªä½¿ç¨éã«å¤åããªããã¨ã確ããããããcookpadã®Webã¢ããªã®ã½ã¼ã¹ã³ã¼ãããã¹ã¦ãã¤ãã³ã¼ãã«ã³ã³ãã¤ã«ããã¨ãã®ã¤ã³ã¿ããªã¿ã®ã¡ã¢ãªä½¿ç¨éã測å®ãã¾ãããå®è¡ã®ãã³ã«ãã¬ãçºçããã®ã§ããããã1000åå®è¡ããã¨ãã®é »åº¦ãåã£ã¦ã¿ã¾ããã
RubyVM::InstructionSequence.compile_file
ã§æ¸¬å®ã/usr/bin/time -f %M kb
ã§æ¸¬å®ã
ã¨ãã«ã¡ã¢ãªä½¿ç¨éã®å¾åãå¤ãã£ã¦ããªããã¨ããããã¨æãã¾ãã
ã¾ã¨ã
ç°¡æ½ããããã¯ãã«ã使ã£ã¦ãRubyã®å½ä»¤æ å ±ãã¼ãã«ã®è¡¨å¼ãé度ãæ¹åãã¾ãããããã«ãããé »ç¹ã«å½ä»¤æ å ±ãã¼ãã«ã®è¡¨å¼ããè¡ãæ¡ä»¶ä¸ã§ã®å®è¡ï¼å ·ä½çã«ã¯TracePointæå¹æãã³ã¼ãã«ãã¬ãã¸æ¸¬å®ä¸ã§ã®å®è¡ï¼ãé«éåããã¾ãã
ç°¡æ½ãã¼ã¿æ§é ã¯çè«çã«ãå®ç¨çã«ãé常ã«èå³æ·±ãæè¡ã§ãå ¨ææ¤ç´¢ãã²ãã 解æãªã©ä¸é¨ã®åéã§ã¯æåã§ãããè¨èªå¦çç³»ã®å®è£ ã«ãæ´»ç¨ã®å¯è½æ§ãããã¨ããã®ã¯æå¤ã§ãã*4ãã¿ãªãããå¿ç¨ãèãã¦ã¿ãã¨ãããããã®ã§ã¯ãªãã§ããããã
追è¨ï¼ãã®è©±ã¯ãã¯ãã¯ãããã®ãã«ã¿ã¤ã Rubyã³ããã¿ã§ãã笹ç°ãç¾ç¶ã®åé¡ãæ´çããé è¤ãç°¡æ½ããããã¯ãã«ãç¨ããã¢ã¤ãã¢ãæãã¤ãã笹ç°ãåæã®å®è£ ãè¡ããé è¤ããããæ¹è¯ãã¦å®ç¾ãããã®ã§ãããã«ã¿ã¤ã Rubyã³ããã¿ãæºã並ã¹ããã¨ã§Rubyãæ¹è¯ãããè¯ãä¾ã§ãã
*1:å®ã¯1æã«ã³ãããããå¤ããã®ã§ãä¼æ´¥å¤§å¦ã§ã®LTã¤ãã³ãã§ç´¹ä»æ¸ã¿ã ã£ãããããã®ã§ãããè¨é²ã®æå³ãããã¦è¨äºã«ãã¦ã¾ãã
*2:ãã±ããªè©±ãã¨æãããããã¾ããããRubyã§ã¯ãã¤ãã³ã¼ãã®ãµã¤ãºã¯ããã¨ç¡è¦ã§ããªãé¢å¿äºã®ã²ã¨ã¤ã«ãªã£ã¦ãã¾ããåãªãã¡ã¢ãªã®ç¡é§é£ãã¨ããã ãã§ãªãããã£ãã·ã¥ãç¡é§é£ããããã¨ã«ãªãã®ã§ãã£ãã·ã¥ãã¹ã«ã¤ãªãããé度ä½ä¸ã®åå ã«ããªãã¾ãã
*3:ç°¡æ½ããããã¯ãã«ã¯ãnçªç®ã®1ã®ã¤ã³ããã¯ã¹ãæ±ããselect(n)ã¨ããæä½ãé«éã«æ±ãã¾ããããããä»åã®æé©åã«selectæä½ã¯ç»å ´ããªãã®ã§ã説æãçç¥ãã¾ãã
*4:é¢é£ç 究ããåç¥ã®æ¹ã¯ãã²æãã¦ãã ããã