å ã«æ¦è¦ãæ¸ãã¨
- caseã®å¤ã¯é£çªãã¤æé (ä¸ããå°ããæ°ã§ä¸ã«è¡ãã»ã©å¢ãã¦ãã)ã¨ãªãããã«å¿ããããããç¨åº¦caseãå¤ããã°ãã¨ã¯åæã«ã³ã³ãã¤ã©ããã¼ãã«ã¸ã£ã³ãæé©åãè¡ãã
- gotoæãreturnæãªã©ã§switchæéä¸ããæããªãããã«ãããããã¨ãã¼ãã«ã¸ã£ã³ãæé©åãè¡ãããªãã
- MSVC(Visual Studio)ã§ã³ã³ãã¤ã«ããå ´åãdefault:ã®ç®æã«__assume(0);ã¨è¨è¿°ããã°ä¸è¨äºã¤ãç¡è¦ãã¦å¼·å¶çã«ãã¼ãã«ã¸ã£ã³ãæé©åãè¡ãããããã ããgotoã»returnçã§ã®switchæããããã¾ã¾å®è¡ããã¨ä¾å¤ã¨ã©ã¼ãçºçããã
- 追è¨ï¼ä¸è¨ã®__assume(0)ã¯C++23ãããstd::unreachable()ãã§æ¨æºå
Â
FM TOWNSã¨ãã¥ã¬ã¼ã¿ã津軽ããå¼ã£ã¦ãã¦ãã©ããã¨ãã¥ã¬ã¼ã¿ã¼ã®å®è¡é度ãæ¢ã«ããTOWNSã¨ãã¥ã¬ã¼ã¿ã®ãããã¥ããåããããªx86 CPUãç©ãã PC(PC98ã»IBM PC)ã®ã¨ãã¥ã¬ã¼ã¿ã¨æ¯ã¹ã¦ãé éããã®ãè¬ã ã£ãã®ã§ã¡ããã¨èª¿ã¹ããã¨ã«ããã®ãäºã®å§ã¾ããã©ã®ãããé ãã®ãã¨ããã¨ç¾å¨Core i5 12600ãæè¼ããPCã使ã£ã¦ããã®ã ãã©ã486 66MHzãããã®CPUé度ãã¨ãã¥ã¬ã¼ãããã®ãã®ãªã®ãªã¨ãã£ãã¨ãããã¨ãã¥ã¬ã¼ã¿ã«ã¯å¯¾è±¡ã®ãã·ã³ãããä½åãã®æ§è½ãæã£ããã®ãå¿ è¦ãªã®ã¯ä»æ¹ãªãã«ãã¦ããã·ã³ã°ã«ã³ã¢ã§æ¯è¼ãã¦ããããã300åå·®ããããã ãããã£ã¨æ©ãã¦ãè¯ãããã ãç°¡åã«èããã¨ã¨ãã¥ã¬ã¼ãããCPUã®1å½ä»¤ãå®è¡ããããã«ããã¹ãå´ã®CPUã§ã¯300å½ä»¤ãå®è¡ãã¦åç¾ãããã¨ãã¦ããããããã¾ã§é ããã§ããã
å ¬éããã¦ãã津軽ã®ã½ã¼ã¹ã³ã¼ããè¦ã¦ãä»ã®ã¨ãã¥ã¬ã¼ã¿ã¨æ¯ã¹ã¦ã大差ãªããããªCPUã³ã¢ã®å®è£ ã§ã³ã¼ããè¦ãã ãã§ã¯å ¨ãåå ããããããVisual Studioã®è¨ºæãã¼ã«ãå®è¡(RelWithDebInfoè¨å®ã§ã³ã³ãã¤ã«ããããããã¯ã®éå§ããã¿ã³ãæ¼ãã¦CPUæéã®æ¸¬å®ã§è¨é²)ããã³ã¼ãã§æéãããã£ã¦ããå¦çã®å ´æãå¯è¦åããã¨ãã
CPUã®ä¸å½ä»¤ãå®è¡ããRunOneInstructioné¢æ°ã§ãCPUå½ä»¤ãèªã¿è¾¼ãFetchInstructioné¢æ°å ã®å¦çãé¤ãã¨ãèªã¿è¾¼ã¾ããCPUå½ä»¤ãå ã«åå²ãã¦å¦çãå®è¡ããswitchæã®ä¸ãæã ãã§ããªãã¨ããã°ã©ã å ¨ä½ã®ç´16%ãã®å¦çæéãããã£ã¦ããã
ãã®switchæã確èªããã¨
switch
{
case 0:
â
break;
case 1:
â
break;
case 2:
â
break;
case 3:
â
break;
â
case 33:
â
break;
case 39:
â
break;
â
case 34:
â
break;
â
case 22:
â
break;
ã¨CPUå½ä»¤ã®ãªãã³ã¼ãæ°å¤é ãããªããå½ä»¤ã®ãªãã©ã³ã(å®è¡ããå½ä»¤ã®æ¼ç®å¤ã®å ´æãæå®ããã¨ãã)é ã«ä¸¦ã¹ã¦ããã®ãå¾®å¦ã«é£ã³é£ã³ã«caseã®å¤ãæå®ããã¦ãããããã ã¨ã¸ã£ã³ããã¼ãã«ã¨ããé åã«ã¸ã£ã³ãå ã®ã¢ãã¬ã¹ãå ¥ãã¦ããããèªã¿è¾¼ãã§ããåå²ãè¡ããããã¼ãã«ã¸ã£ã³ãã¨ããå®è£ æ¹æ³ã§ã®æé©åãã³ã³ãã¤ã©ã§è¡ãããã大éã®ifï½else ifï½æã«ç½®ãæãããã¦ãã¾ãã®ã ãããå½ä»¤æ°ãæ°ããã¨ãããªãã¨308åããããå ¨é¨ifæã¨ãªãåå²ãããã©ãã調ã¹ãããã®ã§ãå¦çã«æéãããã£ã¦ããã®ã ã
æ¬æ¥ãªãã°ã
case 0:
case1:
â
case 27:
â
case 34:
â
case 39:
â
ã¨ã¡ããã¨æ´çããã°ããã®ã ããããå¯è¦åããããããã«ãã®ä¸¦ã³é ã«ãã¦ããå¯è½æ§ãããã®ã¨ããããã308åã®caseæã並ã³å¤ããã®ãé¢åã ãã並ã³æ¿ãã«å¤±æãã¦ãããæé©åãè¡ãããªãã¨ãªãã®ã§ä½ã¨ããã®ã¾ã¾ã®ç¶æ ã§ãã¼ãã«ã¸ã£ã³ãæé©åãè¡ãããªããã¨èª¿ã¹ãããåãããã«ã¨ãã¥ã¬ã¼ã¿ã®CPUã³ã¢å®è£ ã§ãã¼ãã«ã¸ã£ã³ãã®èå¯ãè¨è¼ãã¦ãããµã¤ãã«ã¦MSVCã®ã¿ã§ã¯ããããdefault:ã«__assume(0)ã¨è¿½è¨ããã¨ãã¼ãã«ã¸ã£ã³ãã«å±éãããã¨ãã£ãã®ã§èã«ãç¸ãæãã§ã
default:
#if defined(_MSC_VER)
    __assume(0);
#endif
ã¨ãã®3è¡(å®è³ª1è¡)ã®ã¿è¿½è¨ãã¦ä»ã¯æã¤ããã®ã¾ã¾å度診æãã¼ã«ãå®è¡ããã¨ããã
ãªãã¨ãããã¨ã§ããã(ããã©ã¼ã¢ãã¿ã¼é¢¨)
ç´16%ãCPUæéãå ãã¦ããswitchæãç´1.6%ã¨ã»ã¼ååã®ä¸ã¾ã§è² è·ãä¸ãã£ã¦ããã¾ããã
津軽ã®ã¨ãã¥ã¬ã¼ãé度ãå³ãæ¹æ³ãã³ãã³ãå ¥åã§ãpri timebalanceãã§1msæ¯ã®ã¨ãã¥ã¬ã¼ãã®é²è¡é度ã示ãå¤å¤å¹³åã§ããæ¹è¯åã¯Core i5 12600ç°å¢ã§VM CPUé度 66MHzè¨å®ã«ãããTownsOSä¸ã®è¨æ¸¬å¤ã¯ç´170000ã ã£ãã®ãã
ãã¼ãã«ã¸ã£ã³ãå®è£ å¾ã¯ç´220000ã¾ã§åä¸ããã£ã1è¡ã ãã§1.3åã®é«éåãå®ç¾ã§ãããããä¸ãæãã¼ãã«ã¸ã£ã³ãåããã¦ããªãããããé·ãswitchæããã£ã¦ãã¡ãããã¼ãã«ã¸ã£ã³ãå(__assume(0)追è¨ã ãã§ãªãcaseãæ´ç)ãããããã¡ãã¯RunOneInstructionå ã»ã©ã®å¹æã¯ãªãã£ãããã®switchæ以å¤ã«ãã¾ã ããã«ããã¯ã«ãªã£ã¦ããã¨ããã¯ããããããã ãã§ãã ãã¶åä½ã軽ããªã£ãã絶大ãªå¹æããã£ãã®ã§Githubã§pull requestãéä¿¡ãããã£ããmerge(ã³ã¼ãã®å¤æ´ãæ¿èª)ããããã¾ã Githubä¸ã®ã³ã¼ãã§ã®ã¿åæ ããã¦ããªããã次åã®ãªãªã¼ã¹çã§ã¯åãã¼ã¸ã§ã³ããã軽ããªã£ã¦ããã¯ãã ã
Â
ãªããVCã§ã¯__assume(0);ã§ã¾ãééããªããã¼ãã«ã¸ã£ã³ãåãããã®ã ããcaseå ã§gotoãreturnãªã©ã使ãswitchæéä¸ã§æãããããªå¦çãæ¸ãããã«å°éããã¨ä¾å¤ã¨ã©ã¼ãçºçãã¦ãã¾ãã®ã§è¦æ³¨æãcaseã§å¦çãçµããå ´åã¯break;ã§switchæä¸ã«ç¶ãããã«ããã