ã翻訳ãgoroutine ã®ä»çµã¿
訳è ã«ããæ¦è¦
Krishna Sundarram æ°ã®è¨äºãHow Goroutines Workãã®ç¿»è¨³ã§ãã
ãgoroutine ã¨ã¯è»½éã¹ã¬ããã§ãããã¨ãã説æã«å¯¾ãã¦æ±ãã§ããã ãã©ã®ããã«ãã¦ä¸¦è¡å¦çãå®ç¾ãã¦ããã®ãããæ¢åã®ã¹ã¬ããå¦çã¨ä½ãéãã®ããããªã軽éãªã®ããã¨ããçåã解æ¶ããæç« ã§ãã
ã¨ã¦ãè¯ãæç« ãªã®ã§ãããç¾å¨ãªã³ã¯åãã«ãªã£ã¦ããã ã¨ã¦ããã£ãããªããã¨ã ã¨æã£ãã®ã§ãæ¥æ¬èªã«ç¿»è¨³ãã¾ããã
åæ: How Goroutines Work (2017/12/02 ç¾å¨ããªã³ã¯åã)
golang ã®ç´¹ä»
ããããªãã golang åå¿è ã§ã並è¡å¦ç(Concurrency)ã¨ä¸¦åå¦ç(Parallelism)ã®éããããããªããã°ãRob Pike æ°ã®ãã¼ã¯ (ãªã³ã¯å ã¯è±èª)ãåç §ãã¦ãã ãããç´ 30 åã®ãã¼ã¯ã§ããã30åè¦è´ããã ãã®ä¾¡å¤ã¯ããã¾ãã
éããè¦ç´ããã¨ãã人ã並è¡å¦çã¨èããã¨ãã«æ³åããã®ã¯ä¸¦åå¦çã®ãã¨ã§ããããããã¯é¢é£ã¯ãã¦ãããã¾ã£ããå¥ç©ã§ãããå¹³è¡å¦çã¨ã¯ãããã»ã¹å士ãç¬ç«ãã¦å®è¡ããããã¨ã§ããã並åå¦çã¯ããããã®è¨ç®ãåæã«å®è¡ãããã¨(å ´åã«ãã£ã¦ã¯äºãã«å½±é¿ããããªãã)ã§ãã 並è¡å¦çã¨ã¯ä¸åº¦ã«ããããã®ãã¨ãæ±ããã¨ã§ããã並åå¦çã¨ã¯ä¸åº¦ã«ããããã®ãã¨ãå®è¡ãããã¨ã§ãã*1
golang ã使ã£ã¦ãæã ã¯ä¸¦è¡å¦çããã°ã©ã ãæ¸ããã¨ãã§ãã¾ããgolang ã¯ãgoroutine åã³ããã goroutine å士ãéä¿¡ããä»çµã¿ãæä¾ãã¾ããæ¬æç« ã§ã¯ãåè ã® goroutine ã«ç¦ç¹ãå½ã¦ã¾ãã
goroutine ã¨ã¹ã¬ããã®éã
Java ãã¹ã¬ããã使ãããã«ãgolang ã§ã¯ goroutine ã使ãã¾ãã両è ã®éãã¯ä½ã§ããããï¼ ãã®éããèªãä¸ã§ 3ã¤ã®è¦ç´ ãããã¾ãããã¡ã¢ãªæ¶è²»éããçæã¨ç ´æ£ã«è¦ããæéããã¹ã¤ããã³ã°ã«è¦ããæéãã§ãã
ã¡ã¢ãªæ¶è²»é
goroutine ã¯ãã¡ã¢ãªã大éã«å¿ è¦ã¨ãã¾ãããã¹ã¿ãã¯é åã§ã¯ 2kB ããæ¶è²»ãããå¿ è¦ã«å¿ãã¦ãã¼ãé åãå²ãå½ã¦ããéæ¾ããããã¾ãã*2*3ä¸æ¹ã§ã¹ã¬ããã¯ã¹ã¬ããéã®ã¡ã¢ãªãå¹²æ¸ãåããªãããã«ãã¹ã¿ãã¯ã¬ã¼ããã¼ã¸ãã¨å¼ã°ãã 1Mb(goroutineã®500å) ã®é åã®ç¢ºä¿ããå§ãã¾ãã*4
ããããã«ããµã¼ãã¼ã¸ãªã¯ã¨ã¹ãããããã³ã«ãgoroutine ãçæããã®ã¯ä½ãåé¡ããªãã§ãããã¹ã¬ããã§ãããããã¨ãæ·±å»ãª OutOfMemoryError ãçºçãã¾ãããã㯠Java ã ãã®å¶éã§ã¯ãªãã並è¡å¦ç㧠OSã®ã¹ã¬ããæ©æ§ã使ãå ¨ã¦ã®è¨èªã«ããã¦ããã®åé¡ã«ç´é¢ãã¾ãã
çæã¨ç ´æ£ã«è¦ããæé
ã¹ã¬ããã¯çæã¨ç ´æ£ã®ãã³ã« OS ã«è¦æ±ãæãã¦ããããå®äºãã¦è¿ã£ã¦ããã®ãå¾ ã¤ããæéããããã¾ãããã®åé¡ãåé¿ããã«ã¯ãä¸åº¦çæããã¹ã¬ãããã¹ã¬ãããã¼ã«ã«ç¶æãã¦ããå¿ è¦ãããã¾ããä¸æ¹ã§ goroutine ã§ã¯ãçæã¨ç ´æ£ã«é¢ããæä½ãé常ã«ä½ã³ã¹ãã§è¡ããã¨ãã§ãã¾ãããã£ã¦ãgolang ã§ã¯ãgoroutine ãæåã§ç®¡çããæ¹æ³ã¯ãµãã¼ãããã¦ãã¾ããã
ã¹ã¤ããã³ã°ã«è¦ããæé
ã¹ã¬ããããããã¯ãããã¨ãå¥ã®ã¹ã¬ãããã¹ã±ã¸ã¥ã¼ãªã³ã°ããã¾ãã ããªã¨ã³ããã£ãæ¹å¼*5ã§ã¹ã¬ãããã¹ã±ã¸ã¥ã¼ãªã³ã°ãããã¹ã¬ããã®å®è¡ãã¹ã¤ãããããéã«ã¹ã±ã¸ã¥ã¼ã©ã¼ã¯å ¨ã¦ã®ã¬ã¸ã¹ã¿ãã¤ã¾ã 16種é¡ã®æ±ç¨ã¬ã¸ã¹ã¿ãPC(ããã°ã©ã ã«ã¦ã³ã¿)ãSP(ã¹ã¿ãã¯ãã¤ã³ã¿)ã16種é¡ã® XMM ã¬ã¸ã¹ã¿ã FP co-processor ã®ç¶æ ã16種é¡ã® AVX ã¬ã¸ã¹ã¿ãããã¦ã¢ãã«åºæã®ã¬ã¸ã¹ã¿ etc... ãå¥ã®å ´æã«ä¿åããããä¿åããããããã¬ã¸ã¹ã¿ã«æ»ãå¦çãå¿ è¦ã«ãªãã¾ããããã¯ã¹ã¬ããéã§è¿ éã«ã¹ã¤ããã³ã°ãããå ´åã«ãç¡è¦ã§ãã¾ããã
goroutine ã¯å調ãã¦ã¹ã±ã¸ã¥ã¼ãªã³ã°ãããã¹ã¤ããã³ã°ãçºçããã¨ããããã£ã 3ã¤ã®ã¬ã¸ã¹ã¿(PCãSPãDX)ããä¿åããããã¬ã¸ã¹ã¿ã«æ»ããããã¾ãããããã¯é常ã«ä½ãã³ã¹ãã§ãã
以åã«èª¬æããããã«ãgoroutine ã®æ°ã¯ä¸è¬çã«ããªãå¤ãã®æ°ã«ãªãã¾ãããã¹ã¤ããã³ã°ã«ãããæéã«éãã¯ããã¾ãããããã«ã¯2ã¤ã®çç±ãããã¾ããããã¯ãå®è¡å¯è½ãª goroutine ã®ã¿èæ ®ããããããã¯ããã goroutine ã¯èæ ®ãããªããã¨ã¨ãã¾ãæè¿ã®ã¹ã±ã¸ã¥ã¼ã©ã¼ã¯ O(1) ãªãããå®è¡å¯è½ãª goroutine ã®æ°ããã¹ã¤ããã³ã°ã®æéã«å½±é¿ãä¸ããªãããã§ãã*6
goroutine ãã©ã®ããã«å®è¡ãããã
åè¿°ããããã«ãgoroutine ã¯çæããç ´æ£ã¾ã§ã©ã³ã©ã¤ã ã管çãã¾ãã ã©ã³ã¿ã¤ã ã«ã¯ goroutine ãå¤éåãããå°æ°ã®ã¹ã¬ãããå²ãå½ã¦ããã¾ãã ãã¤ã§ããããããã®ã¹ã¬ããã¯1ã¤ã® goroutine ãå®è¡ãã¾ããgoroutine ããããã¯ãããã¨ãããã¯ã¹ã¯ããã¢ã¦ããããå¥ã® goroutine ã代ããã«ã¹ã¬ããã«ãã£ã¦å®è¡ããã¾ãã*7
goroutine ã¯å調ãã¦ã¹ã±ã¸ã¥ã¼ãªã³ã°ãããã®ã§ãã«ã¼ããã¤ã¥ãã 1ã¤ã® goroutine ããåä¸ã¹ã¬ããå ã®ä»ã® goroutine ã®å®è¡ã妨ãããã¨ãããã¾ãã Go 1.2 ã§ã¯ãé¢æ°ãå®è¡ããéã«ãGo ã®ã¹ã±ã¸ã¥ã¼ã©ã¼ãå¼ã³åºããããã¨ãããããããã®åé¡ã¯å¤å°ç·©åããã¾ããããã£ã¦ãã«ã¼ãå ã§ã¤ã³ã©ã¤ã³é¢æ°ã§ãªãé¢æ°ãå¼ã³åºãã¦ããã°ãä»ã® goroutine ãã¹ã±ã¸ã¥ã¼ãªã³ã°ããã¾ãã
goroutine ã®ãããã¯
goroutine ã¯ä½ã³ã¹ãã§ãããgoroutine ã以ä¸ã®å ´åã§ãããã¯ãããéã«ãããããå¤éåããã¦ããã¹ã¬ããã®ãããã¯ãå¼ãèµ·ããã¾ããã
- ãããã¯ã¼ã¯å ¥å
- sleep å¦ç
- channel æä½
- sync ããã±ã¼ã¸ã«ããããªããã£ãã®ãããã¯
ãã¨ãæ°ä¸åã® goroutine ãçæããããããããããã¯ãããã¨ãã¦ãã ã©ã³ã¿ã¤ã ã代ããã«å¥ã® goroutine ãã¹ã±ã¸ã¥ã¼ãªã³ã°ãããããã·ã¹ãã ã®ãªã½ã¼ã¹ã¯ç¡é§ã«ãã¾ããã
ç°¡åã«ããã¨ãgoroutine ã¯ã¹ã¬ããã®è»½éãªæ½è±¡åã§ãã Go ããã°ã©ãã¯ã¹ã¬ãããæ±ãããã¾ãåæ§ã« OS 㯠goroutine ã®åå¨ãèªèãã¦ãã¾ãããOSããè¦ãã°ãGo ã®ããã°ã©ã ã¯ãã¤ãã³ãé§åã® Cããã°ã©ã ã®ããã«æ¯ãèãã¾ãã(è注 6 åç §)
ã¹ã¬ããã¨ããã»ããµ
ã©ã³ã¿ã¤ã ãçæããã¹ã¬ããã®æ°ãç´æ¥å¶å¾¡ãããã¨ã¯ã§ãã¾ããããä¸æ¹ã§ããã°ã©ã ãå©ç¨ããCPUããã»ããµã®æ°ã決ãããã¨ã¯ã§ãã¾ãã
ãã㯠runtime.GOMAXPROCS(n)
ãå¼ã³åºãã¦ãå¤æ° GOMAXPROCS ãè¨å®ãããã¨ã§å¯è½ã§ããã³ã¢ã®æ°ãå¢ããã¦ããè¨è¨ã«ãã£ã¦ã¯å¿
ãããããã°ã©ã ã®ããã©ã¼ãã³ã¹ãä¸ããã¨ã¯éãã¾ããããããã¡ã¤ãªã³ã°ãã¼ã«ãå©ç¨ãã¦ãããã°ã©ã ã®çæ³çãªã³ã¢æ°ãè¦ã¤ãããã¨ãã§ãã¾ãã
çµããã«
ä»ã®è¨èªã¨åæ§ã«ãè¤æ°ã® goroutine ã«ãã£ã¦ãå ±æãªã½ã¼ã¹ã®åæã¢ã¯ã»ã¹ãé²æ¢ãããã¨ã¯å¤§åã§ããgoroutine éã§ã®ãã¼ã¿ã®ããåãã¯ãchannel ãéãã¦è¡ãã®ãæåã§ããå ±æã¡ã¢ãªãéãã¦ãã¼ã¿ã®ããåããããã®ã§ã¯ãªãã代ããã« channel ã使ã£ã¦ãã¡ã¢ãªãå ±æãã¾ãã*8
æå¾ã«ãC. A. R. Hoare æ°ã«ãããCommunicating Sequential Processesãã確èªãããã¨ãå¼·ããããããã¾ããå½¼ã¯æ¬å½ã®å¤©æã§ããã1978å¹´ã«çºè¡ããããã®è«æã§ãå½¼ã¯ããã»ããµã®åä¸ã³ã¢ã®æ§è½ãæçµçã«æ¨ªã°ãã«ãªãããããã¡ã¼ã«ã¼ã¯ä»£ããã«ã³ã¢æ°ãå¢ãããã¨ãäºè¦ãã¾ãããå½¼ã®ãããã¼ã¶ã«ã®åæ¥ã¯ãGo è¨èªã®ãã¶ã¤ã³ã«æ·±ãå½±é¿ãä¸ãã¾ããã
*1:ãConcurrency is not parallelismãby Rob Pike
*3:goroutine ã®ã¹ã¿ãã¯ä¸ã§ã®ãµã¤ãºã¯ãGo 1.4 ããã8kB -> 2kB ã«æ¸ãã¾ãããåè
*4:ãFive things that make Go fastã by Dave Cheney
*5:訳注 OSãããã»ããµã®å®è¡æ¨©éã管çãï¼ã¿ã¹ã¯ã®å®è¡ãåãæ¿ããæ¹å¼
*6:Dmitry Vyukov æ°ãã golang-nuts ã°ã«ã¼ãã«ã¦ gorouine ã®ã¹ã±ã¸ã¥ã¼ãªã³ã°ã«ã¤ãã¦è¿°ã¹ã¦ãã¾ããåè
*7:ãAnalysis of the Go runtime schedulerã by Deshpande et al.