On Tue, Mar 15, 2016 at 12:10 PM, BartC <b...@freeuk.com> wrote: > The one-byte-code switch works when all case expressions are known at > compile-time. It makes use of a jump-table within the byte-code. > > The total sequence will be more than one byte-code, typically: > > LOAD_FAST The index > SWITCH Jump to the right label > .... > L5: One of multiple labels > ... Deal with the code in this branch > JUMP_ABSOLUTE Break out of the switch > > ... Provision is needed for the jump-table > > But only one is needed for testing and dispatch. Now I've sketched it out, > perhaps you can fill in the details for yourself... (I'm not getting > involved in CPython development.)
Let's be fair here; anyone can make a single byte-code that does arbitrary amounts of work. (Consider Python's CALL_FUNCTION or the old PRINT_ITEM opcode.) How much work does SWITCH do here? Does the jump table require that the possible case values be compact? If they're not, how does it operate? In Python, the most obvious way to do the repeated comparisons would be a hash lookup. The SWITCH opcode could take a dictionary that maps values to jump targets, with an 'else' target if the lookup fails, and jump to that. It would actually be possible to implement this entirely in an optimizer, with code written like this: if x == 'asdf': ... elif x == 'qwer': ... elif x in ('zxcv', '1234'): ... else: ... Or, of course, it could get its own syntax. ChrisA -- https://mail.python.org/mailman/listinfo/python-list