If the interpreter finds that the switch variable x is Each caseĭefines a complete and independent suite much like in an The switch statement should not implement fall-throughīehaviour (as does the switch statement in C). Thomas Wouters has written a patch which demonstrates theĪbove. Where the ‘SWITCH’ opcode would jump to 14, 22, 30 or 38 Solution 2: Adding a switch statement to Python New SyntaxĦ LOAD_FAST 0 ( x ) 9 LOAD_CONST 1 ( switch - table - 1 ) 12 SWITCH 26 ( to 38 ) 14 LOAD_CONST 2 ( '1' ) 17 PRINT_ITEM 18 PRINT_NEWLINE 19 JUMP 43 22 LOAD_CONST 3 ( '2' ) 25 PRINT_ITEM 26 PRINT_NEWLINE 27 JUMP 43 30 LOAD_CONST 4 ( '3' ) 33 PRINT_ITEM 34 PRINT_NEWLINE 35 JUMP 43 38 LOAD_CONST 5 ( "D'oh!" ) 41 PRINT_ITEM 42 PRINT_NEWLINE > 43 LOAD_CONST 0 ( None ) 46 RETURN_VALUE The optimization should only be used for if-elif-elseĬonstructs which have a minimum number of n cases (where n isĪ number which has yet to be defined depending on performance (which could be used to reach protected code), the jump-table To prevent post-modifications of the jump-table dictionary Subtypes, since it’s not clear whether these are still Immutable types: int, float, string, unicode, etc. Safely be implemented either if a “from _future_” styleįlag is used, or the switching variable is one of the builtin _hash_ calls in if-elif-else constructs which are affectedīy the optimization). Semantics (by reducing the number of _cmp_ calls and adding The new optimization should not change the current Python SWITCH opcode and proceeding with the usual if-elif-else byte The standard if-elif-else processing by simply skipping the This condition is not met, the interpreter should revert to ![]() Use the hash table for finding the right opcode snippet. ![]() Well-known immutable types (strings, unicode, numbers) and Triggers the following run-time behaviour:Īt runtime, SWITCH would check x for being one of the Table, store it in the constants and add an opcode SWITCH inįront of the standard if-elif-else byte code stream which The compiler could then setup a read-only (perfect) hash Should be comparable to the type of the left hand switch Right hand sides need not be all of the same type, but they The right hand side a hashable immutable builtin type. the left hand side always references the same variable, If-elif-else construct which has the following signature: ![]() It should be possible for the compiler to detect an (hashable, comparable, a=b => hash(a)=hash(b)). Terms of requiring that both the switch variable and theĬonstants need to be compatible to the dictionary implementation Jump location, so they both share the same problem space in Involve some run-time overhead to assure that the switchingīoth solutions use a dictionary lookup to find the right Keywords to the language, while the second looks cleaner. The first solution has the benefit of not relying on adding new
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |